The new GOTO Statement?
-
Well, to make it a bit shorter here, I'll certainly have some thoughts about the severity of readability by slower type deduction. I've haven't heard complaints of colleagues so far, but let's see when asking them directly about it. Though, that the purpose of the var keyword, as intended by the language designers, is solely for anonymous types - I have to agree even less now, seeing how common place it is in examples in the language specification document. Not only should they have refrained from such examples if they didn't intend for it being used such ways, but actually they could easily have restricted the use of the var keyword for ananoymous types (you suggested a warning there). If it's so glarinlgy obvious how "bad" this is, I think they otherwise careful language designers would have seen it themselves. As fiercely as some people here claim that anon types is the "actual only intention" for this - it now seems even more like pure opinion and taste. Just like curly braces position... which people fight as fiercely over, heh.
Steve#2 wrote:
Though, that the purpose of the var keyword, as intended by the language designers, is solely for anonymous types - I have to agree even less now, seeing how common place it is in examples in the language specification document.
This is debatable. The documentation might have been written by the same as designed. The use of
var
could've been used for the sake of shortness of documentation and to focus on the main subject of the excerpt.
Steve#2 wrote:
If it's so glarinlgy obvious how "bad" this is, I think they otherwise careful language designers would have seen it themselves.
Well, we have a whole Code Horrors section on code project because language designers cannot prevent bad use of a language. Not saying that
var
in your example is a horror.
Steve#2 wrote:
As fiercely as some people here claim that anon types is the "actual only intention" for this - it now seems even more like pure opinion and taste.
Just like curly braces position... which people fight as fiercely over, heh.Maybe you're right, but the purpose they were created for are the anonymous types. Maybe they thought along the way, oh this might be useful "for this thing here" too! The
var
usage can come to opinion and taste at some point, but I still think that readability should be improved when possible, instead of just usingvar
because it's easier. Oh, the curly braces... That's a never ending war.To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Steve#2 wrote:
Though, that the purpose of the var keyword, as intended by the language designers, is solely for anonymous types - I have to agree even less now, seeing how common place it is in examples in the language specification document.
This is debatable. The documentation might have been written by the same as designed. The use of
var
could've been used for the sake of shortness of documentation and to focus on the main subject of the excerpt.
Steve#2 wrote:
If it's so glarinlgy obvious how "bad" this is, I think they otherwise careful language designers would have seen it themselves.
Well, we have a whole Code Horrors section on code project because language designers cannot prevent bad use of a language. Not saying that
var
in your example is a horror.
Steve#2 wrote:
As fiercely as some people here claim that anon types is the "actual only intention" for this - it now seems even more like pure opinion and taste.
Just like curly braces position... which people fight as fiercely over, heh.Maybe you're right, but the purpose they were created for are the anonymous types. Maybe they thought along the way, oh this might be useful "for this thing here" too! The
var
usage can come to opinion and taste at some point, but I still think that readability should be improved when possible, instead of just usingvar
because it's easier. Oh, the curly braces... That's a never ending war.To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
Fabio Franco wrote:
Well, we have a whole Code Horrors section on code project because language designers cannot prevent bad use of a language.
Yeah I know that language design, or the design of anything for that matter, will not yield perfect and guarandteed fool-proof concepts just when something new is introduced. Still I have the impression that the C# team knows what they're doing (although some people complain about this or that, or that new things like functional langue concepts are included... :-D ) I found this MSDN page: http://msdn.microsoft.com/en-us/library/bb384061.aspx[^] containing the section: _"The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is
IEnumerable>
. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity."_ So, while they do hint at this usage could be a disadvantage for maintenance, they agree that it doesn't have to - if it's no problem for anyone in the team. Still, if there may other members being added to the team some point later, this may change, and the guy may have a little harder start ^^ While it'd be crazy to try to guard against anything that may or may not happen in the future, and should do what works best for the current team, I think, I now do get the feeling that perhaps it's not too bad an idea to not go too crazy with the var thing. But it's quite interesting, any way. I guess I'll examine some of my code and see how I'm really using it heh :-D And whether the types may be obvious, e.g. because say, in a function that takes a few "decimal" parameters, and only calculates some stuff with decimal all the time, it should not be too hard to figure that all the var stuff in between would be decimal, too. I think this is actually the main occurence of my "var", besides queries of course (which I mostly use for non-database stuff because, laziness can be good :-D )
-
Fabio Franco wrote:
Well, we have a whole Code Horrors section on code project because language designers cannot prevent bad use of a language.
Yeah I know that language design, or the design of anything for that matter, will not yield perfect and guarandteed fool-proof concepts just when something new is introduced. Still I have the impression that the C# team knows what they're doing (although some people complain about this or that, or that new things like functional langue concepts are included... :-D ) I found this MSDN page: http://msdn.microsoft.com/en-us/library/bb384061.aspx[^] containing the section: _"The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is
IEnumerable>
. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity."_ So, while they do hint at this usage could be a disadvantage for maintenance, they agree that it doesn't have to - if it's no problem for anyone in the team. Still, if there may other members being added to the team some point later, this may change, and the guy may have a little harder start ^^ While it'd be crazy to try to guard against anything that may or may not happen in the future, and should do what works best for the current team, I think, I now do get the feeling that perhaps it's not too bad an idea to not go too crazy with the var thing. But it's quite interesting, any way. I guess I'll examine some of my code and see how I'm really using it heh :-D And whether the types may be obvious, e.g. because say, in a function that takes a few "decimal" parameters, and only calculates some stuff with decimal all the time, it should not be too hard to figure that all the var stuff in between would be decimal, too. I think this is actually the main occurence of my "var", besides queries of course (which I mostly use for non-database stuff because, laziness can be good :-D )
Steve#2 wrote:
So, while they do hint at this usage could be a disadvantage for maintenance, they agree that it doesn't have to - if it's no problem for anyone in the team.
I guess this is the most important aspect to be taken into account, althogh personally, I wouldn't instruct my team to use
var
for these cases.Steve#2 wrote:
While it'd be crazy to try to guard against anything that may or may not happen in the future
Yes, but I think it's wise to do our best to do so. A project could be handed to whole different team, or even, a different company. While I don't think using var on the example you mention is the end of the world, it could prove to be a pain in the ass if an entire code base needs to be studied. Imagine the cases where developers act as C# only had the var keyword. :~
Steve#2 wrote:
laziness can be good :-D )
Yes :), specially when it means significant gains in productivity. Related to var, I find it specially true when using Linq to objects that can save a lot of time while querying object collections.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Nah,
var
has its uses... I basically use it in two situations: 1) Triggering simple events, just because it's glue code that I don't care about:private void OnDownloaded()
{
var evt = DataDownloaded;
if (evt != null)
evt(this, EventArgs.Empty);
}- Eliminate long, redundant declarations, when there's nothing fancy going on... I know, some people would rather see the type on the left side, but I happen to find this a LOT more readable:
var modelTemp = new ViewModels.GridTree.ViewTableModel(view);
I actually don't use it for LINQ unless the generated types are really nasty... LINQ types aren't "obvious", so I'd rather see exactly what they are... And if the returned type is a Lookup of Dictionaries of HashSets of Arrays of Strings, then that's a signal to refactor, not a reason to use
var
. Just eearched my code and found 79 uses of thevar
keyword... Except it looks like about 75% of them are in my snippet-generated OnPropertyChanged functions (INPC in models)... Another 20% or so are in third-party code, and the rest are more of the two items I listed above. ... Speaking of third-party code... Just glanced at a few of thevar
hits from one of the controls I scavenged, and found:var stepSize = (int)value;
... That's just unnecessary...Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian Shlasko wrote:
- Triggering simple events, just because it's glue code that I don't care about:
Assigning to var in this case is completely unnecessary, why not check the
DataDownloaded
directly and avoid a completely unnecessary variable assignment?
Ian Shlasko wrote:
- Eliminate long, redundant declarations, when there's nothing fancy going on... I know, some people would rather see the type on the left side, but I happen to find this a LOT more readable:
You may find it more readable, but ask anyone else if they can guess what's the return type of your method. :doh:
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Ian Shlasko wrote:
- Triggering simple events, just because it's glue code that I don't care about:
Assigning to var in this case is completely unnecessary, why not check the
DataDownloaded
directly and avoid a completely unnecessary variable assignment?
Ian Shlasko wrote:
- Eliminate long, redundant declarations, when there's nothing fancy going on... I know, some people would rather see the type on the left side, but I happen to find this a LOT more readable:
You may find it more readable, but ask anyone else if they can guess what's the return type of your method. :doh:
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
Fabio Franco wrote:
Assigning to var in this case is completely unnecessary, why not check the
DataDownloaded
directly and avoid a completely unnecessary variable assignment?
Simple... Multithreaded system... What if, right after checking if the event is null, the last handler unhooks from it on another thread? As I understand it, the temporary variable avoids that issue.
Fabio Franco wrote:
You may find it more readable, but ask anyone else if they can guess what's the return type of your method.
No, I said redundant... Like
List, Something>> blah = new [same thing]();
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Fabio Franco wrote:
Assigning to var in this case is completely unnecessary, why not check the
DataDownloaded
directly and avoid a completely unnecessary variable assignment?
Simple... Multithreaded system... What if, right after checking if the event is null, the last handler unhooks from it on another thread? As I understand it, the temporary variable avoids that issue.
Fabio Franco wrote:
You may find it more readable, but ask anyone else if they can guess what's the return type of your method.
No, I said redundant... Like
List, Something>> blah = new [same thing]();
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian Shlasko wrote:
Simple... Multithreaded system... What if, right after checking if the event is null, the last handler unhooks from it on another thread? As I understand it, the temporary variable avoids that issue.
Then you're doing it wrong. For multi-threaded applications you should either use lock, semaphore or mutex. Depending on the scenario. This "workaround" could have undesirable results.
Ian Shlasko wrote:
No, I said redundant...
You mean this?
var blah = new List, Something>>();
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Ian Shlasko wrote:
Simple... Multithreaded system... What if, right after checking if the event is null, the last handler unhooks from it on another thread? As I understand it, the temporary variable avoids that issue.
Then you're doing it wrong. For multi-threaded applications you should either use lock, semaphore or mutex. Depending on the scenario. This "workaround" could have undesirable results.
Ian Shlasko wrote:
No, I said redundant...
You mean this?
var blah = new List, Something>>();
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
Fabio Franco wrote:
Then you're doing it wrong. For multi-threaded applications you should either use lock, semaphore or mutex. Depending on the scenario. This "workaround" could have undesirable results.
Ever tried binding a WPF GUI to an INPC class with virtualized ItemsControls? That thing hooks and unhooks the PropertyChanged event so much, I could apply a few X-rated analogies to it :) Just not worth the trouble of locking.
Fabio Franco wrote:
You mean this?
var blah = new List<Dictionary<Tuple<int, string>, Something>>();
Exactly.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Fabio Franco wrote:
Then you're doing it wrong. For multi-threaded applications you should either use lock, semaphore or mutex. Depending on the scenario. This "workaround" could have undesirable results.
Ever tried binding a WPF GUI to an INPC class with virtualized ItemsControls? That thing hooks and unhooks the PropertyChanged event so much, I could apply a few X-rated analogies to it :) Just not worth the trouble of locking.
Fabio Franco wrote:
You mean this?
var blah = new List<Dictionary<Tuple<int, string>, Something>>();
Exactly.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian Shlasko wrote:
Ever tried binding a WPF GUI to an INPC class with virtualized ItemsControls?
Thank God no
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Steve#2 wrote:
And it's exactly that type of scenario the "var" keyword was made for
What "scenerio" is that? You don't want to type more than you have to, so you leave it to the next developer to figure out what the type is?
If it's not broken, fix it until it is
-
ah, you mean the using ^^ Haven't seen this usage of it, wow, yet another meaning of the keyword "using", I think now that's quite an abuse... (language design wise)
I agree,
using
was probably a poor choice given it already has a couple of definitions. Atypedef
would be a better choice, but then again my first languages were C and C++ so I am biased.