Thread: indenting code - different styles

  1. #16
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Quote Originally Posted by sept View Post
    This 1)
    ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) );
    is far better readable for me then this 2)
    ptr=static_cast<int*>(realloc(ptr, sizeof(*ptr)));
    I dunno. I would say a balance is important for any coding style. I would write that like this
    ptr = static_cast<int*> (realloc(ptr, sizeof(*ptr)));
    I just want to use my computer in a productive manner, not learn how to use it. Elysia
    A year spent in artificial intelligence is enough to make one believe in God. Alan Jay Perlis

  2. #17
    Registered User
    Join Date
    Sep 2007
    Quote Originally Posted by ICool View Post
    Im new to C and one of things my lecturer stresses out is that indenting is important, and marks will be awarded for presentation. I had a brief look at this wiki article :
    And it shows a lot of different styles. My question is which one should I , the beginner adopt and which one is used most frequently ? Is white space good ? Shoud I use tabs only or tabs and spaces ?
    Which one does he want to see? Cater to him, because he's giving the grade.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > For nuances for if, else and such I use tabs. Imho tabs are much more easy to write for me (using visual studio).
    > It`s also more easy to change the nuances if you use tabs (in case of removing one of multiple ifs for example)
    1. If you post code to a forum / mailing list, the result is almost always "what we get is NOT what you see". This is even more true if you've set the IDE to be something other than 8 spaces.
    2. The IDE is smart enough to figure out indent. You can for example highlight a whole block of code and press tab to indent one level and shift-tab to undent one level.
    Coupled with auto indent as you type, most of the time it isn't a problem at all.

    > Is there already some program which can change the style of the source? Like 1) to 2) would be awesome.
    Loads of them
    If you have cygwin / Linux, then
    man indent
    info indent

    > Which one does he want to see? Cater to him, because he's giving the grade.
    The first style is that of whoever is giving the marks / writing the pay cheque.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    train spotter
    Join Date
    Aug 2001
    near a computer
    I have two styles.....

    The style I use at work. I write assuming someone will have to fix the bugs in the code a few months/years down the track (train pun...).
    I want to make my intent as clear as possible, the code as readable as I can and so (hopefully) make the fix easier. I even put braces around single lines of code. I use 'allman'.

    The style I use when I write code that _only_ I will read again. I write it as fast as possible with few comments and no spaces.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

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. Fixing the Indentation draft
    By Elysia in forum A Brief History of
    Replies: 47
    Last Post: 02-23-2008, 11:17 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM