Best Practices in C/C++ Programming

ProgrammingA few years back I wrote a short piece on Everything2 about C/C++ programming techniques. Here it is....

I've been programming in 'C' and 'C++' for a few years now, along the way I've learned a thing or two that have become part of my personal best coding practices and I thought I would pass these tidbits on for others to use. These hints, while written with the 'C' and C++ in mind, can be applied to other structured programming languages as well.

For Maintainability
When coding, keep this firmly in mind: someone (most likely you!) will have to revise and maintain the code your writing today at some point in the future. Will you be able to understand the code you're currently writing six months from now? A year from now? If not then you need to change some things.

Do not comment the obvious
But if you have done something rare, elegant, crafty, or otherwise tricky then comment the hell out of it. "Voodoo Programming" is not a viable method for ensuring job security.

Naming Conventions
It is common practices (especially in Windows programming) these days to use what is called Hungarian Notation as a naming convention. This notation seeks to encode type information in a name as a visual clue to the programmer, in an effort to eliminate data type mismatches. The fact is that most compliers will always catch type mismatch type problems.
For example:


int iCount; // An integer variable.<br>
int iGetPageCount(DOC *ThisDoc); // Returns an int<br>
iCount = iGetPageCount(MyDoc); // Types match.<br>

The problem with this practice is that it detracts from the maintainability of the code. If you need to change the iGetPageCount() function (above) to return a long value, you have to wade through your source, looking for every occurrence of the function name and change it to lGetPageCount() and you have to change the name of every variable that receives a value in a call to that function - what a maintenance nightmare!!

Without using the Hungarian Notation conventions all that needs to be changed is the type of the function and the variables that receive its values. For this reason alone I have given up using this convention.

Giving up Hungarian Notation has saved me many hours that would otherwise be worthlessly spent maintaining my naming convention.

Start Small
When dealing with large, daunting projects, it is very easy to get tied up in trying to plan and design the whole thing at one time. This is usually counter productive. Break the problem down into smaller logical units. If each of these is still too big, then break them down into yet smaller logical units. Start designing when you have a piece that you can get your mind around. I've often found it beneficial to design and build a small piece, test it, then go on to the next piece, rather than designing them all at once. Do the coding while the design is fresh in your mind.

Test, test, test!
Test the small pieces of your project as individual units; make sure they work as planned. If they do work then try passing them bad data elements, just to see what happens. Yeah, yeah I know that subroutine will never be called with a value that big! Yeah right. Been there, done that, spent hours tracking down the problem. Protect your code from bad data!

Build test harnesses into your code
Include a main() function in each module (surrounded by conditional compilation macros) so you can compile and test it in isolation from other modules. If you've written a library take the time to write a module that includes a main function that does nothing except test the library routines.

Read, read, read!
Do you read Dr. Dobbs, Software Development, or the C Users Journal? If not - why not? You think you know everything? I learn something from each of these magazines every single month. "So what?" you may ask? Well, let me put it this way; I hope you are the competition for my next job.

Trackback

Trackback URL for this entry: http://www.tbayne.net/trackback.php?id=2008022610225974

No trackback comments for this entry.

40 comments

The following comments are owned by whomever posted them. This site is not responsible for what they say.
Authored by: Anonymous User on Monday, January 28 2013 @ 08:51 AM EST Best Practices in C/C++ Programming
I gotta tell you, learning C/C++ Programming was perhaps the most challenging and draining experiences in my life. The thing was equivalent to five term papers in terms of difficulty. Back in the day, I would rather spend long nights doing the former.
Authored by: Nila on Monday, September 30 2013 @ 04:45 AM EDT best custom essay writing services
The actual story approach, because the term implies, recounts a tale. best custom essay writing services really a personal incident worth retelling, or a fantastic practical knowledge based on a hyperactive creativity. Although whatever the case, your caused composition requires a wealthy higher level of vividness that should capture followers in the history. It's not sufficient which the followers merely fully grasp your story
Authored by: Coach Outlet Online on Thursday, October 17 2013 @ 02:24 AM EDT Coach Outlet Online
The typicalCoach Outlet Online black undulating lace veil shieldingCoach Factory Outletthe young women's eyes are representedCoach Outletby
Authored by: Jamess aleen on Friday, May 02 2014 @ 09:02 AM EDT Best Practices in C/C++ Programming
Either you are a student or professional, a well written piece of paper always attract your attention. If you are poor in grammar then get dissertation writing services from our website which is the best choice for both educators and students to get their papers written at cheap price.
Authored by: Anonymous User on Monday, August 18 2014 @ 05:17 PM EDT Best Practices in C/C++ Programming
Professional college essay writers that can make you a high achiever!
Authored by: Anonymous User on Tuesday, February 17 2015 @ 03:51 AM EST Best Practices in C/C++ Programming
Your blogs are so congested beside information that I somedays guess there should be a logo on them love ‘for total your learned writing wishs, my Premier Essay writing service’. It might stable sensational however truly, your provide us really extra information on this than we are inclined to unruffled receive in family. For this I am very beholden.
Authored by: Micheal Elijah on Monday, May 11 2015 @ 08:53 AM EDT Best Practices in C/C++ Programming
Study example and practice used by professional programmers to note down well-organized and consistent system with C++/CLI. For More Information Contact-Aonepapers.com
Authored by: Anonymous User on Monday, June 29 2015 @ 01:53 AM EDT Best Practices in C/C++ Programming
I want to tell you about learning C / C++ Programming is the most challenging and experiences in my life. because it has many codes to learn it and some off i have learn it properly, it has many codes which makes C / C++ is difficult from other language. Business Conference Calls App
Authored by: Anonymous User on Tuesday, June 30 2015 @ 02:02 AM EDT Best Practices in C/C++ Programming
The real story move toward, since the term imply, recount a tale. sites that do my homework actually a individual event worth retelling, or a unbelievable practical information base on a agitated originality.
Authored by: Anonymous User on Monday, July 06 2015 @ 12:24 AM EDT Best Practices in C/C++ Programming
I am an avid reader who likes engaging content. That's why I am here. Your original views on this topic are refreshing and interesting. You've done a great job of expressing your views. Thank you. tu 95| call of duty| clicker heroes| strike force heroes 2| kitten cannon scooby doo games| scooby doo| brain games| braingames| brain
Authored by: Anonymous User on Friday, September 11 2015 @ 03:11 PM EDT Best Practices in C/C++ Programming
I desire to tell you concerning knowledge Programming is the the majority demanding and experience in my existence. maclock coupon code 2015
Authored by: Plant Vs Zombies on Wednesday, September 16 2015 @ 04:02 AM EDT Best Practices in C/C++ Programming
Authored by: Anonymous User on Thursday, September 24 2015 @ 08:19 AM EDT Best Practices in C/C++ Programming
A college education will also give me a change to bild up good communication and socialization skills. Interacting with people of different races and backgrounds will prepare me to deal with the people I'll face in the workforce. help with essay
Authored by: Anonymous User on Tuesday, October 06 2015 @ 05:02 AM EDT Best Practices in C/C++ Programming
Authored by: Anonymous User on Wednesday, November 04 2015 @ 06:45 AM EST Best Practices in C/C++ Programming
Essay writing help online! http://supreme-essay.net
  • red - Authored by: tyu on Monday, November 09 2015 @ 02:49 AM EST
Authored by: Anonymous User on Monday, December 28 2015 @ 02:50 AM EST Best Practices in C/C++ Programming
Thansk for this post :) By the way, Custom-Essays-Online.com is always here to help with essyas ;)
Authored by: Anonymous User on Friday, February 26 2016 @ 11:36 PM EST Best Practices in C/C++ Programming
Test the small pieces of your project as individual units; make sure they work as planned. If they do work then try passing them bad data elements, HND Assignment Help
Authored by: Anonymous User on Monday, August 08 2016 @ 04:26 AM EDT Best Practices in C/C++ Programming
Chaos Faction 2 with 29 weapons, 18 characters, 15 levels, 14 months in the making... the best games you will like its, have fun! Super mechs is a turn-based online strategy multiplayer game where you build your very own mech and use it to dominate your opponents, be they A.I. or other players like yourself! Mahjong free games online is a puzzle game based on the same tiles. The goal is to match open pairs of identical tiles and remove them from the board, exposing the tiles under them for play
Authored by: Anonymous User on Wednesday, September 21 2016 @ 03:56 AM EDT Best Practices in C/C++ Programming
I got to tell you, learning C/C++ Programming was possibly the most thought-provoking and demanding skills in my life. The thing was equal to five terms in terms of trouble. pay for my research paper
Authored by: shawn on Tuesday, December 13 2016 @ 04:06 AM EST romantic Whatsapp quotes of new year 2017
get the Great collections of Whatsapp Status Messages, SMS For this New year eve’s to share your wishes, Quotes, Status, Greetings, SMS, Messages to your Friends, Family, Relatives and Loved Ones. romantic Whatsapp quotes of new year 2017
Authored by: WagnerZack on Friday, December 30 2016 @ 06:15 AM EST Best Practices in C/C++ Programming
Albeit whatever the case, your brought about structure requires a well off more elevated amount of striking quality that ought to catch devotees in the history. It's not adequate which the supporters just completely get a handle on your story. It is possible that you are an Personal Chauffeur Service Uk understudy or expert, an elegantly composed bit of paper dependably draw in your consideration. On the off chance that you are poor in sentence structure then get administrations from our site which is the best decision for both teachers and understudies to get their papers composed at shoddy cost.
Authored by: Anonymous User on Saturday, January 07 2017 @ 06:13 AM EST Best Practices in C/C++ Programming
Authored by: Anonymous User on Thursday, February 09 2017 @ 07:48 AM EST Best Practices in C/C++ Programming
You could make the variable names more Do My Assignment clear, however the expanded illustration is superfluous inside the capacity on the grounds that the relationship amongst separation and speed is the noteworthy element.
Authored by: Anonymous User on Monday, February 13 2017 @ 03:35 AM EST Best Practices in C/C++ Programming
Programming isn't my favorite thing in the world, but thanks to your ideas it may be quite easier and efficient. I appreciate how detailed description I found here. No doubt you are doing hard work here Please keep it good posting. You may use www.custom-paper-writing.org help and be sure in brilliant quality all of your new posts 24/7! Just try their help now!
Authored by: Anonymous User on Monday, February 20 2017 @ 08:34 AM EST Best Practices in C/C++ Programming
Projects are intended to be lovely. In the event that somebody discloses to you else, you'd most Stansted Airport Transfer likely do best not to tune in to whatever is left of his recommendation. A decent program is lovely in both its idea the calculation utilized, the plan
Authored by: Anonymous User on Wednesday, February 22 2017 @ 05:20 AM EST Best Practices in C/C++ Programming
Programming has been a confusing and quite difficult topic for me since schooldays. I have faced a lot of difficulties in dealing with the C/C+ programming. You have provided some effective methods to study the same. Thanks for the share. table rentals los angeles
Authored by: Anonymous User on Friday, February 24 2017 @ 09:16 PM EST mahjong games
Mahjong is the game that really enjoy the company of the player in the online playground and have the points to win the next game in the cool mahjong online and have some more to continue the game in the real world mahjong free games such a great pleasure to have with. this.
Authored by: Anonymous User on Tuesday, April 25 2017 @ 11:04 PM EDT Best Practices in C/C++ Programming
energy http://www.web-farm.co.uk replica rolex replica rolex storage display, 59220 than 59,000 increased by 1 mm thickness, from the bottom of the http://www.dreamforwatches.org.uk swiss replica watches swiss replica watches , the two are the same layout, the new function seems to be in the form of modules added to the http://www.replicaonlines.me.uk swiss replica watches swiss replica watches dial side. World also play Day-Date --- Park Tao Fenuo manual winding calendar calendar watch
Authored by: Anonymous User on Saturday, April 29 2017 @ 05:24 AM EDT tubemate for android
everything you share with us is current and very informative checkout tubemate for android
Authored by: Anonymous User on Friday, June 16 2017 @ 06:36 AM EDT Best Practices in C/C++ Programming
Dissertation writers from https://chicago.academia.edu/carlwolf
The writingservicebay.com employs writers from English speaking countries. Our writers are highly experienced in dissertation writing. We have dissertation writers who are experts in writing high quality dissertation essays. They will provide you with a first class dissertation on any subject. We have access to modern and current data bases for your dissertation paper. Our writers are familiar with all standard citation and referencing styles; APA, MLA, Chicago and Harvard. They will provide you with a dissertation paper that is correctly referenced.
Authored by: best smartphones under 20000 on Monday, June 26 2017 @ 02:34 AM EDT mi6 release date in india
Authored by: Anonymous User on Monday, June 26 2017 @ 06:09 AM EDT NEG SEO PACK
I visit daily a few websites and blogs to read content, however this web site provides me valuable and interesting information… NEG SEO PACK
Authored by: game of thrones season 7 online on Tuesday, July 04 2017 @ 02:50 AM EDT game of thrones season 7 online streaming
Authored by: Anonymous User on Saturday, July 08 2017 @ 05:08 AM EDT Masters Degree
Why Health Informatics is a Growing Career Field.Masters Degree
Authored by: Drastic Ds Emulator Apk on Thursday, July 13 2017 @ 01:17 AM EDT Drastic Ds Emulator Apk
Authored by: Drastic Ds Emulator for windows on Thursday, July 13 2017 @ 02:30 AM EDT Drastic Ds Emulator for windows

My Account





Sign up as a New User
Lost your password?

Events

There are no upcoming events