- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- A Dark Turn
- Nothing Doing
- Home By Another Way
- Coast Star
- Forsooth
- Epic
- The State of the Arts
- Planing ahead
- 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
Edit Admin
Some people really, really like to kill the environment with their most inefficient way to implement the simplest algorithms. By this point I no longer think it's stupidity, it feels more like an intentional crime against humanity.
Admin
Boolean.Parse is over the top, but I prefer the simple if-then-else above it. Easy to read, easy to maintain, and the compiler can optimize as necessary.
Edit Admin
In many places number of lines is still the main metric for coder productivity. Writing as obtuse and ineffective code as possible becomes the goal.
Admin
Boolean.parse("frist") is still one line. Perhaps this is a clever safeguard against #define true (random() < 0.999)!
Edit Admin
C# doesn't have macros by design, so that's not an option. Plus you cannot overwrite keywords.
Admin
if ((someCondition || someOtherCondition) ? Boolean.Parse("true") : Boolean.Parse("false")) { return Boolean.Parse("true"); } else { return Boolean.Parse("false"); }
Edit Admin
But such a #define is not possible in C#!
Admin
What The False?
Sometimes I wonder what truth can be returned by devs' minds.
Admin
Clearly,
true
andfalse
are magic constants and are to be avoided… e.g., by using clear name strings.Edit Admin
I'm not willing to dig into the docs this morning, but I wonder how well
Boolean.Parse("true")
works when theCurrentCulture
is not one of the "en-whatever" variants. IOW in French, German, Russian, Arabic, etc.Edit Admin
Any boolean comparison is unacceptable if it doesn't contain FileNotFound.
Admin
When I see stuff like this, I always wonder if it's generated code. I could imagine a code generator that produces something like "return {type}.Parse({value}). In fact, I was just looking at one yesterday. But if an actual, living person wrote this, I weep for my profession.
Edit Admin
So, you got me curious about that- I hadn't even considered that behavior. But according to the docs,
Boolean.Parse
does a comparison against the propertyTrueString
andFalseString
, which are "True" and "False" respectively. Per the docs, only "True", "true", "False", "false" will successfully parse- which is interesting, because it implies that it's not a simple case insensitive comparison.Edit Admin
Remember that strings are immutable in C# (.Net in general). And any strings that have the same value will share the same memory address.
This means that doing a simple compare vs "True" and "true" is just comparing against a couple of memory addresses and therefor very efficient.
Edit Admin
Edit Admin
No. Only interned strings share the same memory address:
https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUKgZgAIMTMB2PAbzyLpPXVvqvqObbtUwE4AKYJACmAOgAqAewDKggJZQA5nwCURALxqiAcSHAxwlcoDcHTt34AlIQDMhYIVADGQgKIBHCAEMANgGcBwuLScooqMNq6+hBChsambOZ8Vrb2Tq4ePv6C0UEyYPJKyuHcAAwiAJJQwHZQfDp6BspNcbhsAL6mpgAO+QBuntVkAGxkJREN0SrqAHzx9ABE2ULzQQCqXV12KgDaJQC6RIhE88LzJrhtQA
Using string.Intern on a string will search the intern pool to find the "True" used by System.Boolean, which then has the same address
Addendum 2024-09-26 09:46: String comparison does check memory addresses first, but if they aren't it does a char-by-char comparison (possibly vectorized).
Edit Admin
Pretty sure compilers are just as good as optimising the "return a ? b : c" version as the "if (a) then { return b; } else { return c; }" version. Possibly even better in some cases (it'll be fairly clear to the compiler (as well as to the reader!) that there's only one return path, for instance).
Edit Admin
It's a bit more complicated like that. For starters all literal and const strings are guaranteed interned (which applies to those boolean constants - actually, they are readonly references to literals for performance reasons, but that's another sack of beans).
Now during the old .net framework 1.x days the compiler interned strings very fast, usually at least before a string equality check happened. That's the reason why switch statements worked in C# while it was not possible in Java. Now with 2.0 they changed the string handling; turns out a lot people don't read the manual and did insane stuff like building stuff in an iteration using operators instead of StringBuilder. So from that moment on strings were actually rarely interned automatically and they put a lot effort into keeping features like the switch statement around without loosing to much of the previous performance.
Now one very important thing about string is that most of it is actually implemented directly by the compiler for releases, this is especially true for equality checks and how and when basic methods and operators apply. Things actually got a lot more complicated recently due to how interpolation is actually handled by the compiler. So just looking at the source code will not help you out there because it rarely applies and there is a ton of direct implementations done by the compiler circumventing using string.operator== or string.Equals ;-)
Edit Admin
re:
"turns out a lot people don't read the manual and did insane stuff like building stuff in an iteration using operators instead of StringBuilder"
One thing compiler writers and language committees need to learn is that NOBODY reads the manual. a = b + c is how people have been taught to add things together and joining strings is like adding them (in people's minds anyway). Telling them to use StringBuilder is not going to cut it. It's not nearly as intuitive as using +.