User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
user:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 18:27] – [5. Globals] Igor Yefmovuser:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 18:55] – [7. Code comments] Igor Yefmov
Line 252: Line 252:
  
 ==== 6. Code blocks' nesting ==== ==== 6. Code blocks' nesting ====
 +Somewhat related to the general question of "how many LoC in a function is too many?" this one goes back to gut feeling that cramming all the code into a single function will somehow have positive optimization benefits overall. Let me be clear here: **it doesn't**. That perception is stemming from the fact that function calls in ''C'', while being very cheap, are not zero-overhead operations.
  
 +This perception completely ignores all the advances made in CPU manufacturing in the past 60 years or so((various speculative execution techniques: branch prediction, prefetching, pipelining, value prediction, code and data caching to name a few)).
 +
 +Just write the code in manageable (one-two page length) chunks, packaged as individual functions. And if there really **is** a concern for stack depth you always have the option of inlining those functions((a very often overlooked method to my surprise)).
 +
 +If you have a long-ass ''switch'' statement - make it into a function. If the branches of your ''if'' or ''case'' statements are large - make them into functions. If there's an overgrown block of code that is responsible for one clearly defined operation - make it into a function. If you find yourself writing an ''if'' inside a ''case'' or a similar situation - extract the code block into a function.
 +
 +And as a general rule: if you see a block of code that doesn't fit onto one screen (or your prefrontal cortex, for that matter) - make it into a function!
 +
 +Think of functions as text paragraphs: if you read some text that consists of one huge paragraph chances are you'll give up soon enough and look for some other source of information. One that is made for human consumption.
 +
 +=== Stack depth ===
 +Speaking of stack depth: partitioning your code into smaller functions may indeed //reduce// your stack requirement as you won't be needing to allocate every-single-variable-ever-used-in-any-branch in one chunk, but instead only use up as much stack as needed for each individual function.
 ==== 7. Code comments ==== ==== 7. Code comments ====
 +<code C>CyBool_t isUsbConnected = CyFalse; /* Whether USB connection is active. */
 +
 +int i = 1;   // set i to 1
 +
 +i++;         // increment i by 1</code>
  
 +The comments above are utterly useless. Not only they don't anything to what is already expressed in the code((which is bad enough on its own)), there's a high potential for these types of comments to become stale and ultimately very misleading.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information