首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向数组中添加字符串和字符串数组的混合

向数组中添加字符串和字符串数组的混合
EN

Code Review用户
提问于 2015-12-27 21:57:45
回答 2查看 2.9K关注 0票数 5

原题:

函数将字符串数组或字符串拆分为字符串数组。

有人问我为什么不写多个函数之类的东西。该函数的目的是成为一个单一的函数,并且能够接收不确定数目的字符串或字符串数组并完成工作。不允许限制。

我所写的函数(创造性地命名):

代码语言:javascript
复制
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();
}

我用它来测试它的功能:

代码语言:javascript
复制
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是我想要添加的原始字符串数组。
  • 显然,thing1thing2都是我想要添加到数组中的东西,一个接一个,按object[]的添加顺序排列。

免责声明:我不想削减字节,我想要美观的代码!这是我的激情。

EN

回答 2

Code Review用户

回答已采纳

发布于 2015-12-28 07:56:29

命名约定

正如命名指南所说:

本章的目标是提供一组一致的命名约定,从而产生对开发人员立即有意义的名称。

从这个意义上说,创造性地定义的方法名Add和参数名称ogarrtoadd将不会起作用。

一般命名公约指出:

避免使用与广泛使用的编程语言关键字冲突的标识符。不要将缩写或缩写用作标识符名称的一部分。对可读性的偏爱胜于简洁。

方法名称Add是集合类型用来向集合添加新元素的常用方法名。将源作为第一个参数而要添加的项作为第二个参数的Add方法没有意义。

ogarrtoadd是参数的坏名称,应该至少更改为sourceArrayitemsToAdd或类似的名称。

资本化公约

对于参数名称,请使用camelCasing。

当然,我并不是建议将ogarr改为ogArr,将toadd改为toAdd。参数首先应该有描述性名称。您可以考虑sourceArrayitemsToAdd或类似的。

可能的改进

与代码格式保持一致很重要。考虑使用花括号,总是:

代码语言:javascript
复制
if (toadd[i].GetType() == typeof(string[]))
{
    results.AddRange((string[])toadd[i]);
}
else
{
    results.Add((string)toadd[i]);
}

你说

不允许限制。

那么,为什么将第二个方法参数限制在object[]上呢?让我们把它作为一个IEnumerable。这样,您的方法也可以对string[]List<T>实例进行操作。

代码语言:javascript
复制
public string[] Add(string[] sourceArray, IEnumerable itemsToAdd)

还可以将该方法更改为可拓法 of string[]。这将清楚地说明如果以良好的名称声明该方法的目的:

代码语言:javascript
复制
public static string[] AddItemsOrRanges(this string[] sourceArray, IEnumerable itemsOrRangesToAdd)

您应该始终检查参数是否以null形式传入。如果将toadd.Length作为空引用异常传递给toadd,代码将失败。

代码语言:javascript
复制
if (toadd == null)
{
    // throw ArgumentException() ?
    return ogarr;
}

您还应该检查传递的集合中的项是否为空引用。如果object[] toadd的任何项为空,您的代码将在x.GetType()出现空引用异常时失败。

代码语言:javascript
复制
if (toadd[i] == null)
{
    // throw ArgumentException() ?
    continue;
}

最后,在修复了所有的命名约定冲突和功能混淆之后,该实现可能如下所示:

代码语言:javascript
复制
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();
}

叫它就像:

代码语言:javascript
复制
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);
票数 4
EN

Code Review用户

发布于 2015-12-29 13:13:23

你可以在奥古兹·奥兹格尔的回答中加入一点。如果你总是先做否定检查,那为什么不在foreach循环中呢?您可以轻松地终止else块。

代码语言:javascript
复制
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() ?
    }
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/115210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档