我如何减少在这段代码中使用的变量
本文关键字:变量 代码 何减少 段代码 | 更新日期: 2023-09-27 18:06:54
我试图减少下面代码中使用的变量的数量。
理想情况下,我想继续重用字符串变量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;
}
很多人已经告诉过你,这段代码有太多的错误。您应该使用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;
}
这应该给你你想要的,但要注意,这段代码有很多可能出错的地方。
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;
}