原题:
有人问我为什么不写多个函数之类的东西。该函数的目的是成为一个单一的函数,并且能够接收不确定数目的字符串或字符串数组并完成工作。不允许限制。
我所写的函数(创造性地命名):
public string[] Add(string[] ogarr,object[] toadd)
{
var results = new System.Collections.Generic.List<string>();
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();
}我用它来测试它的功能:
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);temp是我想要添加的原始字符串数组。thing1和thing2都是我想要添加到数组中的东西,一个接一个,按object[]的添加顺序排列。免责声明:我不想削减字节,我想要美观的代码!这是我的激情。
发布于 2015-12-28 07:56:29
正如命名指南所说:
本章的目标是提供一组一致的命名约定,从而产生对开发人员立即有意义的名称。
从这个意义上说,创造性地定义的方法名Add和参数名称ogarr和toadd将不会起作用。
一般命名公约指出:
避免使用与广泛使用的编程语言关键字冲突的标识符。不要将缩写或缩写用作标识符名称的一部分。对可读性的偏爱胜于简洁。
方法名称Add是集合类型用来向集合添加新元素的常用方法名。将源作为第一个参数而要添加的项作为第二个参数的Add方法没有意义。
ogarr和toadd是参数的坏名称,应该至少更改为sourceArray和itemsToAdd或类似的名称。
在资本化公约中
对于参数名称,请使用camelCasing。
当然,我并不是建议将ogarr改为ogArr,将toadd改为toAdd。参数首先应该有描述性名称。您可以考虑sourceArray和itemsToAdd或类似的。
与代码格式保持一致很重要。考虑使用花括号,总是:
if (toadd[i].GetType() == typeof(string[]))
{
results.AddRange((string[])toadd[i]);
}
else
{
results.Add((string)toadd[i]);
}你说
不允许限制。
那么,为什么将第二个方法参数限制在object[]上呢?让我们把它作为一个IEnumerable。这样,您的方法也可以对string[]或List<T>实例进行操作。
public string[] Add(string[] sourceArray, IEnumerable itemsToAdd)还可以将该方法更改为可拓法 of string[]。这将清楚地说明如果以良好的名称声明该方法的目的:
public static string[] AddItemsOrRanges(this string[] sourceArray, IEnumerable itemsOrRangesToAdd)您应该始终检查参数是否以null形式传入。如果将toadd.Length作为空引用异常传递给toadd,代码将失败。
if (toadd == null)
{
// throw ArgumentException() ?
return ogarr;
}您还应该检查传递的集合中的项是否为空引用。如果object[] toadd的任何项为空,您的代码将在x.GetType()出现空引用异常时失败。
if (toadd[i] == null)
{
// throw ArgumentException() ?
continue;
}最后,在修复了所有的命名约定冲突和功能混淆之后,该实现可能如下所示:
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<string>(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();
}叫它就像:
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);发布于 2015-12-29 13:13:23
你可以在奥古兹·奥兹格尔的回答中加入一点。如果你总是先做否定检查,那为什么不在foreach循环中呢?您可以轻松地终止else块。
foreach (var itemOrRangeToAdd in itemsOrRangesToAdd)
{
if (itemOrRangeToAdd == null)
{
continue;
}
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() ?
}
}https://codereview.stackexchange.com/questions/115210
复制相似问题