The Forkless Philosopher

Little Helpers


In an environment where errors are routinely logged it can be of great help if classes provide a method that dumps their current state in textform.
toString() should do exactly this: concatenate the values of all (and especially the private) member fields into one string that is easy to read if dumped into a textfile or written to a console window. In most cases a comma-separated list inside angular brackets will work just fine.
The idea is that whoever will be debugging code that uses your classes will have an easy way to dump data. If you as the classes' author do not provide a toString() method that also logs the values of private attributes, the other developer is restricted to public attributes or whatever getters you provided, and that might sometimes not be enough.

equals(const CYourClass& other)

A customized equals() method that includes comparisons of private attributes as well can greatly help with unit testing. Like with toString(), limiting comparisons to publicly available data might not always be enough and besides that, any concatenation of getters in an "assertEquals" will make the code of the unit tests close to unreadable and also more error prone. A decent equals() on the other hand makes for nice and clean and easy-to-read code that keeps the focus of the test on what is tested.