1

Just incase my vague description isn't enough to give you the full picture, I mean like this:

private bool
    f = true,
    oo = false,
    b,
    a,
    r = false;

instead of:

private bool f = true;
private bool oo = false;
private bool b;
private bool a;
private bool r = false;

Is there any notable advantage or disadvantage in grouping in this way?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
n00dles
  • 255
  • 3
  • 18

3 Answers3

4

Pasting both of your examples into LINQPad produces identical IL results:

IL_0000:  nop         
IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // f
IL_0003:  ldc.i4.0    
IL_0004:  stloc.1     // oo
IL_0005:  ldc.i4.0    
IL_0006:  stloc.s     04 // r
IL_0008:  ret    

So no functional difference, just personal preference.

Jon B
  • 51,025
  • 31
  • 133
  • 161
  • Nice! Omg btw, wtf is IL? – n00dles Jul 17 '17 at 17:52
  • 1
    @n00dles [Intermediate Language](https://en.wikipedia.org/wiki/Common_Intermediate_Language). – Jon B Jul 17 '17 at 17:53
  • Oh, right! That thing! So how do I check performance like you did there? can I do it within VS2017? – n00dles Jul 17 '17 at 17:55
  • 1
    @n00dles I used LINQPad. There are other ways. [See here](https://stackoverflow.com/questions/3326571/how-can-i-view-msil-cil-generated-by-c-sharp-compiler-why-is-it-called-assemb). – Jon B Jul 17 '17 at 17:56
  • 1
    @n00dles VS 2017 includes a command-line utility called [ildasm](https://learn.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler) (AKA Intermediate Language Disassembler). That said, I can just about guarantee LINQPad is easier. Good utility for testing, regardless. – Sinjai Jul 26 '17 at 14:44
3

The code the compiler generates for both code snippets will be identical, so the only remaining concern is maintainability of the resulting code.

As far as maintainability is concerned, the main difference is that the "ungrouped" approach lets you write documentation comments for each individual variable, i.e.

/// <summary>Red color component</summary>
private int red;
/// <summary>Green color component</summary>
private int green;
/// <summary>Blue color component</summary>
private int blue;

while the "grouped" approach lets you specify a single documentation comment for the entire group:

/// <summary>RGB color components</summary>
private int red, green, blue;

Neither approach is better than the other; they are just different.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    GA!(Good Answer)(...now redundant) So it's good if you want a blanket comment over a group. That's good to know. – n00dles Jul 17 '17 at 18:05
0

So from what I can gather from the comments and answers; there is no actual performance difference between grouping and not-grouping; grouping being generally 'frowned upon' for various reasons. However, grouping variables does seem like it could be advantageous if you were to group closely-related variables, which would always share the same type, accessibility and comment. This could increase efficiency and workflow performance, but maybe not too a clear degree where it is widely utilised.

n00dles
  • 255
  • 3
  • 18