3

I have a method that looks like this:

Public Function NormalizeStreetAddress(country As Namespace.Country,
                                       streetAddress As Namespace.StreetAddress) _
                                   As Namespace.StreetAddress

        Dim _streetAddress As New Namespace.StreetAddress = streetAddress

        If My.Settings.Streeteable = True Then
            Dim _AddressCustom As New Namespace.AddressCustom
            _streetAddress = _AddressCustom.NormalizeStreetAddress(country, streetAddress)
        End If
        Return _streetAddress
End Function

I receive a streetAddress object, but inside the method I need to use another streetAddress object which I called _streetAddress — is that following the standard? A friend of mine told me that object names such as _yourNameObject are for global variables, but I can't find info about this and I want to make this method more readable.

gnat
  • 20,543
  • 29
  • 115
  • 306
Luis
  • 535

2 Answers2

6

The names of the variables must be explicit, i.e., when possible, the reader must know what is the variable just by looking at its name.

If you have both streetAddress and _streetAddress in the same code, there is a huge problem. Since the two variables are referring to something different, they should have different names.

Keeping both names is not only confusing, but extremely error prone. Would you be able to immediately make the difference between those two variables in six months when modifying some code in the middle of the method? If you type the wrong name and the program fails, would you be able to immediately see in the debugger where the error comes from?

What about:

Public Function NormalizeStreetAddress(country As Namespace.Country,
                                       streetAddress As Namespace.StreetAddress) _
                                       As Namespace.StreetAddress

    If My.Settings.Streeteable = True Then
        Dim _AddressCustom As New Namespace.AddressCustom
        Return _AddressCustom.NormalizeStreetAddress(country, streetAddress)
    Else
        Return streetAddress
    End If
End Function
1

This is largely opinion, the only thing I care about when it comes to variable names is consistency. You want to have a flow from your project that is continued in all its facets.

I don't see why it would matter what it's named as long as it's not actively confusing it's function.

Furthermore, I'd really like to see people take on the modified-Hungarian notation.

Seriously, people too often name their variables like this

EXAMPLE OF BAD VARIABLE NAMES:

int statistic;
float statistics;
string stats;

What's wrong with that? Well, it is telling me that it's a statistic, but do I really need to dig through the code to find all these definitions? It would be good practice to do so, but it's also nice for a programmer to think ahead about others reading his code.

Example of good variable names:

int nStatistic; //n prefix for ints
float fStatistics; //f prefix for floats
string sStats; //s for strings

Of course, whatever you do the most important thing is CONSISTENCY. I don't want to get reading through your code, and half-way down you decided to start naming the variables after a different pattern.

Mike
  • 109