Chapter II of the Clean Code notes

 

Are you really will be named

 Foreword

This is what I read the book a second time (Clean Code) when, for the first time to see when is see a world five hundred in their code, I did not see one comment, I still remember scene at that time, when I Download Dir a code, I would like to know the company code coding habits as well as a small business in my previous perception, the code in these notes helped with a lot of comments I understand the business, but in this code, I actually do not see a comment to that was my reaction is that there will be a certain document, I sought help from the person in charge of the project, who knows more than he retorted you understand what name on the line, ( in fact, my English is very poor, and later after getting used to find a good name must be a huge help ).

Personal understanding

In the "Domain Driven," a book there is such a term "common language" His main purpose is to use a common language to be a thing named after communication can reduce the error between the waste of time and information. Thereby reducing communication costs.

       "Common language" in the code we can be seen as "naming" "This is the way of communication between the Code and the code between programmers and programmers.

       In a good code variables, function names, classes can already tell all our big problem. If your code needs to add a comment to tell people important issue, then I think you can think about your name reasonableness of.

Of course, the code name is not static, as your business has changed, your name may also need to change, if the change does not result in the consequences of missing information will eventually lead to code difficult to maintain.

 Examples

The following shows a top view of the two codes with

        // user selected type of user is the user Vip

        public List<string> getUsersName() 

        {

            List<string> list = new List<string>();

            foreach (var item in UserList)

            {

                if (item.Type = "Vip")

                    list.Add(item.Name);

            }

            return list;

    }

The code above I have also written many times, ultimately express my needs, but now there is such a requirement, the code do not appear or that Chinese demand needed to "find users older than 100 years old user name"

This time most people are accustomed to changes in conditions "item.Type =" 100 "" comments are very likely to be ignored this time a new developer took over the very misleading, because he could not look at it by naming this code, because naming is not standardized, resulting in him must come in view tracking code, and also caused a misunderstanding.

Talk about more errors in the code:

    getUsersName: Italy can not see the name recognition

    List <string> list: a collection of content can not tell

    UserList: misnamed because there are not suitable for use to List Description This is a collection Users should use the list because it has a special meaning for the programmer. Unless the content is really a type List

    item.Type: I do not know the specific meaning

    return list; can not tell me how to use the return value

The following sample code is correct: for reference herein user.Type = "VIP" only to demonstrate

public List<string> getVipUserNameByUserType()

        {

            List<string> vipUsersName = new List<string>();

            foreach (var user in Users)

            {

                if (user.Type = "VIP")

                    vipUsersName.Add(user.Name);

            }

            return vipUsersName;

    }

 

It is proposed that we still read through cleanCode

to sum up

 

Guess you like

Origin www.cnblogs.com/szlblog/p/11908863.html