Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Be Kind, Rewind

0.00/5 (No votes)
12 Apr 2017 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.

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:

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:

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here