繁体   English   中英

如何减少此代码中使用的变量?

[英]How can I reduce the variables used in this code?

我正在尝试减少以下代码中使用的变量数量。
理想情况下,我想继续重用字符串变量LatestRipMe而不创建LatestRipMeVersion string[]变量。
我是否需要创建一个数组才能执行Skip(1)First()
我能更高效地达到LatestRipMe的最终价值吗?

private void RipMeGetLatestVersion_Process()
{
    //Get the .json file and save it in LatestRipMe string
    LatestRipMe = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json");

    //Create an array from the previously saved string, and skip the first line
    LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray();

    //Put the array back in a string, and select only the first line
    LatestRipMe = LatestRipMeVersion.First();

    //The characters which need to be removed
    char[] LatestRipMeTrim = { ' ', ' ', '"', 'l', 'a', 't', 'e', 's', 't', 'V', 'e', 'r', 's', 'i', 'o', 'n', '"', ' ', ':', ' ', '"', '"', ',' };

    //Trim the above declared characters
    LatestRipMe = LatestRipMe.Trim(LatestRipMeTrim);

    //Show the remaining string content in the TextBlock
    LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
}

你可以更换

  //Create an array from the previously saved string, and skip the first line
  LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray();

  //Put the array back in a string, and select only the first line
  LatestRipMe = LatestRipMeVersion.First();

  LatestRipMe = LatestRipMe.Split(Environment.NewLine.ToCharArray())[1];

甚至

  LatestRipMe = LatestRipMe
           .Split(Environment.NewLine.ToCharArray())[1]
           .Trim(new char[] { ' ', 'a', 'b' });  // etc ...

观点:
风格明智,这很丑陋,难以调试,并且会抛出异常,该站点不会返回您期望的内容(没有互联网连接等)。 我真的不明白替换清晰的、记录在案的分步代码只是为了减少变量数量的愿望,除非它是一个编程挑战。

private void RipMeGetLatestVersion_Process()
{
    LatestRipMe = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json");

    LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray())[1];

    LatestRipMe = LatestRipMe.Trim("  \"latestVersion\" : \"\",".ToCharArray());

    LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
}

这应该给你你想要的东西,但要注意这段代码可能会出错很多。

private void RipMeGetLatestVersion_Process()
    {
        LatestRipMeVersion = ClientRipMe
            .DownloadString("http://www.rarchives.com/ripme.json")
            .Split(Environment.NewLine.ToCharArray())
            .Skip(1)
            .ToArray();

        LatestRipMe = LatestRipMeVersion.First()
            .Trim(' ', ' ', '"', 'l', 'a', 't', 'e', 's', 't', 'V', 'e', 
            'r', 's', 'i', 'o', 'n', '"', ' ', ':', ' ', '"', '"', ',');
        LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
    }

正如很多人已经告诉你的那样,这段代码有太多问题。 您应该使用 JSON 解析器并遵循一些标准约定来命名和构建代码。 我有一种感觉,你的代码仍然没有做你想要它做的事情。 看看我下面的版本,它可以正确地执行您想要的操作(从 json 中获取属性 latestVersion 的值)。 毫无疑问,这是很好的代码,但比你拥有的要好得多

static void Caller() {
    LatestRipMeVersionText.Text = "Latest RipMe version: " + GetLatestVersion();
}

private string GetLatestVersion() {
    var ripmeJson = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json").      //Get the .json file and save it in LatestRipMe string
        Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)[1].     // convert to an array and pick the 2nd element
        Split(':')[1].Trim('"', ',', ' ');                                                  // fetch the value part

    return ripmeJson;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM