Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

CProgramTrends
FlashNewz
DevWebPro








C Style Coding

By Bryan Young
Expert Author
Article Date: 2010-07-01

My very first computer science teacher in college taught me C++. Starting with "Hello World" and going through structs, classes, and pointers, she showed me how to make computers dance with a few simple commands. Imagine my surprise when I turned in a program, only to find I had lost points for using the conditional operator.

When I asked her about it, she told me that I shouldn't use C style code, and it was a bad habit. I got the same speech for using a switch in another assignment, and again for using continue to cycle a loop.

Now, out of college and with more free time to ponder such things, I have come to a conclusion for the reason she disliked this so called C style of coding. When using a switch or continue, or in a lesser case, the conditional statement, you are basically issuing a goto command, which can easily go awry. The goto command is fairly controversial in that it can be misused easily, and can almost always be avoided by other means.

For instance, I mentioned above my use of the continue statement, which causes a loop to stop executing and return to the beginning of the block to start again. This behavior could have been achieved using an if statement, but I thought continue was a more elegant solution. This could be argued either way. Yes, the if statement works, but it also adds another layer of braces, which I felt unnecessarily cluttered my code.

The conditional statement ( condition ? if_true : if_false ) is the only supposed C style code that is not a goto statement masquerading as a useful function. As to my professor's aversion to my using it, I have come to the conclusion that she was concerned about the difference between if /statements/ and if /expressions/. The difference between the statement and the expression is simply that the expression returns a value, which is what gives it purpose.

Is C style programming a bad habit that needs to be stamped out of C++ programs? That depends on who you ask. I know that when I am coding, I use what I feel is a healthy balance of coding styles, only breaking from common conventions when I feel it is necessary to make my code simpler to read and execute. If I were to be honest with myself though, this happens much less often than I care to admit.



About the Author:
Bryan Young is a staff writer for WebProNews.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

C Programming Trends is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal