Why is Simple Code Important?
Modular MX
Embedded Systems Nearshoring from Mexico: From Automotive to IoT, We've Got You Covered
Sometimes, we don’t usually?think about making our code as simple as possible. We just follow our thoughts and write whatever comes to mind, without realizing that this can lead to unnecessary complexity. For example, you might end up with +100 lines of code when you could achieve the same in just 25 lines!?
For instance, the following code uses Switch cases to select the pin it would be configured.
Take a look at how repetitive port settings can be. Instead of writing the same configuration multiple times, why not use a pointer array? This approach makes the code cleaner, more efficient, and easier to manage.
Now, I know pointers can seem a bit tricky at first, but once you get the hang of them, they help keep your code organized and reduce redundancy. Plus, they make it easier to scale your code without rewriting the same logic over and over.
Now, look how beautiful this code is! It’s simpler, cleaner, and way more understandable. Plus, if we want to make it even better, we can replace those magic numbers with macros—that way, it’s crystal clear what each part of the code is doing.
Writing clean and simple code takes experience. You learn by failing during unit tests, struggling with debugging, and constantly looking for better ways to improve your code.?
领英推荐
But here’s the key: code should be easy to read, almost like a book. Ideally, even someone with little to no programming knowledge should be able to get a basic idea of what your code is doing. Also, making your code complex does not make you a better embedded engineer than someone else, so make your life easier for the rest of us.?
Why does this matter??
At the end of the day, simple code means fewer headaches—for you and for anyone else who has to read or maintain your work. So why make things harder than they need to be??
If you enjoyed this article, don’t forget to follow us and stay tuned to our website for more content like this! https://modular-mx.com/
This article was written by Edwin Bolon