Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide Programmatic Access to All Data Available in String Form: toString()

Bloch said: Provide Programmatic Access to All Data Available in String Form.

I am wondering if he means to override toString() which should involve 'all data available'?

I think the 'in string form' means that the string is for human reading, so override toString() is enough for the advice. Am I correct?

like image 472
卢声远 Shengyuan Lu Avatar asked Dec 17 '22 22:12

卢声远 Shengyuan Lu


1 Answers

No, apparently he meant quite the opposite of that. If a data member is available as part of the toString() output (or other string methods of the class), Bloch's fear is that developers using the API will rely on that and parse the strings to get at the underlying data values. His advice is to provide specific accessors for those data elements, to prevent developers from relying on the format of toString()'s output.

like image 98
Carl Manaster Avatar answered Jan 14 '23 02:01

Carl Manaster