A beautiful yard

  And have a good readability of the code is to expand the project in medicine, it is not only looked comfortable, it is also easy to change, and even reuse, clear logic modules. "See code knowledge skills", and to achieve the kind of concise master of the realm, requires a lot of practice and summary, today we have to talk about how to write beautiful code.

  

name

  Good naming should have the following characteristics:

  1, meaning correct . This is a basic requirement, not trickery, lied to know at a glance what that means. Even at first glance does not come out, copied to translate what way dictionary also know what it means;

  2, singular and plural clear . If an array is either added to the end of the s / es it indicates that it is a complex, or added to list is represented in an array. Such as cars, carList can express a list of vehicles;

  3, caution acronyms . Acronyms can make our name more concise, but a! set! To! Yes! industry! boundary! through! use! Shrink! slightly! Word ! For example, info intended for information, msg intended for the message, fn intent function, conf config intent and so on, these acronyms are the industry's tradition, and we all know what that means, do not remember her own making acronyms;

  4, have specific meanings . According to business scenarios to name, rather than named according to the abstract; for example getUnreadMsgList, to see that this is meant to obtain a list of unread messages, and getData did not say that, like say, the lack of specific meaning.

 

Note

  Expressive code does not need comments. For example, an init function, to see that is used to do some initialization work, no need to write extra comments to explain.

  But there are some scenarios comment is very necessary, several scenes you want to add the following comments:

  1, some special logic for a particular business scenarios and custom . For example, when we update personal information, due to the back-end problem, we need to pass a little information such as date of birth, or to be more pass after a time stamp to use for other business updates to Avatar. These additions and deletions property inexplicable, if not comment, will lead to follow-up their own can not understand;

  2, Code hazards may occur . Because of its limited level, or itself technically can not be achieved only through a number of effects to imitate some of the special skills, often there are security risks, such as: user operating too fast can go wrong, will go wrong network is too slow, a this page will adjust the interface nowhere to hang the whole, temporarily unable to solve the bug, and so some special action will cause a scene, we need explanatory notes;

  3, related to certain advanced or rare technical knowledge . This comment also, to remind yourself and other developers.

  In general, the comments are basically in the expression, " Here I am Why do ", there is little will to express comments " I'm a what stuff ," If it is the latter comment, only that name did not do a good job.

  

function

  Function code is the soul, the write logic is a vector, the following requirements are determined a developer to write a function really good standard.

  1, it is not a single responsibility . A function should do one thing, and it should pass a function name can be clearly demonstrated. This is a very nice feature, a function of a spicy chicken may be several hundred lines of code, various logic piled together, see people feel dizzy mind, even the developers themselves are not clear rationale; determine whether this function is not a single responsibility the technique is simple: to see if it can no longer split.

  2, there is no hierarchy of points . Between function and function is the status of the points , there are high-level function responsible for the overall big picture, but also focus on the details of the wage earners low-level functions. If you do not establish a hierarchy, it is easy to get lost in the ocean in detail. such as:

  

 

 

  For a meal of this function, he does not need to be concerned about such details, such as repeated selection when shopping, it only needs to know, about three big steps on the line. So this logic will be split according to their position as a function of different levels, assuming no sub-levels, the first step that has been written from the "take the bus" last step "trash", which will become very difficult thing maintain.

 

  3, the scene carried by whether simple enough . Sometimes we encounter such a scenario: a function that will be used in many places, but in different parts of the incoming parameters are not the same, so we have to function versatility, it is for the reference made to identify a variety of scenarios, resulting in very much the parameters, which also need to make fine adjustments depending on the logical scenarios. So just take the incoming parameters are enough to choke up, a bunch of if else or switch case.

  This function is too difficult, and this has violated the principle of single responsibility function, it made a judgment in which a variety of scenarios, which show different behaviors, but these behaviors are not completely different, but "much the same" If you override would seem to increase the number of repetitive code. The solution is to do a smaller size of the split, those portions of the real business decoupling pulled out, and different scenarios corresponding to different handlers, the business has just pulled decoupling function just as common functions of these different scenarios section!

 

Guess you like

Origin www.cnblogs.com/zhangnan35/p/11872729.html