Properties: explained at last



  • We recently welcomed a new hire to our team called, let's
    say, Vince. He was sold to us as a .Net expert. Just the guy to get us up to
    speed with this baffling new technology.

     

    Our initial misgivings over his actual .Net skills were
    recently assuaged by some work he checked into source control. Finally we
    have conclusive proof that Vince has truly got his head around this complicated
    ‘Properties’ concept that previously had us all so stumped.

     

    It turns out that, firstly, you declare a private member variable...

     

        Private _isValid As Boolean

     

    And then you define an accessor (ReadOnly in this instance) that returns you, oh, a global constant...?

     

        Public ReadOnly Property IsValid() As Boolean
           
    Get
               
    Return gcIsValid
           
    End Get
       
    End Property

     

    I’m glad he’s got that cleared up for us.

     

     

    The real WTF is VB, obviously...


  • @Gandudas said:

    The real WTF is VB, obviously...

    QFT.



  •  @Gandudas said:

    Public ReadOnly
    Property IsValid() As Boolean

    Maybe he is WAY beyond me but is it really neccessary to have the ReadOnly flag???  It's not like you can set a property that only has a get.



  •  Is there no checkin policy against this sort of junk?



  • @heffkat said:

     @Gandudas said:

    Public ReadOnly
    Property IsValid() As Boolean

    Maybe he is WAY beyond me but is it really neccessary to have the ReadOnly flag???  It's not like you can set a property that only has a get.

     

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.




  • @keigezellig said:

     Is there no checkin policy against this sort of junk?

     

     

    Vince is our expert, and I would never question him. It must be me who is wrong, and not this code.



  • @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

     

    It's been a while since I've done any VB.NET, but I think that's true. C# however, you don't need the readonly keyword on properties that don't have a setter.



  • That is correct.. VB uses the readonly as a "cross-check" to determine if the lack of a setter is intentional.



  •  How did he get hired? Did the hirer not ask him for recent code samples?



  • Based solely on the post, it could be as simple as a typo (which could occur in any language (i.e. using the wrong variable as the backing field)...

     

    My bigger question is not about the code per se, but rather about the process (e.g. testing, code review, etc) that allowed this code (again, possibly only a two character typo that can not be caught by the compilere - and is largely independent of language) into the system.



  • @TheCPUWizard said:

    Based solely on the post, it could be as simple as a typo (which could occur in any language (i.e. using the wrong variable as the backing field)...

     

     

     There is quite a body of evidence against this being a mere typo, perhaps my example could have better chosen, but I'll spare you a further attempt.

     @TheCPUWizard said:

    My bigger question is not about the code per se, but rather about the process (e.g. testing, code review, etc) that allowed this code (again, possibly only a two character typo that can not be caught by the compilere - and is largely independent of language) into the system.

    Indeed, indeed. There is a Process in place here, though lately it seems that some devs can be deemed so expert that they don't need to observe The Process.



  • @dhromed said:

     How did he get hired? Did the hirer not ask him for recent code samples?

     

     It would appear not. I am not party to the hiring process. Which is not to say I would do any better.



  • @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB.

    You don't need the readonly in C#.



  • @dhromed said:

    Did the hirer not ask him for recent code samples?
     

    I've never been asked that, not for ajob, nor for consultancy).

    I brought some code samples to my first IT employer (I think it was of a Tetris game I created in VB) but they mainly laughed at it (but hired me anyway); my current employer asked me to do a whiteboard exercise.

    Also, I'm not sure if you would catch Vince that way. It's an interesting idea, but I prefer the whiteboard exercise (what with Google and all that).



  • @dubbreak said:

    @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB.

    You don't need the readonly in C#.


     maybe TRWTF is that microsoft says that their .net libs are the same across different languages when they aren't.



  • @heffkat said:

    @dubbreak said:
    @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB.

    You don't need the readonly in C#.


     maybe TRWTF is that microsoft says that their .net libs are the same across different languages when they aren't.


    .net libraries != .net languages



  • @heffkat said:

    @dubbreak said:
    @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB. You don't need the readonly in C#.

     maybe TRWTF is that microsoft says that their .net libs are the same across different languages when they aren't.

    Net libs are not the same as the compiler, the compiler is the one nagging you about this so the TRWTF is VB and you because you don't know the difference between a compiler and a lib.



  • @heffkat said:

    @dubbreak said:
    @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB.

    You don't need the readonly in C#.


     maybe TRWTF is that microsoft says that their .net libs are the same across different languages when they aren't.

    I know people have already done this, but I want to pile-on too: this example has nothing to do with a difference in the libraries (which are the same across different languages-- you can import the VB6 compatibility libraries and use them in C# no problem.)



  • blakeyrat is correct (and more)...First this has nothing to do with any Library. Second the code generated is identical. Write a simple class with a property in your language of choice. Compile it. Use .Net Reflector to disassemble it...change source language...

     This is purely a difference in syntax. VB.Net has an interesting mix of explicit and implicit views (as does any language). On one hand it makes "assumptions"/"deductions" for places where it make it easier for the user. On the other hand, it requires more explicit (expressive?) syntax in areas where a user is likely to make a mistake.

     I work in about a dozen different languages on a regular basis. I have never had a case where there was a significant difference in effort, quality or any other metric between VB.Net and C# (at a strategic design/implementation level).

     



  • I remember one of the atrocities of code I committed in my innocent young days of learning VB.NET.

    I couldn't figure out how to make a Function (all I had known at that point was Sub), so I used ReadOnly properties with parameters instead.

    Forgive me, Father, for I have sinned.



  • @blakeyrat said:

    @heffkat said:
    @dubbreak said:
    @Gandudas said:

    I have asked Vince, and he tells me that without the ReadOnly flag, the 'compiler' will insist that we provide a Set.

    Then the real WTF is indeed VB.

    You don't need the readonly in C#.


     maybe TRWTF is that microsoft says that their .net libs are the same across different languages when they aren't.

    I know people have already done this, but I want to pile-on too: this example has nothing to do with a difference in the libraries (which are the same across different languages-- you can import the VB6 compatibility libraries and use them in C# no problem.)

    I also find the readonly modifier in C# very useful (on fields though, not on properties)



  • @bjolling said:

    I also find the readonly modifier in C# very useful (on fields though, not on properties)

    Just remember, that the coding guidelines discourage the use of this modifier with reference types (fields that are classes rather than structs) unless the item is immutable.



  • @TheCPUWizard said:

    @bjolling said:

    I also find the readonly modifier in C# very useful (on fields though, not on properties)

    Just remember, that the coding guidelines discourage the use of this modifier with reference types (fields that are classes rather than structs) unless the item is immutable.

     

    Makes sense to me.  I assume [code]const[/code] has the same restriction?



  • @powerlord said:

    @TheCPUWizard said:

    @bjolling said:

    I also find the readonly modifier in C# very useful (on fields though, not on properties)

    Just remember, that the coding guidelines discourage the use of this modifier with reference types (fields that are classes rather than structs) unless the item is immutable.

     

    Makes sense to me.  I assume <FONT size=2 face="Lucida Console">const</FONT> has the same restriction?

    Const is actually compiled into the code, and doesn't have a variable reference.  Because of this you have to use something that can be determined at compile-time, and is limited to basic objects (int, string, bool, etc)

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.