Click here to Skip to main content
15,890,336 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Be Kind, Rewind

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
12 Apr 2017CPOL2 min read 4.6K   1
Thoughts for writing code for the next developer to use (or how to apply the golden principle to code)

Introduction

The "golden rule" which appears in many guises in various religious texts and societal foundation stories can be characterized as "Do to others what you would want them to do to you" or in more secular terms, "Be kind: rewind".

What follows are a few suggestions that won't necessarily make your business object code better but will make it nicer to use - which will make the next developer that has to use it happier. This is important because it is statistically likely that the next developer will be you.

1. Add an override to ToString()

In .NET, the base "object" class has very few properties or methods but one it does have is the overridable ToString method. By default, your business object will return the name of the object type when this is called - but this hides all of the business meaningful content of the object.

This method can be explicitly called in code, but it is also used by the Visual Studio IDE when you hover over the object or if you are looking at a collection (IEnumerable) of them. Simply overriding the ToString method to put some business meaningful data (especially if the object has an inherent unique key field) makes this a good deal easier and faster to work with.

VB.NET
Public Overrides Function ToString() As String
    'Unique key is account number and currency
    Return Me.AccountNumber & " (" & Me.BaseCurrencyCode & " ) : " &  _ 
        Me.EndingCashBalance.GetValueOrDefault().ToString()
End Function

2) Implement Equality Checking

Another method of the base object class is Equals(obj) which allows you to test two instances of an object and make a determination as to whether they are equal or not. Out of the box, what this does is check that the two objects are literally the same object - i.e., they are stored in the same place in memory.

For a business object, there may be a more meaningful way of checking if two objects are the same - for example, if a unique transaction identifier matches, you might say the two transaction objects are equal.

There is a type safe (generic) interface called IEquatable that you can implement as well to make the equality testing type safe:

VB.NET
Public Function EqualsCurrency(other As ICurrencyIdentifier) As Boolean _ 
Implements IEquatable(Of ICurrencyIdentifier).Equals

    If (m_id = 0) AndAlso (other.Id = 0) Then
        Return m_code.Equals(other.Code)
    Else
        Return (m_id.Equals(other.Id))
    End If

End Function

You can then use this in your won override of the Equals(obj) method:

VB.NET
Public Overrides Function Equals(obj As Object) As Boolean
    Dim IOther As ICurrencyIdentifier = obj

    If (IOther IsNot Nothing) Then
        Return EqualsCurrency(IOther)
    Else
        Return False
    End If

End Function

Finally to make sorting faster, you can override the GethashCode() function:

VB.NET
Public Overrides Function GetHashCode() As Integer
    Return ToString().GetHashCode()
End Function

Others?

If you can think of anything else that should go in this tip, add it to the comments below...

History

  • 2017-04-12 - Initial thoughts

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 1096127713-Apr-17 9:27
Member 1096127713-Apr-17 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.