Thread: Coding style

  1. #16
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Any code that doesn't call constructor at the top of the block can either be

    a) rewriten with pointers and dynamic allocation as with nv's example (slow, leak prone even)
    b) rewritten so that there is an extra pair of braces not associated with conditional execution (unneccesary).

    Is there anyone who didn't come from coding in C, and thus didn't get into the habit of thinking that all variable declarations should be at the top of the block, but who still refrains from ever putting variables in the middle of a block?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  2. #17
    Registered User
    Join Date
    Jan 2002
    Posts
    363
    >>Is there anyone who didn't come from coding in C, and thus didn't get into the habit of thinking that all variable declarations should be at the top of the block, but who still refrains from ever putting variables in the middle of a block?

    Yes I did.
    I just think it helps the code to make more sense.
    I hate going through other peoples code and suddenly finding some new identifier and having no idea where its come form.
    If you own a piece of land and there is an volcano on it and it ruins a
    nearby town, do you have to pay for the property damage?

  3. #18
    It seems simple to me... If the variable is going to be used throughout the entire function, then its declared at the top of the function. If the variable is going to be used only once or in a small and specific area of the function (like for a for loop, error checking, etc) then it gets declared where its used. I would find it horribly disturbing to see:

    for(i = 0; i < l_ThisNumber; i ++)

    ...and not instantly know what 'i' was. It better damn well not be a short if you know what i mean...
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #19
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> I would find it horribly disturbing to see:

    I wouldn't, that is what I would expect to see! If you code in several languages, you use a style that fits all of them. I always know where my variables are declared.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #20
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Coding in a style that fits all langauges prevents you from exploiting the advantages of any.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #21
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> prevents you from exploiting the advantages of any.

    In this thread we were talking about the definition place for variable. Since C++ is the only one of the languages I use that allows the definition in random places, I don't believe it is a big sacrifice.

    I don't believe the ability to declare variables all over the place is an advantage. I've never worked in a professional house where it was done.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #22
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >I don't believe it is a big sacrifice.

    Hey I agree with that, especially since nothing is gained by it [nor do the rules for variable declaration say we must abide by it]. In fact, I support the former since it organizes your variable per scope, and prevents redeclaration of variables as well.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it bad style of coding ??
    By noobcpp in forum C++ Programming
    Replies: 15
    Last Post: 11-06-2008, 10:39 AM
  2. Your Coding Style?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 45
    Last Post: 06-02-2005, 08:19 AM
  3. Coding style?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 10-22-2003, 11:26 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 07:36 AM