Debuggability
This chapter contains considerations about debuggability.
Always provide a toString() method
To make debugging easier, add a toString()
implementation to every class you introduce, even to internal ones. If toString()
is part of a contract, document it explicitly.
The following code is a simplified example from a graphical modeling area:
The output of this code is not very useful:
Neither is the information provided in the debug tool window:

To make both logging and debugging much more readable, add a simple toString()
implementation like this:
This results in improved output:

Consider implementing toString()
even if you don't think the class is going to be printed anywhere, as it can help in unexpected ways. For example, inside builders, it may be important to see the current state of the builder.
The intended use of the code above is the following:

If you set a breakpoint on the line after the closing brace of the first child
(as on the picture above), you see a non-descriptive string in debug output:

If you add a simple toString()
implementation like this:
The debug data becomes much clearer:
You can also see immediately which fields are set and which are not.
What's next?
Learn about APIs' backward compatibility.