Re: Are these good features to use?

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



On Mon, 22 Sep 2008 10:16:57 -0700, David Thielen <thielen@xxxxxxxxxxxxx> wrote:

The first is where I can do "var x = new MyObject()" instead of
"MyObject x = new MyObject()". This strikes me as a step back to Basic
as it's good to explicitly declare what I'm creating. Am I just being
too resistent to change? Or do others find this not a feature to use.

It's a matter of taste. I'm resistant to change :) and thus I prefer the "old school" declaration style. Others argue that using "var" minimizes characters to type and doesn't lose any semantic information. The latter is true, as long as you're initializing the variable in the simplest way, by calling a constructor for the type, which means that the name of the type is in fact in the same statement.

Personally, I prefer consistency over efficiency. In programming, I've run into numbers of errors due to an attempt to be efficient, but I've never run into any serious error due to an attempt to be consistent. You can't use "var" without losing human-readable type information in all scenarios and it's not even legal in other scenarios, so there will always be some variables that should or must be declared with an explicit type.

That means that at least some of the time, you'll have to look to the precedent of the variable name to know the type of the variable. Since I would rather know that if I'm going to have to look there some of the time, I can look there _all_ of the time, I go ahead and always use an explicit type.

The exception being the situations for which "var" was actually designed: anonymous types, inferred by the compiler from the initializer of the variable. Obviously, there's no valid type name that could be used and "var" has to be used.

My second is the auto-properties where a private variable is just
marked as a property. O see this as being more useful, but again I
think having it written in code makes it clearer. What do people who
are using this think of it?

I use it when I can. Honestly, it doesn't come up that often; adding code to a property getter or (especially) the setter is just too useful. But when you're truly just getting and setting a value with no additional work, it's not to not have to write everything out explicitly.

I can't imagine how writing the property out explicitly could be clearer than the auto-property syntax. If anything, the verbosity gets in the way of understanding the property. With the auto-property, it's an easily recognized pattern and reading it you immediately know everything there is to know about the property. With the drawn-out version, you have to scan more code just to determine that the property does nothing other than getting and setting a single value.

Note by the way that it's not a matter of having "a private variable just marked as a property". The auto-properties creates a real property. It's not just marking some variable as a property. There's underlying storage that is a separate private variable created by the compiler for the purpose.

Pete
.



Relevant Pages

  • Re: option explicit
    ... Explicit", it might be moments of debugging time instead of hours. ... And one more reason I like it is that when I declare an object explicitly: ... dim myWksThatShowsTheSummary as Worksheet ... I can type mywks and hit ctrl space bar to have the VBE autocomplete (or show me ...
    (microsoft.public.excel.misc)
  • Re: Must declared length of a character function be matched by any procedure declaring it? YES
    ... length that any routine that used the function had to declare it to be ... end function bigfunc ... The reason that the error is not detected is that there is no explicit ... a character function can return a string ...
    (comp.lang.fortran)
  • Re: Garbage collection
    ... As you know very well, a GC has advantages and drawbacks, both technical and human ("no free beer" (TM)), so, imho, there are sometimes situations in which a GC is recommended, but sometimes not. ... An on/off global switch per project is in fact rather a one-way road (tell me, what would you do if you have a project with GC enabled and you decide that for eg. speed reasons, avoiding sloppy code aso. ... That's why I tried to propose an approach to allow the developer to explicitly declare what's GCed or not by using both inclusion and exclusion filtering declarative approach. ... Imho, the GC situation now is something similar with 'var' declaration explicit in Pascal, implicit in VB. ...
    (borland.public.delphi.non-technical)
  • Re: lang effort: type conversions
    ... inferencer for the lang I am implementing, I have started to consider some ... Yes I have came to the same idea of using a variant type with either ... explicit declaration or invisible syntatic sugar like you are in functions. ... will declare a mutable one. ...
    (comp.lang.misc)
  • Re: use of var
    ... long before "var" was even introduced. ...   SetFocus; ... then, logically, you should also either avoid the original chaining ... style entirely, or at least use explicit casts after every operation, ...
    (microsoft.public.dotnet.languages.csharp)