DEV Community

Discussion on: What is your advice on writing "clean code" ?

Collapse
 
ekeijl profile image
Edwin • Edited
  • Base rules: a function should do only one thing, use descriptive variable names that make sense, document your code (explain WHY the code is there, not WHAT it does).
  • Write your code so it can easily be expanded and refactored. Loosely coupled modules come to mind. You could use a boolean for a function parameter, but maybe a string/enum might be better if you foresee a binary parameter won't be enough in the near future.
  • Don't try to be "smart" by overengineering the solution. This often leads to pointless abstractions for edge cases that may never happen. Premature optimization is a cardinal sin. "You Aren't Gonna Need It" (YAGNI)
  • The bigger the project/feature you're building, the more upfront design you should do. If you intend to maintain the project for a very long time, spending some time on architecture design is worth it. If it is just a quick proof-of-concept kinda thing, why bother?
  • An app that is never shipped does not exist. You can spend ages writing beautiful code, but your users won't care how the app looks on the inside. I don't mean you should rush writing code and take shortcuts all the time. At some point your code is just "good enough".

Sometimes you create a suboptimal implementation and decide to take on the technical debt (probably because of a deadline), while the software works as intended. If you don't intend on expanding the software, why would you touch the code again? Maybe you can leave it as is. Sometimes, a change in requirements can make the tech debt go away and the problem basically solved itself!