Don_t_Do_It_CodingNew_Promo.jpg

Don’t Do It: Coding Tricks

Aug. 22, 2019
It may seem like a good idea to use coding tricks in embedded systems, but you really shouldn’t go there.

I remember doing all sorts of coding tricks when writing in assembler and the early days of C when C compilers were little more than glorified assemblers. This is when I would use an XOR A instead of MOV A,0. The former is a single byte instruction while the latter is two bytes. I eventually wrote a massive LET macro that handled equations and generated the most efficient code. LET A = 0 generated the XOR A.

Finally getting my hands on a decent compiler let me ignore this type of programming approach. That’s because, in theory, the compiler should be able to generate the optimum code based on the setting selected by the developer, which can include settings to choose size or speed, etc. 

Every programming language and compiler has idiosyncrasies, but for the most part, the best practice for programmers is to make the code clear and to implement algorithms in an understandable fashion. Documentation and comments should not be an afterthought. It still surprises me, though, how often this isn’t the case regardless of the programming language used. And for those who say their code is the documentation, you’re wrong.

This is why I get a bit concerned when reading articles like “10 simple tricks to optimize your C code in small embedded systems.” It reminds me of when I did dumb things like this because in the long run, the cute optimizations warp the algorithm being implemented. This approach can actually make it harder for an optimizing compiler to do its job, potentially inducing bugs that are hard to track down.

Trick #2 in the article reduces the code size by four bytes (5%) by changing a CASE statement into a series of IF statements. The problem is that the example doesn’t use an ELSE clause and essentially trades off speed for size—this only works because the expressions involved don’t modify the conditions involved.

The problem with these kinds of tricks is that if they’re used, they must be very well documented because they always make assumptions that usually aren’t apparent from reading the code. They also tend to be very brittle; minor changes can mess up the algorithm, which often creates bugs.

The smart move in doing embedded C programming actually goes in the other direction, utilizing tools such as Motor Industry Software Reliability Association (MISRA) C. “MISRA C Isn’t Just for Automotive Applications” is an article I wrote a while back to address the myth the MISRA C compliance was only something automotive applications and systems programmers had to be concerned about. The rules involved not only prevent the use of many “features” of the C language, but most of the “tricks” developers often pick up along the way.

One upshot of using static-analysis tools like MISRA C and standard coding rules is that errors are reduced. This includes security-related errors that allow a system to be compromised. Null pointer and buffer overflow errors are less common when following these best practices and using these tools all the time.

It’s much more important to program an embedded system correctly than to squeeze every last byte of the code. This may have been true for very small PICs and 8051 microcontrollers, but these are rare these days. It still pays to use a good compiler and to evaluate your algorithms. However, it’s always a bad idea to use “tricks,” especially without very, very, very good documentation and lots of comments.

If you really want to do it right, you can take a look at Ada contracts. This is a way to make sure that the code is doing what you intend because it’s stated programmatically. There’s nothing comparable to C or C++, but that’s another story.

About the Author

William G. Wong | Senior Content Director - Electronic Design and Microwaves & RF

I am Editor of Electronic Design focusing on embedded, software, and systems. As Senior Content Director, I also manage Microwaves & RF and I work with a great team of editors to provide engineers, programmers, developers and technical managers with interesting and useful articles and videos on a regular basis. Check out our free newsletters to see the latest content.

You can send press releases for new products for possible coverage on the website. I am also interested in receiving contributed articles for publishing on our website. Use our template and send to me along with a signed release form. 

Check out my blog, AltEmbedded on Electronic Design, as well as his latest articles on this site that are listed below. 

You can visit my social media via these links:

I earned a Bachelor of Electrical Engineering at the Georgia Institute of Technology and a Masters in Computer Science from Rutgers University. I still do a bit of programming using everything from C and C++ to Rust and Ada/SPARK. I do a bit of PHP programming for Drupal websites. I have posted a few Drupal modules.  

I still get a hand on software and electronic hardware. Some of this can be found on our Kit Close-Up video series. You can also see me on many of our TechXchange Talk videos. I am interested in a range of projects from robotics to artificial intelligence. 

Sponsored Recommendations

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!