Tips and Tricks – 8 Reserved Words to Avoid in C


RSS
Follow by Email
Facebook
Facebook
Google+
Google+
http://beningo.com/8-reserved-words-to-avoid-in-c/
YouTube
LinkedIn

Just because a feature is made available to a programmer in their language of choice doesn’t mean that they should use it! This is particularly true when it comes to developing embedded software using C. While C provides developers with a wide range of indispensible tools, there are 8 keywords built into the language that should either be completely avoided or used only as a last resort. Let’s briefly explore these reserved keywords and understand why we should minimize their use.

Digital Globe

Reserved Keyword #1 – auto

The auto keyword is a storage class specifier that tells the compiler the storage duration (scope) of the variable that is being defined. The only place that this keyword can be used is within a function to declare a variable that is created within the function and then destroyed when the function has completed. Since this happens by default in ANSI-C to local variables in functions the use of this keyword is, well, useless! The use of it will only confuse developers and should therefore be avoided.

Reserved Keyword #2 – break

The break keyword is most often used within the case statement of a switch block. This is a completely valid and best use of the break statement! Unfortunately break can also be used to exit loops prematurely. This can be a valid use of break but there is a danger if the loop becomes nested and complex. The issue becomes that the use of break could have unexpected consequences of breaking and not executing code that otherwise would have been expected to run. Basically what break is doing in this case is bypassing the original termination expression for the loop. For this reason it is recommended to only use break with switch/case statements and use other constructs within loops to get the desired behavior.

Reserved Keyword #3 – continue

The continue keyword has much of the same stigma that break and goto statements have in that it breaks up the flow of the code and can leave programmers confused. Confused programmers who are modifying code usually accidentally add bugs! A continue statement bypasses the normal execution of the loop by forcing execution control to go directly to the test condition of the loop. The only useful place to use continue is in the event that an error has occurred and the programmer wants to the statement sequence to restart.

Reserved Keyword #4 – extern

The extern keyword infamously requires very little introduction due to the fact that by default everything in C is pretty much implicitly extern! The problem with extern is that it makes everything global! This is just bad programming practice because it allows every function and variable to see everyone else which can not only cause reentrancy issues but allow unrelated functions to modify data unexpectedly. The use of extern should be minimized with variables be defined to have minimum scope and practices such as encapsulation being employed as much as possible.

Reserved Keyword #5 – goto

The goto keyword is an old time favorite of many electrical engineers turned software engineer. Back in the days of functional programming the main control construct was the goto keyword. This keyword allows program execution to jump to a specified label within the program. The problem with goto is that its use generally creates unreadable, spaghetti code! The use of functions and other flow control statements can make for a much better software implementation. While generally it is recommended to never, ever use goto, the only practical place that it may make sense is for use within an error handler within function scope. The jury is still out on it though!

Reserved Keyword #6 – inline

The inline keyword is actually pretty cool. You can add it in front of a function (provided you are using a C99 compiler) and instead of that function being called as a function in compiled code, the contents of the function are pasted in place! This means that instead of taking a time hit for having to call a function it is like the function code was written there! There are two main problems with this. The first is that it is up to the compiler to decide whether it will actually inline the code or not! Putting the keyword in front of a function doesn’t guarantee that the compiler will heed the developers’ recommendation.  Secondly, if the compiler does inline the function if the function is used heavily throughout the code then the size of the compiled code could grow out of control!

Reserved Keyword #7 – register

The register keyword is another storage class specifier that should be avoided by developers. This keyword is meant to suggest to the compiler that the object should be made available quickly through the use of one of the CPU registers. One problem with this keyword is that once gain it is up to the compiler whether it will comply with the suggestion or whether it will be stored on the stack with other automatic variables. Today compilers are extremely intelligent and know the best places to store objects and data so it is better to let the compiler decide than potentially once again confuse a developer who may be reading the code and make a bad assumption.

Reserved Keyword #8 – restrict

The restrict keyword is a type qualifier that is only applicable to pointers. It was introduced in C99 to allow for the compiler to perform optimizations on the code provided that the object being pointed to is only accessed through the restricted pointer. The use of restrict is not recommended as it may of unintended side effects.

 

These are just a few thoughts on these often-controversial keywords within the C language. Do you have any additional thoughts or examples?

RSS
Follow by Email
Facebook
Facebook
Google+
Google+
http://beningo.com/8-reserved-words-to-avoid-in-c/
YouTube
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *