Thread: Good C programming practice

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    38

    Good C programming practice

    Hello,

    You can share good and bad C programming proctices as per your perspective. If anybody know good article on web about this, let me know.

    I would like to know, whether following C coding style is good or bad.
    1. Too many returns in routine. ( does that slow down the process)
    2. Cascading too many ifs and elses.
    3. Why not to use 'go' statement in C. How it is different from other control transfer statements?

    Bye,
    Juganoo

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    1. Too many returns in routine. ( does that slow down the process)

    Doesn't slow you down, in some cases can be even faster, but it often makes it more difficult to mentally step through code, especially if you didn't originally write it.

    2. Cascading too many ifs and elses.

    Conditional execution is involved in just about every aspect of programming. "Too many" connotes a bad thing but usually it is just a matter of efficiency and readability.

    3. Why not to use 'go' statement in C. How it is different from other control transfer statements?

    Its goto, and it really hurts readability. Its easy to use at first, simple to have one flat code base and jump from place to place, but that imposes serious maintenance and scalability issues later on. That said, not all uses of the malligned 'goto' are inherently evil. Namely, breaking out of nested loops is usually a good place for it.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're only ever going to learn one "good programming practice", I'd suggest: Make readable code..

    If your code is clear and understandable, it doesn't really matter what else you do, assuming you're using standard safe practices. (IE: Assuming it's bug free and meets the standard as far as functionality and usage go.)

    Hard to read code is a nightmare. If you're ever going to concern yourself with any one point, I'd focus on making your code logical and clear to understand with a standard style (indentation and what not) throughout. (IE: Don't switch to 2 spaces per indentation on some lines, then a tab on another, etc.)

    No, this isn't really what you were asking, but if your code is readable, it doesn't really matter how many if or return statements you use.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Make use of function calls to make your code more readable. I have seen some code that has about 2 pages of local variables, and then 6 pages of who-knows-what. I always let someone else work on that one...

    Related: Disable cut and paste features of your editor.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can share good and bad C programming proctices as per your perspective. If anybody know good article on web about this, let me know.
    There are several style guides on the internet which provide a lot of tips. I suggest you read it and think about it.

    http://www.chris-lott.org/sitemap.html

    Also an interesting site, from the perspective of software engineering

    http://software-engineer.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 05:38 PM
  2. Good practice of writing debug print statements
    By hzmonte in forum C Programming
    Replies: 11
    Last Post: 11-09-2005, 11:55 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 04:23 PM
  4. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM
  5. good programming practice
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 02-26-2002, 08:09 AM