4 Tips to Write Pretty Codes in 2019
Ever since I started to code, I always asked myself a question, do all developers write the same code to perform the same task? Why are some codes prettier than others?
It turns out that there are many tips to write pretty codes.
As I became more experienced with writing codes, I started to question the performance not only the beauty of the code. I figured that there is a direct relationship between prettiness and efficiency.
I, now, believe that pretty codes are more efficient. (to a certain extent)
Here are a few tips on how to write pretty yet efficient codes:
1. Use the switch statement instead of if-else.
Switch statements makes your code legible. Specially, when you use nested if-else blocks. Think when you have more cases. Plus, it works just fine.
2. Use shorter functions.
Try to stay away from writing unnecessary lines of code. Just like the above example, there is no need to define a vat variable, a total variable, and then return. While you can return the total within one statement.
3. Utilize data structures.
When you have a single record (say a person), why not keeping it organized? Use object (data structure) that keeps your assets aligned. You can access any element using the dot notation.
4. Use classes.
when you need multiple copies of the same object, class is your best friend. A class offers you many services that can shorten the time production time. Also, it can keep your code elegant.
Better yet, define the class in a separate file. Defining the class in a separate file is considered a good practise to keep your code clean and organized.
Mind the highlighted lines. INCLUDE your files appropriately.
Clean & pretty codes do not only increase efficiency, they also reduce the maintenance time. Also, it helps you to think faster and produce more.
These are 4 important tips. I hope you learned something new today.
If you have some tricks, please share them here.
If you find these hacks helpful, feel free to share this article.
Great article and very well written