The past week I managed to get my certificate from Freecodecamp.org about JavaScript Algorithms and Data Structures.
Actually, I got the certificate about a week prior I just wanted to finish the courses included.
And boy did those Intermediate Algorithm Problems kick my butt.
An example is their Smallest Common Multiple problem.
My code took:
- 77 lines of code
- 4 Functions
- 3 For loops
- 1 do-while loop
- and tons and tons of if statements.
Theirs took:
- 21 lines
- 1 function
- 3 for loops
- 2 if statements.
Now Both codes display the same result. And probably does the same thing but why is mine longer?
Well, they have a more optimized code. And because they are way smarter than me! 🤣🤣
So why do programmers want or need to optimize their code?
Well here's my take on it.
Faster Sites/ Applications
Shorter more optimized code usually tends to be faster than non-optimized code.
Why is that though?
See the difference in the lines of code?
The computer needs to read all of those lines.
And that takes time.
It's the same way that it's easier to read a short story vs. a novel.
All the loops included also takes time.
Although some loops tend to be faster in certain situations. The computer still needs to go through all of those loops.
I remember my first job and our first pet project. We created a POS using Java. Being fresh graduates, none of us optimized our code.
Now our code would probably run okay on a modern computer. But 10 years ago? On a low-powered computer?
It literally took 5-10 mins just to generate a simple report.
It probably took a minute before it populated a table.
So from experience, a shorter and more optimized solution is better for a faster application.
Better Code Readability
From the example above, it took me 4 functions for my solution.
Now that's okay since it's my program and I'm the only one working on it.
Imagine if you're working with someone else. They have to read and understand those functions before they can modify them.
Fewer lines and fewer functions can be easier to read right?
Although the shortcuts like ternary conditionals and ES6 code can be harder to read especially to those who are just beginners.
But in general, as I've said before It's easier to read a short story vs a novel.
More Straightforward Debugging
It's the same principle as with the previous point.
The less code there is the less code to debug.
Can you imagine how many Console.log()s I have on my code? (Yes there's a lot.)
But yeah, debugging would be easier if you have less code and functions to go through.
Easier Code Maintenance
And lastly of course, as with the last 2 points.
It's easier to maintain if it's optimized.
Since there are fewer lines, variables, loops, and functions in general.
If you have to change something, like variable names, it would be easier.
CONCLUSION
So there are my reasons why we need to optimize your code.
But do you have to optimize your code?
Of course not!
Unless your program is running really slow then I guess you do. But if not?
Optimization can be a hindrance if prioritized.
Make sure you finish your work first, then optimize. As my professor once said,
"A pretty code is worthless if you did not manage to finish it."
Thank you for reading! I hope you learned something!
If you have any tips on how to optimize code please comment them down below! I would really appreciate it! Thank you! 😊
If you want to reach out, you can contact me via Twitter