Crafting clean, readable code isn't optional. It's your responsibility as a programmer.
Here's a simple guide on how to do it:
Here's a simple guide on how to do it:
1. Functions should only do one thing
For example, it would be a bad design to have a single function that calculates a shopping cart's total, tax, and shipping costs.
Instead, this should be done as three separate functions.
This makes it easier to maintain, reuse, and debug.
For example, it would be a bad design to have a single function that calculates a shopping cart's total, tax, and shipping costs.
Instead, this should be done as three separate functions.
This makes it easier to maintain, reuse, and debug.
2. Use meaningful and descriptive names
Self-explanatory variable and function names improve the code's readability.
E.g. β we should call a variable used to store a shopping cart's total amount "total_cost" rather than "x" as it clearly explains its purpose.
Self-explanatory variable and function names improve the code's readability.
E.g. β we should call a variable used to store a shopping cart's total amount "total_cost" rather than "x" as it clearly explains its purpose.
3. Don't use global variables
Global variables can introduce many problems, including unexpected side effects & difficult-to-track programming errors.
Let's say two functions share a global variable. If one function changes its value the other function may not work as expected.
Global variables can introduce many problems, including unexpected side effects & difficult-to-track programming errors.
Let's say two functions share a global variable. If one function changes its value the other function may not work as expected.
4. Regularly refactor
Codebases inevitably change over time, which can result in outdated, redundant, and sometimes messy code.
Keep the quality of the codebase high by reviewing and refactoring surrounding code when editing an area of the codebase.
Codebases inevitably change over time, which can result in outdated, redundant, and sometimes messy code.
Keep the quality of the codebase high by reviewing and refactoring surrounding code when editing an area of the codebase.
5. Don't use magic numbers & hard-coded values.
Consider these two lines of code:
99 * 3
vs
price * quantity
The 2nd line is easier to understand as it uses variables with descriptive names, making it self-explanatory
Use constants or variables instead of hard-coded values.
Consider these two lines of code:
99 * 3
vs
price * quantity
The 2nd line is easier to understand as it uses variables with descriptive names, making it self-explanatory
Use constants or variables instead of hard-coded values.
6. Build what you need now, not what you think you might need in the future.
This is the idea behind the YAGNI (You Ain't Gonna Need It) principle.
Simple, focused programs are more flexible and less complex.
This is the idea behind the YAGNI (You Ain't Gonna Need It) principle.
Simple, focused programs are more flexible and less complex.
7. Use comments to explain "why" not "what"
Clean code is self-explanatory, and therefore comments should not be used to explain what the code does.
Instead, comments should be used to provide additional context, such as why the code is designed in a certain way.
Clean code is self-explanatory, and therefore comments should not be used to explain what the code does.
Instead, comments should be used to provide additional context, such as why the code is designed in a certain way.
If you enjoyed this thread, don't forget to like, comment, and retweet the first tweet.
I simplify programming and getting into techπ‘
Follow @ChrisStaud for more threads like this.
I simplify programming and getting into techπ‘
Follow @ChrisStaud for more threads like this.
Loading suggestions...