Programming Style? Which is preferred??

This is a discussion on Programming Style? Which is preferred?? within the C Programming forums, part of the General Programming Boards category; Hey guys, I am pretty new here and although I have learnt programming for some time now, I am still ...

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    Programming Style? Which is preferred??

    Hey guys, I am pretty new here and although I have learnt programming for some time now, I am still "raw"

    Anyway, just like to know what format do you (or any other real professional programmers) prefer:

    1)

    int main () {
    //body
    }

    OR

    2)

    int main ()
    {
    //body
    }

    Personally I prefer 1) cause it makes the code easier to read, but.......
    If only life is as easy as C...

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    its all personal preference use what you like best.

    but since you asked i use 2.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Its really funny, when I started programming, I used 2) too, but since I started college, everyone i know uses 1).....before long, I too am using 1)
    If only life is as easy as C...

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I've never used 1, I have always used 2 and will keep using 2. Since in my opinion it's more readable. When reading code it's much easier to find { } pairs when you write them in the same column. It's two years now that I've left university and went to work, but I have seen just a few people using 1.

    I don't mind what style people use, as long as they use the same style throughout their whole code. It's just personal.

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    I use 2. I program using vi alot and it makes lining up the braces easier, and I just carry this habit across to my Windows machine. Its funny because I am in the process of writing a programming spec for the programmers at work who use a C like interperated language. This topic could become the subject of programmer holy wars I tell you!

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Interesting. At our company we have a QA department which has written a set of documents about how programming. For example an Embedded C Coding Guideline, C Coding Standards, C++ Coding Standards etc. These documents are used or adapted by the projects.

    A topic like this is also part of those documents. Just like
    - never use goto
    - don't use return in void functions
    - avoid using exit() and breaking functions
    etc.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    2 - always.
    kooma - t.h@iki.fi

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    I use 2 as well. It seems easier to read that way.
    Last edited by The_Nymph; 03-09-2002 at 09:35 AM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Actually, I prefer a variation of 1. Functions and structures use an Allman style for the braces while loops and control structures use a K&R style.
    Code:
    int main ( void )
    {
      int i = 0;
      if ( i == 0 ) {
        puts ( "Fooby" );
      }
      return EXIT_SUCCESS;
    }
    Lining up the braces doesn't matter if the code was indented properly, and I've found that I can read code more easily that uses this style with an indention of 2 spaces. But code formatting is very much the preference of the programmer.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    I use no. 2
    int main ()
    {
    .........
    ........
    }
    !G!

  11. #11
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Whoah, so I guess 2) really is more popular!

    Prelude's style is really a great idea, I guess I will adopt that
    If only life is as easy as C...

  12. #12
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Come to think of it, I guess what I started using 1) is because one of my instructors suggested a general rule is that anything more than a page of code should be broken down into different functions...

    So if I use 1) I would run out of space pretty soon! Well, in the end, I started using 1) but my code still ends up longer than a page for each function!
    If only life is as easy as C...

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but my code still ends up longer than a page for each function!
    It depends on the complexity of the function. If your function does something simple and is very long then you might want to consider redesigning it into several functions. A good rule of thumb is to modularize functions in such a way as to have each function perform one operation, like an assembly line. Each person on an assembly line performs one operation and does it well, but when put together, all of the people make something big.

    The style of your code doesn't matter as long as someone can pick it up and read it without having to ask you what something does or where you have so and so. Compact is good to a point, but some people take it to the extreme, my functions tend to be rather short but they are still easy to follow for anyone with a grasp of the basic syntax of the language.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I use 2. I think it makes it easier to match up things.

  15. #15
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    You can recognise some programmers by their programming style.

    My style:
    Allman style braces.
    Never use // where possible /* */ looks better
    int main(void) always, except when argc/argv are needed or WinMain and whatnot.
    one space indentation
    I use pointers a lot in functions and avoid global variables, and structures I use only for binary files and the time (they use too much memory )
    I usually have full variable names: "string" rather than "str".
    I don't code in C++, it's unnecessary!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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