问题
Aside from language-specific constructs, for someone who is programming in PHP, javascript, java, and python, what are the top 10 clean coding must-do's? I would like to keep my code as clean as possible and as readable as possible. For example, what's the take on placing the function body opening bracket on the same line as the function declaration vs a new line? Also, what's the take on spacing between things for example (x==10) vs ( x == 10 )? Any helpful hints for clean coding will be appreciated!
回答1:
Here's a few that may help:
- Give intuitive names to variables and methods
- Use your specific language coding style (e.g. in Java vs C++ you use different style on brackets; choose according to specific language conventions)
- Always put comment on what/why something is happening.
- But don't pollute code with comments.
- Try to integrate design patterns if you can. It helps in reusability and maintainance
- Don't put everything in one method. Each method should do one thing and do it really well (this also makes the method easier to read/understand/test/debug)
- Don't put hard-coded strings in your code
- Separate application data and application code. I.e. try to not hard code configuration of your code.
- Don't try to be more clever than the compiler. Write code that other people can understand it as well and let the compiler do any optimizations
- Write code in a way that shows your intentions. Write code keeping in the back of your head that in 7 months you may need to debug it, or someone else might need to fix/enhance it. It is impossible to remember why you did something after 7 months (and comments are good but sometimes not good enough). Let alone the poor guy trying to fix your code
I am sure there are a lot more but I believe these can be useful in any language
回答2:
Reference:
http://misko.hevery.com/code-reviewers-guide/
http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf
And check the links from this response:
https://stackoverflow.com/a/10359288/1268570
Watch the clean code talks from Misko Hevery (given to the Google people)
http://www.youtube.com/watch?v=wEhu57pih5w&feature=player_embedded
http://www.youtube.com/watch?v=RlfLCWKxHJ0&feature=player_embedded
http://www.youtube.com/watch?v=-FRm3VPhseI&feature=player_embedded
http://www.youtube.com/watch?v=4F72VULWFvc&feature=player_embedded
Theses are warning signs that you should avoid: (From Misko Hevery)
- new keyword in a constructor or at field declaration
- Static method calls in a constructor or at field declaration
- Anything more than field assignment in constructors
- Object not fully initialized after the constructor finishes (watch out for initialize methods)
- Control flow (conditional or looping logic) in a constructor
- Code does complex object graph construction inside a constructor rather than using a factory or builder
- Adding or using an initialization block
- Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
- Adding or using singletons
- Adding or using static fields or static methods
- Adding or using static initialization blocks
- Adding or using service locators
This is a great book about writing clean code:
http://books.google.com.mx/books/about/Clean_Code.html?id=dwSfGQAACAAJ&redir_esc=y
回答3:
The answer in my opinion is that there is no real answer. A good rule is to stick to a convention, which usually is a reasonable convention that the community is already familiar with. I would rather suggest you a couple of books to have an idea.
For Java/C++/C#/VisualBasic you should read
Code Complete: a practical handbook of software construction
On the Java-heavy side, you would get useful insight from
Clean Code: a handbook of agile software craftmanship
Consider that the concepts in those two books are of general validity and should not be bound to a specific language.
来源:https://stackoverflow.com/questions/10365009/what-are-some-universal-clean-coding-conventions