patterncsharpMinor
Adding a mix of strings and string arrays to an array
Viewed 0 times
mixarraysarrayaddingandstringsstring
Problem
Original question:
Function to split either a string array or string into a string array
I was asked why I didn't write multiple functions or something like that. The point of the function is to be a single one and to be able to receive an indefinite number of strings or string arrays and get the job done. Restrictions are not allowed.
The function I have written (creatively-named):
I use this to test its functionality:
Disclaimer: I'm not trying to cut bytes; I want aesthetic code! This is my passion.
Function to split either a string array or string into a string array
I was asked why I didn't write multiple functions or something like that. The point of the function is to be a single one and to be able to receive an indefinite number of strings or string arrays and get the job done. Restrictions are not allowed.
The function I have written (creatively-named):
public string[] Add(string[] ogarr,object[] toadd)
{
var results = new System.Collections.Generic.List();
results.AddRange(ogarr);
for (int i = 0; i < toadd.Length; i++)
{
if (toadd[i].GetType() == typeof(string[]))
results.AddRange((string[])toadd[i]);
else
results.Add((string)toadd[i]);
}
return results.ToArray();
}I use this to test its functionality:
string[] temp = new string[]{"123","456","789"};
string[] thing1 = new string[]{"abc","def"};
string thing2 = "ghi";
object[] toadd = new object[] {thing1,thing2 };
temp = Add(temp,toadd);tempis the original string array I want to add things to.
thing1andthing2are both things I want to add to that array, one after another, in the order of addition to theobject[], obviously.
Disclaimer: I'm not trying to cut bytes; I want aesthetic code! This is my passion.
Solution
Naming Conventions
As the Naming Guidelines states:
The goal of this chapter is to provide a consistent set of naming
conventions that results in names that make immediate sense to
developers.
In that sense, the creatively defined method name
The General Naming Conventions states:
Avoid using identifiers that conflict with keywords of widely used
programming languages.
Do not use abbreviations or contractions as part of identifier names.
Do favor readability over brevity.
Method name
And in Capitalization Conventions
Do use camelCasing for parameter names.
I'm not suggesting to change
Possible Improvements
Being consistent with code formatting is important. Consider using curly brackets, always:
You say
restrictions are not allowed.
Then, why restricting the second method parameter to
You can also change the method to be an extension method of
You should always check if the parameters are passed in as null. Your code would fail with a null refererence exception at
You should also check the items of the passed in collection for null references. If any of the items of the
Finally, the implementation, with all the naming convention violations and functional confusions fixed, might look like the following:
And calling it would be like:
As the Naming Guidelines states:
The goal of this chapter is to provide a consistent set of naming
conventions that results in names that make immediate sense to
developers.
In that sense, the creatively defined method name
Add and the parameter names ogarr and toadd will not do.The General Naming Conventions states:
Avoid using identifiers that conflict with keywords of widely used
programming languages.
Do not use abbreviations or contractions as part of identifier names.
Do favor readability over brevity.
Method name
Add is a common method name used by collection types to add a new element to the collection. An Add method with source as the first parameter and the items to be added as the second parameter does not make sense.ogarr and toadd are bad names for parameters and should be changed at least to sourceArray and itemsToAdd or similar.And in Capitalization Conventions
Do use camelCasing for parameter names.
I'm not suggesting to change
ogarr to ogArr and toadd to toAdd of course. The parameters should have descriptive names in the first place. You can consider sourceArray and itemsToAdd or similar.Possible Improvements
Being consistent with code formatting is important. Consider using curly brackets, always:
if (toadd[i].GetType() == typeof(string[]))
{
results.AddRange((string[])toadd[i]);
}
else
{
results.Add((string)toadd[i]);
}You say
restrictions are not allowed.
Then, why restricting the second method parameter to
object[]? Let's get that as an IEnumerable. This way, your method can also operate on string[] or List instancespublic string[] Add(string[] sourceArray, IEnumerable itemsToAdd)You can also change the method to be an extension method of
string[]. This will clearly state the purpose of the method if declared with a good name:public static string[] AddItemsOrRanges(this string[] sourceArray, IEnumerable itemsOrRangesToAdd)You should always check if the parameters are passed in as null. Your code would fail with a null refererence exception at
toadd.Length if toadd is passed as null.if (toadd == null)
{
// throw ArgumentException() ?
return ogarr;
}You should also check the items of the passed in collection for null references. If any of the items of the
object[] toadd would be null, your code would fail with a null refererence exception at x.GetType()if (toadd[i] == null)
{
// throw ArgumentException() ?
continue;
}Finally, the implementation, with all the naming convention violations and functional confusions fixed, might look like the following:
public static string[] AddItemsOrRanges(this string[] sourceArray, IEnumerable itemsOrRangesToAdd)
{
if (itemsOrRangesToAdd == null)
{
// throw ArgumentException() ?
return sourceArray;
}
// It is possible with extension methods to have the self instance this as null
// because extension methods are actually static methods and can be invoked
// on a null reference (interesting?)
// string[] myStringArray = null;
// string[] finalStringArray = myStringArray.AddItemsOrRanges(new object[] { "Test" });
// will work!
if (sourceArray == null)
{
// throw ArgumentException() ?
return sourceArray;
}
var results = new System.Collections.Generic.List(sourceArray);
foreach (var itemOrRangeToAdd in itemsOrRangesToAdd)
{
if (itemOrRangeToAdd != null)
{
if (itemOrRangeToAdd.GetType() == typeof(string[]))
{
results.AddRange(itemOrRangeToAdd as string[]);
}
else if (itemOrRangeToAdd.GetType() == typeof(string))
{
results.Add(itemOrRangeToAdd as string);
}
else
{
// What to do if the element is neither a string nor a string[]?
// The (string) cast in the else block would fail in your source code
// throw ArgumentException() ?
}
}
else
{
// How would you determine if the element is to be added or not?
// is it a null string instance, or is it a null string[] instance?
// throw ArgumentException() ?
continue;
}
}
return results.ToArray();
}And calling it would be like:
string[] temp = new string[] { "123", "456", "789" };
string[] thing1 = new string[] { "abc", "def" };
string thing2 = "ghi";
object[] toadd = new object[] { thing1, thing2 };
temp = temp.AddItemsOrRanges(toadd);Code Snippets
if (toadd[i].GetType() == typeof(string[]))
{
results.AddRange((string[])toadd[i]);
}
else
{
results.Add((string)toadd[i]);
}public string[] Add(string[] sourceArray, IEnumerable itemsToAdd)public static string[] AddItemsOrRanges(this string[] sourceArray, IEnumerable itemsOrRangesToAdd)if (toadd == null)
{
// throw ArgumentException() ?
return ogarr;
}if (toadd[i] == null)
{
// throw ArgumentException() ?
continue;
}Context
StackExchange Code Review Q#115210, answer score: 4
Revisions (0)
No revisions yet.