关闭

C#程序中注释过多的8条理由

发表于:2013-6-20 09:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:James Li    来源:51Testing软件测试网采编

  而修改的异常捕获策略之后,代码像这样,也不友好

/// <summary>
        /// 复制文件,如果目标文件已经存在则覆盖掉
        /// </summary>
        /// <param name="oldFile">源文件</param>
        /// <param name="newFile">目标文件</param>
        public static void CopyFile(string oldFile, string newFile)
        {
            //try
            //{
                File.Copy(oldFile, newFile, true);
            //}
           // catch (Exception exc)
            //{
            //    throw new Exception(exc.ToString());
            //}
        }

  应该直接去掉这个方法封装,直接在代码中调用File.Copy。

  5、.NET框架的发展,导致一些代码变成多余但又没有删掉,先将其注释

  动态构造SELECT语句的字段列时,在构造完成后,通常会给它们加上逗号

SELECT  ITEM_NO ,ITEM_GROUP FROM GBITEM

  通常我们会用ArrayList或是IList<string> 把ITEM_NO和ITEM_GROUP聚集在一起,再循环一次,给每个字符的末尾增加一个逗号,最后去掉多余的逗号:

public static string ArrayToList(string[] ids, string separativeSign)
    {
        int num = 0;
        string str = string.Empty;
        foreach (string str2 in ids)
        {
            num++;
            string str3 = str;
            str = str3 + separativeSign + str2 + separativeSign + ",";
        }
        if (num == 0)
        {
            return "";
        }
        return str.TrimEnd(new char[] { ',' });
    }

  MSDN中字符串类型string的Join方法,可以达到这个目的,只需要调用Join方法即可。MSDN中有例子解释如下

  如果 separator 为“,”且 value 的元素为“apple”、“orange”、“grape”和“pear”,则 Join(separator, value) 返回“apple, orange, grape, pear”。 如果 separator 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则改用空字符串 (Empty)。

  6、运行环境的改变,注释掉代码以便于以后发现问题

  请参考下面的方法例子

/// <summary>
        /// 获取一个文件的绝对路径(适用于WEB应用程序)
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>string</returns>
        public static string GetRealFile(string filePath)
        {
            string strResult = "";

            //strResult = ((file.IndexOf(@":\") > 0 || file.IndexOf(":/") > 0) ? file : System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + "/" + file));
            strResult = ((filePath.IndexOf(":\\") > 0) ?
                filePath :
                System.Web.HttpContext.Current.Server.MapPath(filePath));

            return strResult;
        }

  这个方法之前可能是用Web环境中,现在改成WinForms或是控制台项目中,导致被注释的代码会报错,于是将它注释。关于路径的选择,AppDomain的BaseDirectory或是Application.ExecutePath都是独立于运行环境的(ASP.NET,Console,WinForms,Windows Services),应该优先考虑使用。

  7、测试数据以注释的方式,保留中代码中,增加对代码的解释

 string host = System.Configuration.ConfigurationManager.AppSettings["EmailHost"];
 MailMessage m = new MailMessage();
m.Subject = subject;
m.SubjectEncoding = Encoding.UTF8;
m.From = new MailAddress(from);
m.To.Add(to);
m.Body = body;
m.BodyEncoding = Encoding.UTF8;
m.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = host; //"ASHKGEX4.asia.ad.flextronics.com";
client.Credentials = new System.Net.NetworkCredential("asia\baoshhli", "");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Send(m);

  如上面代码中的host=ASHKGEX4.asia.ad.flextronics.com,作者测试问题时是用这个host也没有报错,于是将这个数据保留在代码中,以方便以后代码维护人员测试问题。

  8、对每一个数据项都进行注释,必要的注释和不必要的注释混杂在一起

  例如下面的代码

DataTable Dt = new DataTable();
DataRow Dr;

Dt.Columns.Add("name");//名称
Dt.Columns.Add("type");//类型:1为文件夹,2为文件
Dt.Columns.Add("size");//文件大小,如果是文件夹则置空
Dt.Columns.Add("content_type");//文件MIME类型,如果是文件夹则置空
Dt.Columns.Add("createTime");//创建时间
Dt.Columns.Add("lastWriteTime");//最后修改时间

  Add方法后面的对字段的解释,有的是是多余的。有的是必要的。

  我以为,多余的注释是:名称,创建时间 ,最后修改时间  这三列的值可以通过代码或是它本身的名字得知。

  type这一列的注释,我以为这是很有必要的,这可以减少维护人员的工作量。

22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号