Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.
  2.  
  3. NOTE: Edited the original to remove a few points that are mostly Java-specific.
  4. _____________________________________
  5.  
  6. ## General rules
  7. 1. Follow standard conventions.
  8. 2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  9. 3. Boy scout rule. Leave the campground cleaner than you found it.
  10. 4. Always find root cause. Always look for the root cause of a problem.
  11.  
  12. ## Design rules
  13. 1. Keep configurable data at high levels.
  14. 2. Prefer polymorphism to if/else or switch/case.
  15. 3. Prevent over-configurability.
  16. 4. Use dependency injection.
  17. 5. Follow Law of Demeter. A class should know only its direct dependencies.
  18.  
  19. ## Understandability tips
  20. 1. Be consistent. If you do something a certain way, do all similar things in the same way.
  21. 2. Use explanatory variables.
  22. 3. Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
  23. 4. Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.
  24. 5. Avoid negative conditionals.
  25.  
  26. ## Names rules
  27. 1. Choose descriptive and unambiguous names.
  28. 2. Make meaningful distinction.
  29. 3. Use pronounceable names.
  30. 4. Use searchable names.
  31. 5. Replace magic numbers with named constants.
  32. 6. Avoid encodings. Don't append prefixes or type information.
  33.  
  34. ## Functions rules
  35. 1. Small.
  36. 2. Do one thing.
  37. 3. Use descriptive names.
  38. 4. Prefer fewer arguments.
  39. 5. Have no side effects.
  40. 6. Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.
  41.  
  42. ## Comments rules
  43. 1. Always try to explain yourself in code.
  44. 2. Don't be redundant.
  45. 3. Don't add obvious noise.
  46. 4. Don't use closing brace comments.
  47. 5. Don't comment out code. Just remove.
  48. 6. Use as explanation of intent.
  49. 7. Use as clarification of code.
  50. 8. Use as warning of consequences.
  51.  
  52. ## Source code structure
  53. 1. Separate concepts vertically.
  54. 2. Related code should appear vertically dense.
  55. 3. Declare variables close to their usage.
  56. 4. Dependent functions should be close.
  57. 5. Similar functions should be close.
  58. 6. Place functions in the downward direction.
  59. 7. Keep lines short.
  60. 8. Don't use horizontal alignment.
  61. 9. Use white space to associate related things and disassociate weakly related.
  62. 10. Don't break indentation.
  63.  
  64. ## Objects and data structures
  65. 1. Hide internal structure.
  66. 2. Prefer data structures.
  67. 3. Avoid hybrids structures (half object and half data).
  68. 4. Should be small.
  69. 5. Do one thing.
  70. 6. Small number of instance variables.
  71. 7. Base class should know nothing about their derivatives.
  72. 8. Better to have many functions than to pass some code into a function to select a behavior.
  73. 9. Prefer non-static methods to static methods.
  74.  
  75. ## Tests
  76. 1. One assert per test.
  77. 2. Readable.
  78. 3. Fast.
  79. 4. Independent.
  80. 5. Repeatable.
  81.  
  82. ## Code smells
  83. 1. Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.
  84. 2. Fragility. The software breaks in many places due to a single change.
  85. 3. Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.
  86. 4. Needless complexity.
  87. 5. Needless repetition.
  88. 6. Opacity. The code is hard to understand.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement