Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Brogrammer: *not sure if string, better call toString*
TheRestOfUs: (┛ಠ_ಠ)┛彡┻━┻ -
I feel like toString on strings should just be forbidden to be called by compilers and interpreters.
-
Although, in the case of JavaScript, lazy typing might prevent that from being a good practice.
-
@kescherRant
That would break encapsulation in unintuitive ways :) more a style thing in this case. -
Perhaps there is a corner case where it returns a non-string. So it is a catch-all. Maybe it was easier than tracking down the corner case.
-
Kids : *calls toString() on objects*
Men: *calls toString() on String*
Legends: *Calls toString() on null*
Ultra legends : calls (""+null).toString() -
I call ToString() 3 times on each string, just to make sure it's a string. It's good practice.
-
@AlmondSauce It's not like that computer has anything better to do anyway...
-
Well if you could assign values to an element of a string we probably wouldn't have this shit.
-
cprn16605yWhat language? Does it have strict typing? Does it guarantee input not being null when strict typed? Or does it only cast zero characters for overloading purposes? Because e.g. C#:
> string s = null;
> IPAddress i = null;
> Console.WriteLine(object.Equals(s, i)); // prints "True"
> Console.WriteLine(object.ReferenceEquals(s, i)); // prints "True"
I understand the rant but it's not always obvious you have a string in the first place even if your IDE and static analyser claim you do. Also, `toString()` might be doing something different than what you think, e.g. presetting an overload without casting type so... all and all, it's safer to call that extra casting. Not saying I do it, I hate future-proofing. Saying I understand why experienced coders sometimes choose to do it.
To all the programmers calling “ToString()” on variables that are already of type String....
Please stop.
rant