Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Nov 2023

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.


Lines of Code

by in CodeSOD on

Brittany is a game developer, and frequently ends up working on contracts for other companies. One company wanted her to add some features to their trading-card based game, and they offered her an option: she could either do a fixed-rate contract or a paid-per-line contract.

Brittany went with the fixed-rate.


Limited Space

by in CodeSOD on

While XML is a complicated specification, and incredibly bureaucratic and verbose, it's also powerful enough that many languages, from Java to Python, have XML helper classes in their standard library.

C# is one of those languages. But just because there's a built-in library (and a wealth of 3rd party libraries with richer features) doesn't stop people from reinventing the wheel.


Constant Adventure

by in CodeSOD on

We know that June 7th, 2006 was a long day for Jonas, Rusty's long-ago predecessor. We know that, because Jonas made a big commit that day. It was the day someone told him to stop using magic numbers and switch to named constants.

    public static final float FLOAT_0NE_HUNDRED_FIFTY = 150.0f;
    public static final float FLOAT_EIGHT = 8.0f;
    public static final float FLOAT_EIGHTY_FIVE_HUNDREDTH = 0.85f;
    public static final float FLOAT_EIGHT_HUNDREDTH = 0.08f;
    public static final float FLOAT_EIGHT_HUNDRED_SIX = 806.0f;
    public static final float FLOAT_TEN_HUNDREDTH = 0.10f;
    public static final float FLOAT_ELEVEN = 11.0f;
    public static final float FLOAT_ELEVEN_HUNDREDTH = 0.11f;
    public static final float FLOAT_FIFTEEN = 15.0f;
    public static final float FLOAT_FIFTY = 50.0f;
    public static final float FLOAT_FIFTY_NINE_HUNDREDTH = 0.59f;
    public static final float FLOAT_FIVE = 5.0f;
    public static final float FLOAT_FIVE_HUNDRED = 500.0f;
    public static final float FLOAT_FIVE_HUNDRED_SIXTY = 560.0f;
    public static final float FLOAT_FIVE_TENTH = 0.5f;
    public static final float FLOAT_FIVE_THOUSANDTH = 0.005f;
    public static final float FLOAT_FORTY = 40.0f;
    public static final float FLOAT_FOURTEEN_HUNDREDTH = 0.14f;
    public static final float FLOAT_FOUR_HUNDRED_EIGHTY = 480.0f;
    public static final float FLOAT_FOUR_HUNDRED_NINETY = 490.0f;
    public static final float FLOAT_FOUR_TENTH = 0.4f;
    public static final float FLOAT_NINE = 9.0f;
    public static final float FLOAT_NINETY = 90.0f;
    public static final float FLOAT_NINETY_EIGHT_HUNDREDTH = 0.98f;
    public static final float FLOAT_NINETY_NINE_HUNDREDTH = 0.99f;
    public static final float FLOAT_NINE_HUNDREDTH = 0.09f;
    public static final float FLOAT_ONE_DOT_TWO_TENTH = 1.2f;
    public static final float FLOAT_ONE_HUNDRED = 100.0f;
    public static final float FLOAT_ONE_HUNDREDTH = 0.01f;
    public static final float FLOAT_ONE_HUNDRED_NINETY_FIVE = 195.0f;
    public static final float FLOAT_ONE_TENTH = 0.1f;
    public static final float FLOAT_SEVENTY = 70.0f;
    public static final float FLOAT_SEVEN_HUNDREDTH = 0.07f;
    public static final float FLOAT_SIX_HUNDREDTH = 0.06f;
    public static final float FLOAT_SIX = 6.0f;
    public static final float FLOAT_SIXTEEN = 16.0f;
    public static final float FLOAT_TEN = 10.0f;
    public static final float FLOAT_THIRTY = 30.0f;
    public static final float FLOAT_THREE_HUNDRED = 300.0f;
    public static final float FLOAT_THREE_TENTH = 0.3f;
    public static final float FLOAT_TWELVE = 12.0f;
    public static final float FLOAT_TWENTY = 20.0f;
    public static final float FLOAT_TWENTY_FIVE = 25.0f;
    public static final float FLOAT_TWENTY_FIVE_THOUSANDTH = 0.0025f;
    public static final float FLOAT_TWO = 2.0f;
    public static final float FLOAT_TWO_HUNDREDTH = 0.02f;
    public static final float FLOAT_TWO_TENTH = 0.2f;

    public static final int INT_EIGHT = 8;
    public static final int INT_ELEVEN = 11;
    public static final int INT_FIFTEEN = 15;
    public static final int INT_FIFTY = 50;
    public static final int INT_FIVE = 5;
    public static final int INT_FORTY = 40;
    public static final int INT_FOUR = 4;
    public static final int INT_FOURTEEN = 14;
    public static final int INT_NINE = 9;
    public static final int INT_NINETEEN_HUNDRED = 1900;
    public static final int INT_ONE_HUNDRED = 100;
    public static final int INT_ONE_HUNDRED_FIFTY_SEVEN = 157;
    public static final int INT_ONE_THOUSAND = 1000;
    public static final int INT_ONE_THOUSAND_EIGHT = 1008;
    public static final int INT_ONE_THOUSAND_ELEVEN = 1011;
    public static final int INT_ONE_THOUSAND_FIVE = 1005;
    public static final int INT_ONE_THOUSAND_FOUR = 1004;
    public static final int INT_ONE_THOUSAND_NINE = 1009;
    public static final int INT_ONE_THOUSAND_SEVEN = 1007;
    public static final int INT_ONE_THOUSAND_SIX = 1006;
    public static final int INT_ONE_THOUSAND_THIRTEEN = 1013;
    public static final int INT_ONE_THOUSAND_THIRTY_FIVE = 1035;
    public static final int INT_ONE_THOUSAND_THIRTY_FOUR = 1034;
    public static final int INT_ONE_THOUSAND_THREE = 1003;
    public static final int INT_ONE_THOUSAND_TWELVE = 1012;
    public static final int INT_ONE_THOUSAND_TWO = 1002;
    public static final int INT_SEVEN = 7;
    public static final int INT_SIX = 6;
    public static final int INT_SIXTEEN = 16;
    public static final int INT_SIXTY_THREE = 63;
    public static final int INT_TEN = 10;
    public static final int INT_THIRTEEN = 13;
    public static final int INT_THIRTY = 30;
    public static final int INT_THIRTY_EIGHT = 38;
    public static final int INT_THIRTY_ONE = 31;
    public static final int INT_TWENTY_FIVE = 25;
    public static final int INT_THREE = 3;
    public static final int INT_THREE_HUNDRED_SIXTY_FIVE = 365;
    public static final int INT_TWELVE = 12;
    public static final int INT_TWENTY = 20;
    public static final int INT_TWENTY_FOUR = 24;
    public static final int INT_TWO = 2;
    public static final int INT_TWO_HUNDRED = 200;
    public static final int INT_TWO_HUNDRED_SEVENTY = 270;

    public static final long LONG_ONE = 1L;
    public static final long LONG_SEVEN = 7L;
    public static final long LONG_TWO = 2L;

Mapro

by in CodeSOD on

Steinin was doing some work for a company that needed some geographic information systems work done. Steinin was just the programmer, and was no expert on the algorithms they needed implemented, and so asked for some references on how to implement them.

They handed him this C code.


Shift Sign Off

by in CodeSOD on

An anonymous submitter was working with some vendor-supplied code for talking to a network device. They sent along this sample C code for properly populating the required header.

unsigned int head = htonl(0<<24 | (len+address_len+1));
memcpy(&header[0], &head, 4);

All Roads

by in CodeSOD on

A conditional statement represents a branch in our code. A place where things could go one way, or they could go the other. That, at least, is traditionally what they are. Adam's co-worker took a different approach.

if user:
    if user.is_owner():
        self.template_values['login_url'] = users.create_login_url(self.request.uri)
    else:
        self.template_values['login_url'] = users.create_login_url(self.request.uri)
else:
    self.template_values['login_url'] = users.create_login_url(self.request.uri)

Include This

by in CodeSOD on

C and C++ are weird, specifically in the way they handle the difference between declarations and definitions. We frequently put our declarations in a header (.h) file, and the definitions associated with them in an implementation file- say, .cpp.

But things can only get declared once, which means if multiple implementation files depend on the same header, the compiler will yell at us. We need a way to ensure that, no matter how many times a header is referenced, it's only actually read once.


On the Border of Badness

by in CodeSOD on

The WebForms APIs in ASP .Net tried to make web programming look like native programming- you designed pages in a WYSIWYG editor, and bound event handlers to controls, and pretended like the request/response cycle didn't exist. It was a bit of a mess.

One of the ways it was messy was that you now had two different approaches to styling elements in your UI. You could go the route of using CSS, like a normal web page. But every web Control object also exposed a bunch of properties that you could access directly from your server-side code, allowing you to do things like myControl.BorderColor = Color.Red.


UniQQue Naming

by in CodeSOD on

The application Zach B found himself supporting needed to accept file uploads. At one point, someone decided to just drop all the files in the same directory, so they needed to find a way to ensure that there were no name collisions.

They wrote this:


Not My Domain

by in CodeSOD on

Dian found this code snippet.

String[] key_value = url.split("\\.");

for (int i=key_value.length; i>0; i--) {
        if (i > 1)
                domain = key_value[i-2]+"."+key_value[i-1];
        break;
}

UTF-16 Encoding

by in CodeSOD on

Let's say you were browsing through some code, and saw a function signature like this:

function changeUTF16( $string )