Applying “All codes are guilty until proven innocent” in your code.
A way to clean code.
Have you heard this quote before?
All codes are guilty until proven innocent.
probably not.
How does it work in real world?
Here are some examples:
Let’s say we want to create a new student in the DB.
Here in the example we use the if.. else… statement, this is perfectly fine and will work as intended, but have you noticed we have 2 nested {} (braces) function { if {} else {}}?, as your code grows it will be not easy to read your code anymore.
How about we use return statement:
Wow! This feels like more roomier, but still we are not solving the main issue here nesting our main logic inside multiple {} braces, so what if we could do the other way around? hmm…
Super! More space and feel more tidier and easy to read, you can get away of unnecessary additional nested braces of your main logic code compare to example code 1 above.
This helps eliminate potential distractions and easy to debug 3–6 months from now. There are many ways to improve writing your code but definitely this is one of them.
This is not an absolute rule that you should follow in your whole application, apply it when you feel necessary.
Trust your gut!
Hope this article helps to make your code like an art and great in the eyes :).
What do you think? Does this helps?