When programming it is important to keep in mind that your code will, most likely, be maintained by others in the future. We should always aim to write clean code – code that other developers can easily read and understand. Failing to write clean code will increase technical debt.
Technical debt is the term for future costs caused by choosing an easy short-term solution instead of using a better approach that would take longer. Taking short cuts to finish a viable product earlier and deliver business value is generally not a bad decision. But we must strive to keep the technical debt at a low level since it must be repaid sooner or later.
This document contains Spot On's suggestions to help facilitate writing clean code and to avoid technical debt. Your current and future co-workers will love you!
Always leave the code cleaner than you found it!
If we don't do this technical debt will grow and in the long run be impossible to handle.
2. Write readable code
Use meaningful names on classes, variables and functions.
Let the name explain whether a function answers something, returns something or does something.
Readable code will save you and your colleagues a lot of time.
3. Implement single responsibility
Classes, variables and functions should only do one thing
This helps you organizing the code and makes it much easier to test and maintain.
4. Keep it simple
Small functions, around 20 lines of code or less
Few input arguments to a function, strive to use less than 5
Small files, around 500 lines or less
Short rows, around 120 characters or less
5. Don’t repeat yourself
If you have duplicated code make a function out of it
If two classes have properties in common let them inherit these from a base class
Writing less code gives you less code to maintain.
6. Avoid comments
Strive to write self-documenting code, use function names and informative variables instead of explaining the code with comments
Don’t comment code that will not be used, remove it
If you're thinking about writing a comment, the code is probably bad and should be refactored.
7. Use a consistent code style
Uppercase/lowercase (best practice depends on programming language)
Use spaces between operators, parameters, and commas
Concentrate expressions that belong together and separate those that do not
Always use curly brackets for if-statements
8. Implement error handling
Make sure you catch and take care of thrown errors
Log thrown errors to be able to find the cause in production environment