Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Avoiding Bugs caused by not Overriding ToString

Tags:

c#

tostring

I find the following bug occurring far too often in my code and wondered if anyone knows some good strategies to avoid it.

Imagine a class like this:

public class Quote
{
   public decimal InterestRate { get; set; }
}

At some point I create a string that utilises the interest rate, like this:

public string PrintQuote(Quote quote)
{
    return "The interest rate is " + quote.InterestRate;
}

Now imagine at a later date I refactored the InterestRate property from a decimal to its own class:

public class Quote
{
    public InterestRate InterestRate { get; set; }
}

... but say that I forgot to override the ToString method in the InterestRate class. Unless I carefully looked for every usage of the InterestRate property I would probably never notice that at some point it is being converted to a string. The compiler would certainly not pick this up. My only chance of saviour is through an integration test.

The next time I call my PrintQuote method, I would get a string like this:

"The interest rate is Business.Finance.InterestRate".

Ouch. How can this be avoided?

like image 701
cbp Avatar asked Jun 29 '09 00:06

cbp


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

By creating an override of ToString in the IntrestRate class.

like image 177
MiffTheFox Avatar answered Oct 05 '22 09:10

MiffTheFox


The way to prevent this kind of problem is to have a unit test for absolutely all your class members, which therefore includes your PrintQuote(Quote quote) method:

[TestMethod]
public void PrintQuoteTest()
{
    quote = new Quote();
    quote.InterestRate = 0.05M;
    Assert.AreEqual(
        "The interest rate is 0.05",
        PrintQuote(quote));
}

In this case, unless you defined a implicit conversion between your new InterestRate class and System.Decimal, this unit test would actually no longer compile. But that would definitely be a signal! And if you did define an implicit conversion between your InterestRate class and System.Decimal, but forgot to override the ToString method, then this unit test would compile, but would (correctly) fail at the Assert.AreEqual() line.

The need for having a unit test for absolutely every class member cannot be overstated.

like image 20
Mike Rosenblum Avatar answered Oct 05 '22 09:10

Mike Rosenblum