That was a complete waste of time. To sum it up: don't rely on the format of a generic toString() method. Prefer country.getName() over country.toString(), even if toString() returns getName().
It's cool to read, but yeah, it's like reading Beginner's Java, lesson 3.
Never Use toString() for Behaviour
11–20 of 51 posts
Re: Never Use toString() for Behaviour
#12Re: Never Use toString() for Behaviour
#13Re: Never Use toString() for Behaviour
#14Never use a language that puts a toString() implementation on every object regardless of whether or not that object implements toString()
Serialization should require involve separate APIs.
Re: Never Use toString() for Behaviour
#15Never use a language that puts a toString() implementation on every object regardless of whether or not that object implements toString()
The ability to convert any object to a string is very useful for debugging and most languages support this. Even in C it's frequently useful to print %p to identify objects. Serialization should require involve separate APIs.
Re: Never Use toString() for Behaviour
#16Never use a language that puts a toString() implementation on every object regardless of whether or not that object implements toString()
The ability to convert any object to a string is very useful for debugging and most languages support this. Even in C it's frequently useful to print %p to identify objects. Serialization should require involve separate APIs.
[0] https://docs.microsoft.com/en-us/visualstudio/debugger/using...
Re: Never Use toString() for Behaviour
#17Earlier quoted context omitted.
The ability to convert any object to a string is very useful for debugging and most languages support this. Even in C it's frequently useful to print %p to identify objects. Serialization should require involve separate APIs.
It should be opt-in though. A compiler-flag for debug builds or similar.
Re: Never Use toString() for Behaviour
#18Earlier quoted context omitted.
It should be opt-in though. A compiler-flag for debug builds or similar.
Why so, what are the downsides? It's not like it introduces any meaningful overhead at all, and there are plenty of situations where it is useful even in production (eg for logging in catchall "can't happen" top level exception handlers). Adding yet another compiler flag seems like a worse solution to me.
I want to be able to design objects that have toString() and I want to be able to design objects that don't have toString().
The same goes for clone(), hash(), getPtr(), getEndian(), equals(), toBytes(), toJson(), encrypt(), delete(), isNumeric(), toXml(), serializationVersion() or compare().
Re: Never Use toString() for Behaviour
#19I am not too familiar with java, but should’t you call get() from optional to get Country from Optional and toString of Country will behave as expected.
Re: Never Use toString() for Behaviour
#20> If you used the returned Country to construct an API request including the name of the country, this will now produce strange countries named as “Optional[“Norway”]” I am not too familiar with java, but should’t you call get() from optional to get Country from Optional and toString of Country will behave as expected.