Re: Are people abusing "var"?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"MC" <for.address.look@xxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le message de groupe de discussion : OlGSAPzTJHA.5024@xxxxxxxxxxxxxxxxxxxxxxx
"Michael B. Trausch" <mike@xxxxxxxxxx> wrote in message news:20081125130745.613db209@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Tue, 25 Nov 2008 15:19:24 -0000
"Mark Rae [MVP]" <mark@xxxxxxxxxxxxxxxxx> wrote:

Even though C# makes a reasonable stab at choosing the most
appropriate datatype
(http://msdn.microsoft.com/en-us/library/bb384061.aspx), I prefer to
tell it what I mean explicitly rather than allow it to decide what it
thinks I mean implicitly...

Absolutely. This is one of the reasons that I choose to use statically
typed languages at all: not using some mechanism like "var" eliminates
many classes of code by specifying _precisely_ what you want. While
"var" is easily figured out by the computer, it's far easier for a
human to see "int" or "string" or even "object", if it must be.

[ "many classes of code" => "many classes of coding errors" ? ]

Right! By declaring types, you make sure the compiler knows what YOU are thinking, so if you make a mistake later on, you'll be warned!

This was one of the key insights in the original design of Pascal (which, despite superficial differences, is an important intellectual ancestor of C#): Let the compiler help you keep >track of what YOU think the data types and structures should be.

But this is not an issue with auto.

Var doesn't mean variant, and the object is still strongly typed. If the compiler can deduce what you mean without any ambiguity, why should you still need to say it ?

Another thing to consider is that var can only be used locally. So, if the reader has a problem to understand what is the real type of the object (let's suppose the intellisens won't tell him), i guess there is something very wrong either with the function, either with the reader.

And because the object will be very local, to know preciseley what is the type of the object will be useless most of the time, as long as the compiler can do its type checking.

.



Relevant Pages

  • Re: No compiler warning for uninitialized variable passed to var parameter !!!
    ... Test no compiler warning for uninitialized variable passed to var parameter ... version 0.01 created on 26 april 2008 by Skybuck Flying ... DoYouFeelLuckyPunkQuestionMark; // NO WARNING, BAD!!! ... So why would the compiler think that "Lucky" needs to be initialised before calling the function? ...
    (alt.comp.lang.borland-delphi)
  • Re: Conversion troubles
    ... function TfrmMain.StripValue(str: String): int64; ... Var tStr: String; ... The compiler gives you a hint that Resultaat is assigned but never used, ...
    (alt.comp.lang.borland-delphi)
  • Re: Lets organize our Delphi code
    ... the inhibitions to optimization are revealed (e.g. because of the lifetimes ... compiler will put most inner vars into CPU registers. ... calculates the scope by determining the first and last occurance of the var. ... describe with inline nested procedures is roughly the same, ...
    (borland.public.delphi.non-technical)
  • Re: A remobjects wtf...
    ... // The compiler takes care of cleaning up here even if an exception was raised ... There is no exception handling necessary here, because the garbage collector will do the hard work, I don't like GC though;-). ...
    (borland.public.delphi.non-technical)
  • Address of .globl _start
    ... .globl var ... and got output with echo $? ... (reads %ebx register) ... After that I compiled test.s with 'as' compiler: ...
    (Linux-Kernel)