Evaluating Perks

by in CodeSOD on

Today's anonymous submitter works for a company that handles customer rewards perks. It's handling thousands of dollars of transactions a day, which isn't a huge amount, but it's certainly non-trivial.

Now, there's a conversion formula from points to dollars: points/100*1.7. Now how would someone implement this complex formula in PHP? Well, our submitter's predecessor did it this way:


TrUe Romance

by in CodeSOD on

Mario's predecessor was writing some Java code that parsed textual input. Thus, it needed to be able to turn the string "true" into the boolean value true, but also needed to handle "True" and "TRUE".

Now, in Java, the obvious answer would be to use the equalsIgnoresCase String member function. if (value.equalsIgnoresCase("true"))…. I mean, obvious to you or me, anyway.


Capitalizing on Memories

by in CodeSOD on

Gavin inherited some "very old" C code, originally developed for old Windows systems. The code is loaded with a number of reinvented wheels.

For example, they needed to do a case insensitive string comparison. Now, instead of using stricmp, the case insensitive string comparison, they wrote this:


A Brand New Bag!

by in Error'd on

We've all been watching programming gaffes come and go, and they're almost always some variation on a set of usual themes I've been trying to classify and name. But today, we've got one for you that is amazingly, excitingly, fabulously novel! I'm calling this one ecrash unless somebody's got a better idea.

Not to keep you waiting any longer than absolutely necessary, here's Ian M. with this week's winner: "I don't even know how you do search like this, it's somehow both way more complicated and way more unsettling then it needs to be."


Input Validation is a Sure Thing

by in CodeSOD on

Validating inputs matters. It's also a challenge. Validating that an input is numeric might be easy, but validating an email address is orders of magnitude harder (and technically isn't a regular language and thus can't be parsed by regex, though you can get close). Validating a URL is also a pretty challenging task, since URLs can contain all sorts of surprising information.

Daniel's co-worker, when tasked with validating URLs, looked at the complexity, and came up with a simple, elegant solution, in JavaScript.


A Caught Return

by in CodeSOD on

When John takes on a new codebase, he always looks for low-risk ways to learn the code by changing it. Things like beefing up the unit tests, tracking down warnings that have been left to languish, minor quality-of-life changes.

Well, a few years back, John inherited some C# code, and started tracking down some warnings. That lead to this method.


Free From Space

by in CodeSOD on

Henrik H is contracting with a client, and that client uses a number of other contractors. Some of them have… interesting approaches to problem solving.

For example, one of the servers is a Windows server, which stores a lot of temp files on the D: drive. So someone needed to write a C# function that would check the available space, and if it exceeded some threshold, delete the temp files.


Zero Failures

by in CodeSOD on

Parsing strings into other data types is always potentially fraught, what with the edge cases and possible errors. This is why most languages provide some kind of helper methods that try and solve those hard problems.

C# has a number of them. One, for example, would be Int32.Parse- it attempts to parse a string into an integer, and throws an exception when it fails. Similarly, there's an Int32.TryParse function, which avoids throwing an exception and returns an error code instead.


Archives