Coding What The…. Moments

From my Superfluous Code and Readability post where I was relaying information that I have seen recently with regards to my work.  Well there is a wonderful one that I am seeing right now, and my words were “What The”.

I was wondering WHY  yes emphasis on that, why date and time field in a data set were being reset to NULL and then when I am saving the data I am getting a, Start Date of NULL needs to be before the current End Date of [DATE].  I am going through my code, making sure I am setting it properly that it is only my code that is being changed and I am not overwriting the values being set for other conditions, but this one took the cask for me.

Private _DateTimeField As DateTime

Public WriteOnly Property DateTimeField() As DateTime
    Set (value As DateTime)
        _DateTimeField = value
    End Set
End Property

Pirvate Function StoreData()
    '...Various Code
    Database.Table.DateTimeField = DateTimeField
    '....More various Code
End Function

This on its own SHOULD cause a compile error only IF Option Explicit is turned on.  But it is turned of.  They have also turned off a lot of other compile options that would normally throw errors at compile time but now they are just warnings.

I am not really sure, WHY one would create a public write only properly.  I am sure it is there to bring the code more inline with C# and C++ which has you setting what the property does when you’re coding it.

So, yes this is the cause for a few days of drama thinking it was my code in someway causing this or not causing it as I was not setting the values.  Why was this happening…

I would recommend in this, do not turn off Option Explicit.  It is there to help strongly type the variables you use.  It has come from the early days of VB when putting Option Explicit at the top of each form, module, class, etc… meant that the variable needed to be declared before you could use it.   You could in all intents and purposes declare it as VB would if Option Explicit wasn’t used;

Dim x As Variant
Dim y As Object

One of these will be declared based on the language version used.  It is better to declare ALL variables before you use them, it is a matter of efficiency than anything. 

“The server my application is running on has 32GB of RAM why would I care about a measly x bytes of data saving for all the variables I use.”

Well, you could declare everything as object, garner the overhead of having objects everywhere, performing conversions to perform comparisons (This might explain a lot of the CTypes I have seen before in this same code).  But since all data types in .NET have inherited the Object class then isn’t there a benefit, not really. It is more efficient in any scenario to use the intrinsic data type than needing to perform conversions (or allow implicit conversions if that is turned on). 

My recommendation is, declare your variable, do not turn off Option Explicit to get around any errors at compile time you have because there might be hundreds of variables not declared.  Not only will you be doing the same thing that native language programmers have done, even C# you NEED to declare all variables before you use them but you will be making your code smaller in memory without the need for overhead to cater for any object that might populate that variable.


Posted on October 28, 2013, in Development, Tips and Tricks and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: