Thread: A Question of Style

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To use an analogy, think about how you write English. Is there a "correct" style? You write in sentences, which can have a certain length and a certain style that's appropriate to what you're writing. These form paragraphs which also have a certain length, style and formatting. There are organizations which produce style guides which dictate some rules to follow in certain contexts, however, ultimately your goal should be pretty similar to what you do when you write code, regardless of which style you use - to convey your ideas clearly, to both yourself and your team.

    I don't understand why people need to focus so much on indentation. It's pretty obvious what looks acceptable or not. When I see hard-to-understand code it is usually not because of braces and indentation. For example, functions which are too long are also a style issue - probably a more important one than whether you use 2,3,4 spaces for indenting.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    To use an analogy, think about how you write English. Is there a "correct" style?
    It is certainly true that some writing styles are more acceptable than others. Read a few pages of any writing, and one does respond to the writing style as well as the content.

    Quote Originally Posted by c99tutorial View Post
    I don't understand why people need to focus so much on indentation.
    Because, when coding, indentation helps lay out the code so a human being can better understand it. Different indentation styles can make code easier or harder to understand, even if the code otherwise has the same meanings.

    Quote Originally Posted by c99tutorial View Post
    It's pretty obvious what looks acceptable or not.
    Not always, depending on how deeply you examine the code.

    Quote Originally Posted by c99tutorial View Post
    For example, functions which are too long are also a style issue - probably a more important one than whether you use 2,3,4 spaces for indenting.
    It is true that function length, as a consideration, is more important than indentation. However, that's because function length is more than just a style issue. It is actually a basic architectural concern (in fact, having functions that are too long is sometimes described as an "architectural smell", a sign of a potential technical deficiency in the software) and getting it wrong can directly compromise ability to get code working correctly at all, maintain it, and reduce undue sensitivity of parts of a system to changes in other parts.

    It is possible to write simple code that converts any code to a specified indentation scheme. It is much harder to change architecture (basic organisation) of non-trivial code without breaking it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #18
    Registered User DreamThink's Avatar
    Join Date
    Feb 2013
    Posts
    8
    So, basically, some people feel there is a right and wrong way to format your code. Other people feel it's just a matter of personal choice.

    Reading over the replies, I can see advantages and disadvantages to each style, and my conclusion is that I should use whatever style helps me code and maintain that code efficiently--unless that style might make it difficult for someone else later on to revise that code. Thanks for stopping by the thread guys, it helped me look at things in a different perspective.

    DT

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Allman.
    Variations I use:
    When quickly writing code for this forum, I tend to use 2 spaces instead of 4 to save time

    Also, after a very annoying bug caused by a do/while getting mixed up, I now use
    Code:
    while(blah)
    {
      continue;
    }
    
    /* instead of */
    while(blah);
    That way you can see that the while statement is not part of a do/while loop

    It's not important what style you choose, as long as your consistent.

    Here is a good read if you'd like Indent style - Wikipedia, the free encyclopedia

    Oh, and if I was to nominate a "correct" indent style, it would probably be the style the Linux kernal and Unix kernal were written in (1TBS, even though I don't use that style myself).
    Fact - Beethoven wrote his first symphony in C

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What does "correct" style mean?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You know what it means. The same thing it meant when that one person said "Juicyfroot" was correct, in certain terms, because Microsoft uses it. Except this time, it's correct because it was used making software! Incredibly geeky software at that.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    There actually is a 'correct' style in JavaScript - albeit it's only truly necessary to be aware of when returning an object literal; in that case putting the open brace on the next line will cause the code to parse in a manner in which the programmer probably does not intend. But because of that, for consistent styling it's best to always put the open brace at the end of a line.

    Personally, I use whatever is standard for the code I'm working with; generally for C# I put opening braces on new lines and JavaScript gets braces at the end of each line. In fact my IDE will do that for me if I choose a different style.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #23
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by thickster View Post
    Though my statement will be controversial, there is only one correct answer to this. Juicyfroot. The reason for this is in the following example of Microsoft C#. That's what Microsoft uses in their examples. That sets the standard. Follow the leader. I'm not kidding.
    What horrible logic this is. Let's say for some reason everyone has to use whatever some big company recommends, like you say: Why Microsoft specifically, why do they decide? Google has a C++ style guide here, so why not follow their style guide? I could probably argue that Google is even more of a "leader" than Microsoft when it comes to the "biggest congregration of professional software engineering genius in the world".

    Ignore anyone who says anything else....
    Anyone who claims something and then tells you to ignore all other arguments either knows he is wrong and is deliberately trying to misguide people, or he thinks he is the final authority on the matter. So which is it? I will listen to any and all arguments and decide for myself, thank you very much.

    Indent by 4, though. 2 is too small,
    Google doesn't agree with you.

    C# is the natural successor to C and C++, so start abiding! When the biggest congregration of professional software engineering genius in the world decides to do it this way, it doesn't make sense to do any different.
    ....Just.....wow. I'm speechless.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Having worked with teams at many of these "trend setting" companies, I can give you some idea of how this deep and complicated decision process actually works:

    Basically, a bunch of top programmers at the company sit in a room and yell at each other until they get tired of yelling. Whatever happens to be written on the white board at the end of the session is accepted as the coding standard for the next year. If the debate is picked up again the next day, at least half of the people in the room will assume the opposite positions than they had on the previous day.

    No really, that's how it works.

    Not many software products have failed because management selected the wrong number of spaces to indent. But many, many projects have failed due to internal dissent, uncooperativeness, and downright sabotage by those who throw hissy fits when they don't get their exact way.

    We don't allow children onto our teams, so for the most part this is not an actual problem for us.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #25
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    But many, many projects have failed due to internal dissent, uncooperativeness, and downright sabotage by those who throw hissy fits when they don't get their exact way.
    WOW - I'm glad that I've never had to deal with that!
    Fact - Beethoven wrote his first symphony in C

  11. #26
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brewbuck View Post
    We don't allow children onto our teams, so for the most part this is not an actual problem for us.
    A fair proportion of programmers and a fair proportion of managers do act like petulant children when they don't get their way. You've been lucky if you've never encountered one on a team.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #27
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I prefer "juicyfruit". I find it easiest to read. I like to have 3 or 4 spaces for indentation. I don't always use braces, but if I find myself having to think even a little bit about the control flow of several nested braceless statements, I'll tend to go back and put braces in. I name class, struct types and global variables in CamelCase, local variables are lowercase underscore_separated.

    Quote Originally Posted by Elysia View Post
    You do not seem to have learned the lesson people are trying to convey here.
    There is no ultimate style. There is no single style that professionals are using. There is no "professional" style.
    Everyone has their own style. Companies have their own style. Projects have their own style.
    So, once more: pick a style you think is best for you and stick with it. Feel free to experiment.
    Well said.

    I do do this for a living -- I work on a fairly old fairly huge codebase, and there's no one style followed throughout. The rule is "follow the convention of the code nearby" -- and if there is no nearby code, people just use their own style. Some other teams have stricter conventions, but I don't think there's any consensus on any one style being "better" than the others. I don't really have any trouble reading any of the differing indendation or formatting styles... but there's a lot more to coding style than that. My boss got high on templates for about a year and parameterised the crap out of everything,... others will go to great lengths to avoid C++ and do crazy scary stuff in C.

    I only remember getting ........ed off about coding style once at work -- this guy had the most bizarre style. Lots of spaces in random places, indentation out of line with the rest of the file, generally weird and wholly inaccurate variable names. Occasional flurries of absolute insanity. I asked him about it -- suggested he could follow the style of the rest of the project -- he said he did it on purpose so he could easily spot his own code. Hmmm, ok. He's since left, and I'm still cleaning up the bugs he left.... but at least they're easily identifiable as his bugs! Saved me an svn blame I guess.

    I think big open source projects (like.... the linux kernel ) are quite skittish about coding practices. Makes sense I guess if you've got a lot of contributers.

    I don't think I have a very strong opinion about coding style. You can bet that if some manager tried to make me change it I'd be on the warpath though

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't think I have a very strong opinion about coding style. You can bet that if some manager tried to make me change it I'd be on the warpath though
    Therefore you do have a strong opinion.

  14. #29
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Hmmm No, I really don't. I think it's important to format your code legibly but beyond that, I really don't think there's much in it between the styles. Some posts in this thread have managed to say tangible good things about particular layouts --- that's more than I can do for my preferred one. I don't think it makes me a better faster stronger coder, just I prefer it.

    So why would I be on the warpath? I think that's more than "damn you manager" warpath, because I'm sure they'd say the opposite of what I just said. They'd claim that whatever they wanted us to do would massively increase our efficiency, and that disparate coding style has been the problem all along. To be fair, the folks I work for haven't ever done anything quite that bad, but it is about the right level of silly for them to get their teeth into.

    Yes, actually... I might have a strong opinion about this!

  15. #30
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    I agree with the coding style of K&R. It states to use an open curly brace of a function in a new line, and in other cases, open it in the same line of the given block.

    Example:

    Code:
    void function()
    {
         if (some_test) {
             /* Do something */;
         }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating C style Strings Question
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 03:19 PM
  2. Defining a Union - A Question of Style
    By clark.leach in forum C Programming
    Replies: 6
    Last Post: 11-21-2008, 08:13 PM
  3. C style arrays question
    By Raigne in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2008, 12:06 AM
  4. if...else...finally? question about style
    By Nyda in forum C Programming
    Replies: 17
    Last Post: 11-24-2004, 10:20 AM
  5. C++ Style Question
    By Davros in forum C++ Programming
    Replies: 7
    Last Post: 11-17-2004, 03:52 AM