- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
They failed to return Constants.FILE_NOT_FOUND!
Admin
How about the fact that a method named
asUnicodedoes an HTML unescape?Admin
A null does, indeed, mean something different from an empty string. One is the absence of a value, while the other is an empty value. However ...
Regardless of how you name them, "optional" types are just disguised nulls, and therefore aren't any better than nulls.
Admin
Not to mention that the original author of the code would probably make a distinction between characters like 'W', 't', and 'f' on the one hand, and unicode characters on the other.
Admin
Optional types are not just nulls in fancy clothing.
Optional<Optional<T>>is different fromOptional<T>.Admin
I mean they're not disguised nulls, they're wrapped nulls. Calling them "disguised" like calling a burrito "disguised rice and beans". It's not disguised! It's put in a container to make it easier to consume!
Admin
Optional/nullable types are a form of documentation. If you're using them consistently, then you're telling the reader -- and the compiler -- which values are allowed to be null and which are not. By doing that, you can shift your null checks to build-time rather than run-time, which simplifies your logic and significantly decreases the risk of some random null-reference exception.
Admin
Plot twist: what if
Constants.NOT_AVAILABLEis actually just an alias for null? Then the null check in theasUnicode()function might actually be needed.Admin
I think the double null check is perfectly fine. Maybe
asUnicodewill get called by something else in the future that doesn't check for nulls. Better safe than sory.Admin
Absolutely. Changing something to Unicode is not the same as unescaping HTML. The fact the two are intermixed here makes me wonder what else the author is getting confused. Nulls and empty strings, maybe?
Admin
I'm not sure they are so dissimilar.
I'm not at all an expert on this language, but when I searched for the descriptions of
HtmlUtils.htmlEscapeandHtmlUtils.htmlUnescape. I happened upon a library where one flavor ofhtmlEscapecould be told to render an input string (presumably Unicode) into "US-ASCII" if that were the specified output encoding using&#xHex;as necessary. And wherehtmlUnescapewould "Turn HTML character references into their plain text UNICODE equivalent."So, if someone wanted to represent a Unicode string (presumably wide characters) into US-ASCII, they could have found this library to be useful. And it might even be an intelligent choice if the library was already in the dependency list for other uses, perhaps to sanitize user input.
Admin
Yes, the return of asUnicode in Unicode because htmlUnescape returns Unicode. However, it's still a poor indication of what the function actually does. It doesn't take ASCII string and convert them to Unicode. The only thing it does is take html escaped strings and convert those to Unicode unescaped strings. A really good name for this function would be "htmlUnescape", and maybe put it on a class named "HtmlUtils".
Since javascript is unable to handle any other type of string encoding than Unicode (specifically UTF-16), the idea of calling out a function as returning Unicode is redundant.
Admin
OK, I'm late to the comments today. How is it we've gone this far without a single comment about:
Sure, it's acceptable to write:
But a negative test with an else is either bad thinking or lazy thinking; it doesn't matter what programming language.