6

I like variables named correctly but in some cases it is very hard to do.

So if my object implements IDisposable then I can use:

using (var whatever = new Whatever()) { //... }

But it is rare case, so I found another way to handle it - anonymous block (not sure how it called correctly):

//...
{
    var whatever = new Whatever();
    //...
}
//...
if (condition)
{
    var whatever = new Whatever();
}

Is it a good approach? Are there any pitfalls or widespread belief that it reduces code readability?

nawfal
  • 70,104
  • 56
  • 326
  • 368
Vladimirs
  • 8,232
  • 4
  • 43
  • 79
  • 2
    Even though I have read the question, why not name the variable after what it's going to be used for, rather than what it was declared from? – Nahydrin Jun 04 '13 at 16:10
  • 2
    Why can't you just overwrite `whatever`? `var whatever = new(); ... whatever = new(); ...` – SimpleVar Jun 04 '13 at 16:10
  • 4
    Normally I'd split the method up into different methods instead... at least in many cases. – Jon Skeet Jun 04 '13 at 16:11
  • Brian Graham, sometimes naming variables by what for I going to use it became odd for me(names became to long). So after I cannot came up with normal name I prefer to give name what it was declared from. – Vladimirs Jun 04 '13 at 16:24
  • Yorye Nathan, I think it a bad practice to assign to one variable logically different instances of object. So I am never do like that. – Vladimirs Jun 04 '13 at 16:26
  • Jon Skeet, What I am asking about is comes exactly after when I cannot give normal name or extract method(because it is worthless or it will have also odd name) – Vladimirs Jun 04 '13 at 16:28
  • 1
    You should see [explicit-local-scopes-any-true-benefit](http://stackoverflow.com/questions/16871395/explicit-local-scopes-any-true-benefit) – nawfal Jun 04 '13 at 16:41
  • @Vladimirs If you have logically different instances, they can easily have different names that are appropriate for their logic. Your argument is invalid. – SimpleVar Jun 04 '13 at 16:47

3 Answers3

4

Basically, if the compiler doesn't complain and your code is readable and easy to understand, there's nothing wrong with that.

For example:

foreach (var foo in foos)
{
   var task = new FooTask(foo);
   task.Run();
}
foreach (var bar in bars)
{
   var task = new BarTask(bar);
   task.Run();
}

This is actually (in my opinion) slightly easier to read than

foreach (var foo in foos)
{
   var task1 = new FooTask(foo);
   task1.Run();
}
foreach (var bar in bars)
{
   var task2 = new BarTask(bar);
   task2.Run();
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 1
    `var task = foo ? new FooTask() : new BarTask(); task.Run();` seems most readable to me, in this case. But it is not possible if they don't both implement some `IRunnableTask` interface, or something similar. – SimpleVar Jun 04 '13 at 16:15
  • 1
    @YoryeNathan Yes, I agree, I was just trying to come up with a case that would fit OP's question. I've updated my answer with `foreach` blocks which illustrate the point a bit better. – p.s.w.g Jun 04 '13 at 16:20
  • I never use enumerated variable names because I believe it is also bad practice in naming conversion. So in that case better would be give names which will better discover variable usage purpose. But in my case variables became odd and too long – Vladimirs Jun 04 '13 at 16:31
  • And neither is easier to read than `var fooTask = new FooTask(foo);` and `var barTask = new BarTask(bar); // it's called drinking`. – jason Jun 04 '13 at 18:29
  • What about naming bar as foo, or task (or task1), assuming it is the same type? I have just noticed an anomaly in.NET, but not certain about it. The compiler does not complain tough. – Adam L. S. Nov 18 '16 at 10:55
1

I just tested your code because normally, with VSCodeAnalysis and R# and StyleCop breathing down my neck I would have expected a lot of warnings. But no. They all keep silent. So it does not seem to be against any Microsoft coding guideline.

However: If you need to create superfluous anonymous blocks just for the sake of avoiding compiler errors due to variable names, it's no longer easy and simple. Basically, you are hiding from the compiler and hiding will never solve problems. From my point of view, it would be way better code if you chose a precise name for each variable (naming it for the type isn't exactly great anyway) and remove the anonymous block.

Following the KISS Principle: "Keep it simple, stupid".

nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

I would be cautious with this approach; it can impede readability and debuggability. I've had situations in the past where I had two breakpoints watching two different variables when I thought I was watching one.

The rules that C# imposes to help ensure that your methods do not use names inconsistently are complicated. If you're interested in learning more about them, see:

http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067