哪个代码更好 [英] Which code is better

查看:69
本文介绍了哪个代码更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以告诉哪个代码更好:



1. br />

hi guys,

Can any one tell which code is better:

1.

public void Ftpfunction(List<string> quoteNos, DataTable dt, string Templatename)
        {
            var row = GlobalVariables.dtglobal.Select("templatename='" + Templatename + "'");
            string path = row[0]["UPSLabelsPathuid"].ToString();            string username = row[0]["UPSLabelsPathuid"].ToString();
            string password = row[0]["UPSLabelsPathpwd"].ToString();

            var ftpfiles = FtpDownload.GetFiles(path, username, password);
            FtpDownload ftpClient = new FtpDownload(path, username, password);
            string desImage = string.Empty;
            foreach (var quote in quoteNos.Where(x => ftpfiles.Contains(x)))
            {
                desImage = System.AppDomain.CurrentDomain.BaseDirectory + @"\image\" + quote + ".jpg";
                if (!File.Exists(desImage))
                    ftpClient.download(quote + ".jpg", desImage);
            }
        }







2:




2:

public void Ftpfunction(List<string> quoteNos, DataTable dt, string Templatename)
        {
            string _sqlWhere = "templatename='" + Templatename + "'";
            DataTable _newDataTable = GlobalVariables.dtglobal.Select(_sqlWhere).CopyToDataTable();

            FtpDownload ftpClient = new FtpDownload(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());
            string desImage = string.Empty;

            var ftpfiles = FtpDownload.GetFiles(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(), _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());

            foreach (var quote in quoteNos.Where(x => ftpfiles.Contains(x)))
            {
                desImage = System.AppDomain.CurrentDomain.BaseDirectory + @"\image\" + quote + ".jpg";
                if (!File.Exists(desImage))
                    ftpClient.download(quote + ".jpg", desImage);
            }
        } 

推荐答案

只要两者都做了所需要的更好变得很大程度上是主观的。然而第一个版本更容易上手,乍看之下更容易理解



请注意......

- <$ c System.AppDomain 上的$ c>系统是多余的

- 变量声明不一致 var ...但是 字符串路径(等)

- desImage = string.Empty; 实际上并不需要(不使用该值)

- desImage 可以在 foreach 循环中声明

- 正如TheRealSteveJudge指出的那样,你的一些变量命名可以改进 - 它不清楚 desImage 会是什么样的。

- 在版本2中格式化将使其更容易阅读,例如而不是一个很长的行引入换行
As long as both do what is required "better" becomes largely subjective. However the first version is easier on the eye and easier to understand at first glance

Note though...
- the System on System.AppDomain is redundant
- declaration of variables is inconsistent var row... but string path (etc)
- desImage = string.Empty; is not actually required (the value is not used)
- desImage could be declared within the foreach loop
- As TheRealSteveJudge pointed out some of your variable naming could be improved - it is not clear what desImage would be for example.
- Formatting in version 2 would make it easier to read e.g. rather than one very long line introduce linefeeds
var ftpfiles = FtpDownload.GetFiles(_newDataTable.Rows[0]["UPSLabelsPath"].ToString(),
                                  _newDataTable.Rows[0]["UPSLabelsPathuid"].ToString(),
                                  _newDataTable.Rows[0]["UPSLabelsPathpwd"].ToString());



版本1超过版本2的主要优点是易用性能够在datarow内容上引入一些验证(我认为这个函数已经发生在这个函数之外) - 在潜入使用数据之前我可能会做一些空检查等


The main advantage for me that version 1 has over version 2 is the ease of being able to introduce some validation on the datarow content (which I presume has happened outside this function) - I would probably be doing some null checking etc before diving in to use the data


第一个c ode看起来干净而且更好..对于维护来说这是好的,因为编码器必须只改变一次值而不是多次。
The first code is looking clean and better.. It is good as for maintenance wise, as coder has to change value for one time only and not multiple times..


这篇关于哪个代码更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆