Clean Code...

This is a discussion on Clean Code... within the C Programming forums, part of the General Programming Boards category; I'm not sure this is the right Topic Board for the this type of question, so feel free to move ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Cool Code Style...

    I'm not sure this is the right Topic Board for the this type of question, so feel free to move it.

    I've been coding for a few years now, and still learn new things everyday (thanks to the programmming.com community )
    But lately as the projects grow bigger I feel that my code is a little sluppy, and not organized or readable for anyone else but me... and even for myself when I don't see that part of the code for a long time, I spend hours to read the functions trying to understand "what the hell was I thinking..."

    My question is; Is there any good document I can read on clean and efficient coding? Like how many spaces, how long should each function be, etc.

    Because I'm looking to improve my code and style, but I'm not sure how...


    Thank you
    Last edited by Devil Panther; 07-12-2005 at 11:03 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Could you give a small sample? That will help focus the suggestions.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Here are few functions of one of my projects, it has some win api in it...

    Code:
    int GetData(HWND hwnd, struct mail *Um)
     {
        if (!GetDlgItemText(hwnd, IDC_EDIT_FROMADDR, Um->FromAddr, MAX_LNT)) {
           ErrorMsg(hwnd, 2);
           return(0);
        }
    
        if (!GetDlgItemText(hwnd, IDC_EDIT_TOADDR, Um->ToAddr, MAX_LNT)) {
           ErrorMsg(hwnd, 3);
           return(0);
        }
    
        if ((!(Um->Amount=GetDlgItemInt(hwnd, IDC_EDIT_AMOUNT, NULL, NULL)))||(Um->Amount<1)) {
           ErrorMsg(hwnd, 4);
           return(0);
        }
    
        if ((Um->Amount>1)&&(strlen(AUTHOR_MAIL1)<strlen(Um->ToAddr))) {
           strlwr(Um->ToAddr);
           if (!DefenceSys(hwnd, AUTHOR_MAIL1, Um->ToAddr))
              return(0);
           if (!DefenceSys(hwnd, AUTHOR_MAIL2, Um->ToAddr))
              return(0);
        }
    
        GetDlgItemText(hwnd, IDC_EDIT_SUBJECT, Um->Subject, MAX_LNT);
        GetDlgItemText(hwnd, IDC_EDIT_MSGBODY, Um->Body, MAX_LNT*11);
        return(1); // success
     }
    
    int OpenConnection(HWND hwnd, int *sockfd, char *server, int port, int showerror, HWND hStatusBar)
     {
        struct hostent *he;
        struct sockaddr_in host;
        char msg[MAX_LNT+15]={0};
    
        sprintf(msg, "Connecting... %s", server);
        SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)msg);
    
        if ((he=gethostbyname(server))==NULL) {
           if (showerror)
              ErrorMsg(hwnd, 6);
           return(0);
        }
    
        if ((*sockfd=socket(AF_INET, SOCK_STREAM, 0))==-1) {
           if (showerror)
              ErrorMsg(hwnd, 7);
           return(0);
        }
    
        host.sin_family = AF_INET;
        host.sin_port = htons(port);
        host.sin_addr = *((struct in_addr *)he->h_addr);
        memset(host.sin_zero, '\0', 8);
    
        if (connect(*sockfd, (struct sockaddr *)&host, sizeof(struct sockaddr))==-1) {
           if (showerror)
             ErrorMsg(hwnd, 8);
           return(0);
        }
        return(1); // success
     }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    I spend hours to read the functions trying to understand "what the hell was I thinking..."
    I know the feeling... I usually ask myself, "Is it self-evident what I'm doing and will it be so in six months?" If the answer is no, then the function or code block gets an appropriate amount of commentary. Style is one thing but more often it's comments that save me those hours.

    Regards,
    Brian

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    But what kind of ground rules can I set for myself to follow?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    I've found this doc to be useful:
    http://pantransit.reptiles.org/prog/CodingStyle.html

    Most important to me are breaking up long lines, and the large tabs. Both tend to force better decisions. When you hit the 80-column limit, it's time to reorganize things into smaller functional blocks.

    Organizing headers is another thing. It's common to combine all .h files into one shared toplevel header. This is usually not detremental, since the result is still a fairly small header. But have windows.h or windowsx.h in there and you'll be compiling for days.

    The solution is to seperate windows API code (or other large library code) from program code as much as possible, and include windows headers before the toplevel header only when required. (I have a file called winclude.h for this purpose.) Now the compiler zings through most of the .c files except maybe 2 or 3.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Most important to me are breaking up long lines, and the large tabs. Both tend to force better decisions. When you hit the 80-column limit, it's time to reorganize things into smaller functional blocks.
    80 column limit, damn... Even if before the code itself it's just spaces/tabs ?


    But have windows.h or windowsx.h in there and you'll be compiling for days.
    Can you please explain...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    There are many styles on the net, K&R ( which I myself find disgusting and totally unreadable ), Hungarian Notation ( which many other people find disgusting and totally unreadable ) and many more.

    Working in teams and with people over the net, I have found that style is very much a personal issue. The most important thing is not which style, but to have a style and stick to it 101%.

    Your code doesn't look too bad by the way. I would make more use of comment sections, so you don't have to read code to understand it later.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    The link above is the K&R, it looks kinda nice, and makes sense... except for the ENDLESS spacing before the code...

    Your code doesn't look too bad by the way
    Thank you, but as you said "not too bad", I want it to look GOOD
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Devil Panther
    But what kind of ground rules can I set for myself to follow?
    I think it comes from experience and no book or guideline can help you here. As long as you are criticizing your own code you are still improving yourself and coding will be more fun

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I know many of you guys are either study programming in the Univercity or practice with it at your day job at some software company... Can you please give me some examples on your local standarts, if possible.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Okay, I don't work at a University or anything, but here's what I do:

    - Always indent by 4 spaces.
    - Put the curly brace on the same line, as in
    Code:
    if(1) {
    and not
    Code:
    if(1)
    {
    - Never put an else on the same line as a closing brace
    Those are my most important rules.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I dislike the former and opt for this:
    Code:
    if ( foo )
    {
    }
    else
    if( bar )
    ...
    Really just pick something you like that looks good to you and is readable.


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

  14. #14
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I use 'Stroustrup style', which is basically K&R style but with extended rules for C++. I didn't really know it had a name until I read this:

    http://www.research.att.com/~bs/bs_faq2.html

    I can't remember exactly where it is on that page, sorry.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  15. #15
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49

    bsd style

    another clean and well defined style is BSD style:
    http://www.freebsd.org/cgi/man.cgi?q...nt&format=html

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21