您如何编写代码? [英] how do you write your code?

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

问题描述

为什么有些人会这样写:


Why do some people write this:


public class NotifyObject
   {
       public NotifyObject(string message, string title)
       {
           this.message = message;
           this.title = title;
       }

       private string title;
       public string Title
       {
           get { return this.title; }
           set { this.title = value; }
       }

       private string message;
       public string Message
       {
           get { return this.message; }
           set { this.message = value; }
       }

       private int forbedringsnr;
       public int forberingsNr
       {
           get { return this.forbedringsnr; }
           set { this.forbedringsnr = value; }
       }
   }



代替这个:



instead of this:

public class NotifyObject2
    {
        public NotifyObject2(string message, string title, int forbedringsnr)
        {
            this.message = message;
            this.title = title;
            this.forbedringsnr = forbedringsnr;
        }
        public string message;
        public string title;
        public int forbedringsnr;
    }



它只是在行数中得分,并弄乱了我认为的代码.



It just makes a score in the linecount, and clutters up the code i think

推荐答案

为什么不这样写代码:

Why not write your code like this:

public class NotifyObject3
    {
        public NotifyObject3(string message, string title)
        {
            this.Message = message;
            this.Title = title;
        }

        public string Title {get; set;}

        public string Message{get; set;}

    }



通常最好使用属性而不是公共字段,这样,如果您需要在setter周围添加验证或任何其他代码,则不必从字段更改为属性,从而不会破坏与先前版本的二进制兼容性.



Its generally better to use properties instead of public fields so that if you need to add validation or any other code around the setter you won''t break binary compatibility with the previous version by having to change from a field to a property.


在.Net中,C#和VB,您提供的两种形式以及BC @ CV在解决方案1中提供的两种形式都编译为完全相同的中间代码:您最终得到了一个私有分配用于实现getset属性调用的值和包装.

在这三种技术中,我更喜欢您提供的第一个.由于框架无论如何都会创建私有变量声明,因此显式声明它们使我可以对其进行一些控制.因为我进行了变量声明,所以可以直接在类内部使用变量,以提高性能:使用第二种方法或记录的BC @ CV需要始终通过包装器.

如果我想在以后添加验证或其他处理,则可以轻松地做到这一点,而不必重写代码.例如,如果属性是List,则可以检查对象是否在get中实例化,如果不是,则创建该对象.这样,该属性将永远不会返回null值.也许set上需要针对选项列表进行验证,并确定该值是否在范围内.对于文本属性,在存储值之前,我通常会调用Trim摆脱多余的空格.这样的事情.

我坚持使用提供最大好处的一种方法,即完全明确的方法,而不是使用声明属性的不同方法.几个月或几年之后,当我回到代码中时,这使我很容易找到东西.
In .Net, both C# and VB, the two forms you give and the one offered by BC @ CV in Solution 1 all compile to exactly the same intermediate code: you end up with a private allocation for the value and wrappers implementing the get and set property calls.

Of those three techniques, I prefer the first you you gave. Since the Framework will create the private variable declarations anyway, explicitly declaring them allows me to have some control over them. Because I made the variable declarations, I can squeeze out a bit of performance by using the variable directly, internal to the class: using your second method, or the one BC @ CV noted, requires always going through the wrappers.

If I wanted to add validation or additional processing later, I can do that easily without having to rewrite the code. If a property is a List, for example, I can check to see if the object is instantiated in the get and created if not; that way, the property will never, ever return a null value. Or perhaps on the set needs to validate against a list of options and determine if the value is in range or not. With text properties, I will usually call Trim to get rid of extraneous white space before storing the value. That sort of thing.

Rather than have different ways of declaring properties, I stick with the one technique that offers the most benefit, which is the fully explicit one. That makes it easy for me to find stuff when I come back to the code months or years later.


这篇关于您如何编写代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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