In this post, I will show you how to optimize your code with examples. Optimization for speed, readability and scalability. Almost all code snippets will be written in Go programming languages, but still .. all those tips are applicable in other programming languages like PHP, Javascript, Java, Kotlin, .. etc.
read this post on my blog!
Calculate it once
calculating things again and again is horrible. It consumes CPU time more than actually needed. So we should stop re-calculating things.
When we write a for loop, we compare the iterator against a number .. usually that number is an array length. Why we re-calculate the array length before each iteration/loop 🤨 we should calculate it ONCE , then re-use it in a variable.
The image shows the left code (non-optimized) and the right code (optimized) written in Go. This is applicable in all programming languages that have a for loop.
No else in conditions
use the execution flow instead of if-else to simplify multiple checks in your code.
N O T E, this post is not complete yet because I will add more tips for code optimization weekly on my tech blog —God Willing.