Programming etiquette

Soldato
Joined
18 Oct 2002
Posts
15,861
Location
NW London
I am a C# programmer.
Yesterday I bought Resharper, which is a program that works in conjunction with Visual Studio and makes suggestions for improving your code.

The suggestion it keeps making is using implicit declaration vs explicit.

For example:

My code is completely explicit:
Code:
QuestionMonitorClass qm = new QuestionMonitorClass();

It suggested that I implicitly declare, as follows:

Code:
var qm = new QuestionMonitorClass();

What do you guys suggest?
Which is faster?
Which is better?
 
By the time the code is compiled, the two forms will be identical (same more or less with generics and overloads vs. the longer formats before them).

The benefit comes from writing and maintaining the code.

http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic2

When you have a local variable used as your iterator in a for loop, it makes productivity sense in not having to be so manic over data types, as it's essentially a throw-away and bound by the limits of the range you're looping over, and trivial for the compiler/IDE to always accurately determine the data type necessary.

Now if you need to later change the for loop range to expand it, you don't have to worry that the data type of your iterator is or isn't going to overflow, it'll adjust on its own.
 
Last edited:
When you have a local variable used as your iterator in a for loop, it makes productivity sense in not having to be so manic over data types, as it's essentially a throw-away and bound by the limits of the range you're looping over, and trivial for the compiler/IDE to always accurately determine the data type necessary.

Now if you need to later change the for loop range to expand it, you don't have to worry that the data type of your iterator is or isn't going to overflow, it'll adjust on its own.

From you example, I can see the benefit of using 'var', instead of say 'int', in a for loop. But where else would you see a benefit?

I'm quite happy doing explicit declarations (its become a habit), but is it bad to be so explicit?

What other advantage could there be of using an implicit declaration, over explicit?

From what I can see, this is a very minor programming issue.
 
None, it's all it is, a productivity aid so you can simply change the value you're assigning to the local variable, and that will happily cascade down through other places in the routine, without having to go and track each one down and change them.

IDE's have been trying to help out in this regard for a while now (do something wrong with a variable and you usually get a friendly advisor giving you a range of options on how to 'fix' it throughout the places it is referenced, some of which however are completely wrong and will break more than they sort out given your intent).

This method is both easy for them to implement, and easy for you to maintain, but nobody is claiming it to be huge or radical.

If your function takes parameters, and you assign a copy of those parameter values to a local variable, then later you need to change the data type of the function parameter, no longer do you have a typecast issue or need to make changes to the temporary locals to match. That's all.
 
Last edited:
<3 ReSharper.

In terms of using var, as topdog@OC says it's just syntactic sugar - makes no difference to the compiled code.
I like using it in a lot of situations as I just think it makes the code much cleaner and easier to read, as well as making some refactorings simpler.
If you don't like it you can always turn that one off in the ReSharper settings though.

If you've just got ReSharper then start learning the keyboard shortcuts, it really does make using Visual Studio much more of a pleasurable experience and soon you'll wonder how you ever did without it!
 
If you don't like it you can always turn that one off in the ReSharper settings though.

If you've just got ReSharper then start learning the keyboard shortcuts, it really does make using Visual Studio much more of a pleasurable experience and soon you'll wonder how you ever did without it!

I have just got Resharper and I'm still getting used to it. The right click menu has changed slightly, so I'm still getting to grips with it.

I think I will shut off that feature of the implicit declaration...I'm in the habit of declaring things explicitly and when recasting/converting types, I like to write code which explicitly does the conversion (rather than implicitly allowing the compiler to do this without me knowing).

One area where there seems to be a massive advantage is in For loops/counters and possible data type overflow errors.

Thanks for your input guys.
 
I tend to use var when the right-hand-side easily tells me what the type of object is, e.g.:
Code:
var a = new Class1(); - I know a will be a Class1 object, or
var b = a.CustomerCount - I know it should be integer.

If in doubt, just hover over the var keyword and it'll tell you what type it is.

I also find it very useful when looping over data because I don't have to find out exactly what type the enumerable returns, I let the compiler do it for me. Lazyness is key:).
Code:
foreach (var item in someOddMicrosoftClass.SomeOddReturnType()) {}
 
Here's another thing that ReSharper points out as a "problem" with my code:

It states that the following code breaks the naming rule:

Code:
int integerPartOfCustomerID = 4;
string stringPartOfCustomerID = zyz;

It doesnt like the idea of 2 consecutive upper case and wants to change the code to:

Code:
int integerPartOfCustomerId = 4;
string stringPartOfCustomerId = zyz;

Any opinions on this?
Should change the names of my variables (according to the above recommendation), in my entire project?
 
Click on the variable (integerPartOfCustomerID) and then click on the red lightbulb that Resharper brings up and select 'Add ID to the abbreviations lists'.
 
I always make an exception for names ending ID, even though it's not an acronym, and put both characters in caps as you had done. Just think it looks better as ID is generally very meaningful and so should stand out.
 
Back
Top Bottom