DEV Community

Discussion on: Open Discussion: What Tips Do You Have For Writing Clean Code?

Collapse
 
gustavofsantos profile image
Gustavo Santos

About the article, I feel that is more personal taste than some pragmatic thing. There is a big difference between opinion (which everyone has) and well-known metrics such as SLOC, Cyclomatic Complexity and so on.

As tips, I suggest reading those books:

As a general rules of thumb that I like to follow:

  • Follow Tell, Don't Ask when possible, but not become too dogmatic about that
  • Law of Demeter is not a law, but it helps drive the code to a good and maintainable future
  • Start writing acceptance (e2e) tests, then write unit/integration tests, then write the code. This helps a lot organizing ideas and communication between the parts, without writing all the base classes upfront.
  • ReadMe driven development help organize what to do first and this impact directly to the code organization
  • Know your tools, this help you to use your tools to write your code.
  • Use dependency injection, even if it needs to be done in a manual way.
  • Write code for humans, not for machine.
  • (questionable taste) Switch/case isn't evil. Use it in factories to help to use polymorphism
  • (questionable taste) Do not put repeated prefixes or suffixes, like "UseCase", "I", "Service", "Entity" and so on (unless if needed to work, like the prefix use in React Hooks). Each object should tell a story, each object should deserve its place in the source code by the story that it tells, not by its name.