indenting code - different styles

This is a discussion on indenting code - different styles within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Originally Posted by sept This 1) Code: ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) ); is far better readable ...

  1. #16
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,664
    Quote Originally Posted by sept View Post
    This 1)
    Code:
    ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) );
    is far better readable for me then this 2)
    Code:
    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
    Code:
    ptr = static_cast<int*> (realloc(ptr, sizeof(*ptr)));
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    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 :
    http://en.wikipedia.org/wiki/Indent_style
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > 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
    http://clusty.com/search?query=code+...Mozilla-search
    http://clusty.com/search?query=code+...Mozilla-search
    If you have cygwin / Linux, then
    man indent
    or
    info indent

    > Which one does he want to see? Cater to him, because he's giving the grade.
    Absolutely.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    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

Page 2 of 2 FirstFirst 12
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 Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 10:17 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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