variable declaration style

This is a discussion on variable declaration style within the C++ Programming forums, part of the General Programming Boards category; I've always been told this was just a matter of style/taste, but I was wondering if that is actually the ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002

    variable declaration style

    I've always been told this was just a matter of style/taste, but I was wondering if that is actually the case:

    example 1 - better for brevity:
    int a=0, b=0, c=0;
    example 2 - better for clarity:
    int a=0;
    int b=0;
    int c=0;
    I've tried both on large projects and haven't noticed anything different as far as compile time, program speed, etc.. Is either considered a standard or is it really just a matter of taste?

  2. #2
    Registered User
    Join Date
    Mar 2002
    It's a matter of taste and clarity.

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    I personally mix the two styles.

    If variables are logicaly related to each other, I use your first form. If they are not, I use the second form. if I can't use the first form and they are related, I create groups of variable declarations, like this:
    int a = 0;
    lng b=0;
    int k = 0;
    char m='c';
    For me, the most important is to, not only identify them, but also understand possible relationships, at a glance.

    Finally, I only declare at the top of the current scope those variables that are somewhat relevant to the business model. Flags and such, I only declare and initialize when I need them.
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    I prefer a combination of the two for long lists of variables:
    int a = 0,
        b = 0,
        c = 0;
    For short lists such as the above (only an example), I place them all on the same line since they are relatively easy to locate.

    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002

    I will go with what Prelude worte

    Originally posted by Prelude

    int a = 0,// comment1
        b = 0, // comment 2
        c = 0;
    and If you add some comments that will be great
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compile projects ?
    By manzoor in forum C++ Programming
    Replies: 31
    Last Post: 10-15-2008, 11:52 AM
  2. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  3. Variable Declaration & Initialization :: C++
    By kuphryn in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2001, 09:22 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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