当前位置:网站首页>What is a good programming habit?

What is a good programming habit?

2020-11-10 17:25:21 xxxxxx

   Programmers are required to have good programming habits whether in recruitment or in leadership ,“ I know everything , But how to do it ?” The following can take a seat to see their own programming habits are not “ good ” Of .

   One 、 Code low coupling

   Low coupling is a characteristic of well structured programs , Readability of low coupling programs 、 Maintainability 、 Both reusability and extensibility are good , And tightly coupled modules or systems are too tight , So that when you modify an object , Calls to each other may occur . If two objects are too tightly coupled , Modifying code can be a nightmare , And it's easier to introduce bug.

   Two 、 Avoid God's object

  bondObject A large module or a class , It contains too many variables and functions . For two reasons ,“ I know too much ” and “ Do too much ” Will lead to some problems . First of all , Other classes or modules will become overly dependent on data ( Tightly coupled ). second , Because all the code is in one place , So the whole structure is chaotic . Compared with the object of God , It might be better to break it down into many small objects .

   3、 ... and 、 Reject long functions

   Just like its name , A long function means that the function is too long . Although for a function , No number represents how many lines of code “ Too long ”, But when you see this function , You'll know if it's too long . Long functions mean too many functional implementations . In general, a long function should be decomposed into several sub functions , Each of these sub functions can be used for a single task or problem . Theoretically , The original long function becomes a list of child function calls , This will make the code clearer , Easier to read .

  4. Meaningful identifier naming

   Variable names have one or two letters , The function name has no obvious meaning , The class name is over decorated , Variable names are marked with variable types ( for example ,b_isCounted representative Boolean Variable ), Or mix different naming rules in a piece of code , All of this makes the code hard to read , Difficult to understand , Difficult to maintain . Generally speaking , Variable names should be short , But descriptive . In general , The function name should contain at least one verb , And the function name should show the function of the function , But don't use too many words , Such as class name .

  5. Eliminate hallucinations

   When browsing code written by others , You'll find that some of them are hard coded numbers . They could be if Part of the statement , Or some incomprehensible calculation , It doesn't seem to make any sense , When you need to modify a module , But I can't understand the meaning of numbers , It's very annoying . therefore , In programming , We must avoid these so-called... At all costs “ Wrong number ”. Hard coded numbers have a certain meaning in the writing process , But they soon lose their meaning , Especially when others try to maintain your code . One solution is to leave notes on the numbers , But a better way is to convert magic numbers into constant variables ( Used to calculate ) Or enumerate ( be used for if Statement and switch sentence ). The readability of the code is achieved by giving the magic number a name , And it's not easy to make mistakes .

  6. Avoid deep nesting

   Deep nested code is not always bad , But there may be problems , Because it's hard to understand , Especially when variables are not named correctly , Even more so . If you find yourself writing a double 、 Triple or even quadruple for loop , Then the code may try to find data beyond your own capabilities . then , You should provide a way , Let the object or module function call that contains the data be able to request the data . And deeper nesting if Statement means that you are trying to process too many blocks of logical code in a function or class . actually , Deep nesting and long functions often occur at the same time . If you have a lot of switch Statement or nested if-then-else sentence , You may need to implement status or policy Pattern .

  7. Concise code

   You execute the same block of logical code in multiple separate parts of the program , Then we find that we need to modify the logical code block , But don't remember all the places where it was executed , Suppose you end up modifying only 5 A place , And actually it needs to be changed 8 A block of code in places , This will lead to wrong results . It is usually a good habit to convert to a function , So if you need to modify this logical block , Just modify this method , And then apply it to all the places that call it .

   8、 ... and 、 code annotation

   The code is not annotated everywhere . There is no need to annotate the function , There is no need to outline classes , No need to explain the algorithm, etc . It can be said that , Well written code doesn't need comments , But actually , Even the best code doesn't have comments that are easy to understand . When you write comments , please remember , Your goal is to explain why code blocks exist , Instead of explaining what it's doing . Annotations can help you better understand your own and other people's code , And work less , So don't ignore them . Zhengzhou see infertility hospital which good :http://jbk.39.net/yiyuanzaixian/zztjyy/ Zhengzhou male gynecology hospital which good :http://news.39.net/ylzx/zztjyy/ How much does Zhengzhou do painless flow of people :http://jbk.39.net/yiyuanzaixian/sysdfkyy/

版权声明
本文为[xxxxxx]所创,转载请带上原文链接,感谢