6 ms·
Interesting to see 'var' isn't used as much as I thought it would be.
by mulrian 9y ago
Interesting to see 'var' isn't used as much as I thought it would be.
- matthewwarren 9y agoYeah, I thought the same. I now realise the better test of 'var' would be to compare it to how many times it's not used. That is count the times that 'var myClass = new MyClass()' is used v 'MyClass myClass = new MyClass()'. But my regex skills aren't good enough for that and I'm not even sure if you can do it purely in a regex? Also I imagine that 'var someNumber = 1' is used less that 'int someNumber = 1', but again, I'm not sure how to do the regex? I certainly only use 'var' when the same word is on both sides of the '=', so not in the case of int/double/decimal etc.
- mattmanser 9y agoI still use var in that scenario, because if you need different types you have to suffix numbers with an identifier anyway: var i = 1; //int var j = 1f; //float var k = 1m; //decimal var l = ""; //string var m = ''; //char
- matthewwarren 9y agoYeah, I know you can do that, but I can never remember which suffix is which ;-)
- lukepothier 9y agoThere are a few more for numeric types: var integer = 1; var long = 1l; var float = 1f; var double = 1d; var decimal = 1m; var unsignedInteger = 1u; var unsignedLong = 1ul;
- mattmanser 9y agoI use it religiously now, so much quicker to program with imo as you don't need to find out the return type. Surprised it's less used than async/await though, which I think MS massively overhypes the usefulness of (and is annoyingly making a lot of classes have async behaviour by default which can result in very weird random hangs). Although I imagine a significant number of class declarations would contain no var or async/await simply because of their nature.
- weavie 9y agoBack when I used to work at a large enterprise the use of 'var' was banned in our coding standards. Using Linq was heavily frowned upon. It took quite a few years before it started to be accepted.. At one point we were also strongly encouraged to place a #region around every single method! I never understood why, and luckily that one did get dropped.
- matthewwarren 9y ago> At one point we were also strongly encouraged to place a #region around every single method! I never understood why, and luckily that one did get dropped Shudder!! Especially since VS has plugins (or maybe it's built-in?) that will let you fold/hide individual methods, if you really want to do that!
- acidshards 9y agoFYI: It's built-in.
- romanovcode 9y agoWhat's the point of not using stable language features? I don't get it.
- weavie 9y agoNeither did I.
- Cakez0r 9y agoI've (unfortunately) had to work with this rule before. The justification was that it makes the code more readable. I can understand banning SQL style linq and allowing only functional style, but I guess that's more of a personal preference.
- JustSomeNobody 9y agoWhile it shouldn’t be banned, it should be used appropriately. We have some areas of our project that communicate with hardware where we cannot use it because of the performance hit.