-2

When someone has a lot of variables, is it better to declare all the variables of the same type in one line or write one line for each variable declaration? (in terms of readability of the code)

for example

All in one line :

       int my_rank, comm_size, processors_used, name_length, *x, *y;
       double mean_value, variance, var_sum, *var_sums;
       char string,*tmp,option;

Each on its own line:

    int my_rank;
    int comm_size;
    int processors_used;
    int name_length;
    int *x;
    int *y;
    double mean_value;
    double variance;
    double var_sum;
    double *var_sums;
    char string;
    char *tmp;
    char option;

I would prefer the first over the second but i'm in a situation where i simply have about 19 integers and i think that either way makes the code look illegible and ugly.

I actually separated the declarations in logical blocks and grouped variables in the same declaration if they are correlated.

int my_rank, comm_size, processors_used, name_length;
int *x, *y, *send_counts, *displacement, num; 
int max, min, loc_max, loc_min, *max_array, *min_array;
int i,option,sum,*sums;
double mean_value, variance, var_sum, *var_sums, *delta_vector, *local_delta;
char proc_name[MPI_MAX_PROCESSOR_NAME];
MPI_Status status;

Which do you think makes the code more readable?

MattSt
  • 1,024
  • 2
  • 16
  • 35
  • 2
    It's opinion-based, but if I don't have much free time and I see an SO question with vars 'all in one line', I skip it. It's too easy to miss some bad declaration and suggests that the following code may well be written in an 'economical, clever' fashion, AKA 'difficult to debug'. – Martin James Dec 06 '15 at 11:09
  • 1
    Also 'all in one line' says 'even if their use is confusing and merits explanation, I will never add comments to these vars'. – Martin James Dec 06 '15 at 11:10
  • Although I have the **STRONG** opinion that it does, it's just my opinion. – Iharob Al Asimi Dec 06 '15 at 11:13
  • 1
    I personally **never** write pointer definitions on one line because I tend to forget that `b` in `int* a, b;` is **not** a pointer. Otherwise, I only write multiple declarations on one line if they **serve the same purpose** like `int i, j;` for indexing. – cadaniluk Dec 06 '15 at 11:15
  • 2
    @cad That is almost equivalent to `typdef`ing a pointer. Which is also why I prefer `int *a;` over `int* a;`. – Iharob Al Asimi Dec 06 '15 at 11:15
  • 1
    you should read [this](https://books.google.co.in/books?id=6ipFVfxKeN0C&pg=PT102&lpg=PT102&dq=why+do+not+declare+more+than+one+variable+in+c&source=bl&ots=NIEy6MgIau&sig=7vcTBwFp11wGNqRtr9dNwPhONV8&hl=en&sa=X&ved=0ahUKEwj5ivaKjsfJAhVDlIMKHXAEAfgQ6AEIOjAF#v=onepage&q=why%20do%20not%20declare%20more%20than%20one%20variable%20in%20c&f=false) book to know c standards :-) – Punit Vara Dec 06 '15 at 11:18
  • Thanks for the book reference and all the opinions.Will read the book and I ll stick to the one variable per line after all. But I'm curious,what was wrong with the question to get downvoted? – MattSt Dec 06 '15 at 11:27
  • 1
    Read [this](http://stackoverflow.com/tour). Also, be aware of drive-by downvoters. – cadaniluk Dec 06 '15 at 11:42

1 Answers1

1

It is mostly a matter of opinion whether you group definitions for local variables or separate them one per line. Here are some general remarks:

  • Local coding style may impose one over the other. Consistency with the rest of the project is valuable and helps readability.

  • Documenting some of these definitions with a short comment on the same line may be useful and prompts for separating them. Yet do not comment the obvious.

  • If you group definitions in logical blocks, do not mix pointers and variables on the same line. It is confusing to many readers.

  • Moving the definitions closer to where the variables are used also helps readability as it reduces the scope for these variables are reduces mental space as well, a limited resource.

chqrlie
  • 131,814
  • 10
  • 121
  • 189