- 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
This is worse than ternary, or?
Admin
Yes this is worse : No this isn't worse ;
Admin
nice way to escape the pyramid of doom
Admin
Whoever wrote the code clearly thought that
decimal?
means "I don't know if it is adecimal
, it could be one or it could be anything" instead of a nullabledecimal
Or they didn't know what is even a
decimal?
, not to mention how to convert it todecimal
, and fumbled to that solution. Roughly like:decimal?
directly gets IDE yelling at them becausedecimal?
can't be implicitly cast todecimal
. But they're taught when converting an "unknown" type to number type you need toparse
it, so let's add that indecimal?
istoString
. So let's do thatnull
s give empty string ontoString
, which is not parsable. Or that the observed thatdecimal?
can benull
, and every enterprise coder is trained to handle that in advance, just in case. So let's do a null check. And becausedecimal?
is clearly a type from outer space, let's use the most generic== null
TRWTF: nullable types are apparently aliens?
Admin
TRWTF is of course calling a data type a "decimal".
No, "decimal" does not mean "a thingy with numbers in that's got like a dot (or a comma if you're Yoorpeen) somewhere in it". "Decimal" means "expressed in base 10 notation". Yeah, we get it, computer scientists are appalling mathematicians nowadays, but at least try to get your nomenclature correct.
Admin
Apparently he doesn't know about the null-coalescing operator (??) either.
return value ?? 0;
Admin
return comment ?? "";
Admin
TRWTF is that .NET doesn't have BCL monadic types like Maybe, instead they introduced the half-cocked Nullable.
And now .NET has immutable references, which still isn't quite as good as it could be.
Admin
That would not work because "value" is a "decimal?" while 0 is a "decimal".
Admin
You are TRWTF. You can chain methods on nullable types with
?.
, which is the same as monad chaining.And seriously, "monadic"? Are you on Haskell crack or something? Because nobody else would intentionally invoke that M word.
Admin
Nullable types are extremely useful. The notation with a question mark at the end "int?" is in fact syntatic sugar for the monad "Nullable<int>"
Admin
Ugh, one of my personal WTF triggers is functions named "Validate". That's about as descriptive as "DoStuff". Are you testing that a value is valid? Are you coercing something into a valid value? Are you doing something completely unrelated to the validity of a value but calling it validation anyway? IMO, "Validate" is just too ambiguous of a word to be a good descriptor.
Admin
I've most often seen this because people are afraid of exceptions, and want to return something they can use without null-checking.
So their code has to check for zero instead. Sigh.
The application I'm currently dismantling has this built into the bottom-most data layer, except that they set to min value for DateTime, int, long, decimal, etc.
So a lot of code like: "if field is DateTime.MinValue then print empty string, else format field", because that's a lot easier than using the built-in .HasValue and .Value parts.
Admin
Computers work pretty much like flipping a book page and changing the letters. All you have is on or off.
So floating point calculations, pretty soon you can hit the limits of the hardware.
I really do wonder with quantum computers, if they are really quantum, and if they are really computers, what would happen if LIGO started to detect something just in the right way that the network of quantum computers detect LIGO, and they get this cosmic feedback loop going ... ... ...
All because you didn't want to buy the cloud platform, boss.
Admin
The GetValueOrDefault() method and the null coalescing operator (??) are relatively recent additions to the language. Perhaps this was written before they were available? It's still a WTF...but maybe not quite as much.
Admin
I think P pretty well nailed the process behind this WTF. It's remarkable / terrifying how many devs seem to think that "correctness" begins and ends with "compiles with no errors (although maybe a warning or two)".
God forbid we should have people that are actually skilled at (much less rigorously trained in) their chosen profession. It's not like public safety, the entire world economy, and a whole lot more depend on this stuff. Oh wait ...
Admin
Training can be undone by changing the standards. Standards often change because people get caught up in a fight that's much bigger than them between two rivals who have been so bad, we had to build entire multinational corporations to contain them.
Being humble helps a lot. Walking away really is often the best choice. Don't let fear get you. Trust that a spoken tradition predates the Big Bang, and everything is taken for good cause, all the way down to the smallest particle.
Aim for mutual realignment. Turn good into better, and know that life really is a series of unending orthogonal turns if you do it right. Ask any good MD. You really don't have to die.
This makes your mind the most powerful thing that can be. There are no limits to the human mind. WE ARE EVERYWHERE NOW.
Admin
It is referring to a decimal as in a decimal fraction. Meaning a fraction where the denominator is a power of 10
Admin
Programmers: give them a string and they'll hang themselves with it.
Admin
Nope. A decimal means it's using a denominator that's a (typically negative) power of 10 rather than a power of 2. Thus 1/5 or 1/100 can be represented exactly and dividing by powers of 10 (very common in finance but also in metric conversions) doesn't introduce any rounding error.
Admin
There is a big difference between nullable types in .NET and Maybe types in sane languages: (T?)? == T?, but Maybe<Maybe<T>> != Maybe<T>.
Maybe means "add a new value to this type that's not used by anyone else". Nullable means "make sure this specific magic value is in the type".
If you are writing generic code in C#, you either have to avoid using T?, or you have to tell the user that T can't itself be nullable.
Admin
You know what they say, running your program is the same as
validating
whether your program works correctlyAdmin
There is another (potential) bug hidden here
decimal returnValue = 0; Decimal.TryParse(value.ToString(), out returnValue);
The assignment of 0 to returnValue is useless here, as Decimal.TryParse will ALWAYS overwrite the value of returnValue, even if the method returns false. out parameters HAVE to be written to by the method. In this case, 0 is what TryParse sets if it fails, but say you had the 'default' value set as 5 or something. That 5 would never survive the TryParse call!