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.

Feb 2016

Hero on the Half Shell

by in CodeSOD on

Melissa’s co-worker needed to write some Python code to kill an external process. There are some third-party modules, like psutil that can solve that problem, but companies like Initech tend to put lots of obstacles along the path of bringing third-party code into your applications.

Without third-party tools, you’re stuck “shelling out”. Using built-in Python functions like os.system or the subprocess module. This lets you run commands like ps and kill from inside of your Python program. It’s inelegant, but it works just fine. There’s certainly nothing wrong with it.


Random Ruby

by in CodeSOD on

Corindons rose et pourpre (20 mm) var. rubis (Pakistan)

Peri, the new intern, was proving her worth as a programmer rapidly. She was great with finding clever solutions to problems, and she didn't bother the more senior devs too often while doing it: a boon, as they were trying to get their own work done.


Authorized Users

by in CodeSOD on

“Hey, since Jack quit, you could add Jane to the investigation users?”

William M supported one piece of a large, complicated intranet application used by auditors as part of investigations into “events” within the organization. The overall audience is small, and the purpose of the application is mostly to document the process of the investigation. Since it’s so niche, it doesn’t even have a screen for managing user accounts- the users raise a ticket and rope in developers to manage it.


Safely Configured

by in CodeSOD on

Configuration files are, well… files. File operations are “risky”- there’s a lot of reasons why they might fail, and we have to be prepared to handle those execptions.

For example, maybe you’ve written an application that reads a configuration file at launch. If, for some reason, it failed to load that config, you’d need to deal with that. If the configuration were just some minor settings, you might choose to fallback to some reasonable defaults. If, on the other hand, the configuration contained important security settings, you would probably want to quit the application, or maybe fallback into some “safety” mode.


Unselected

by in CodeSOD on

In the movie Man of the Year, Robin Williams plays a Jon Stewart-esque comedian who runs for President of the United States. He wins the general election due to a programming glitch in some e-ballot machines deployed nationwide.

In Belgium, as Adrien F. can attest, this very nearly happened.


Keeping Regular

by in CodeSOD on

Regular expressions can create new problems. Like an industrial drill, they’re extremely powerful- and potentially destructive. They’re expensive, they’re cryptic, but can be extremely concise.

For example, Jessica is reimplementing some C# code written by another developer. This developer was never interested in being concise, and instead favored being “clever”. For example, this developer had an array of strings, and needed to remove any non-word-characters from each string.


Log of String

by in CodeSOD on

Zrywka drewna 776

The English language contains words with multiple and often contradictory meanings. A dress, for example, is only one of many items you could put on while dressing yourself. Meanwhile, if you want to wear pants instead, you should avoid pantsing yourself, as that would be counter-productive.


High Performance Memory Allocation

by in CodeSOD on

Jamie has a co-worker who subscribes to the “malloc is slow” school of thought. Now, for most programs, it’s fine, but Jamie works on a high-performance computing system operating in a massively parallel configuration, so there are portions of their application where that philosophy is completely valid.

In this case, however, the code Jamie’s co-worker wrote is in their message handling layer. There’s really no reason to pool buffers there, as the performance gain is practically non-existent based on the frequency of buffer allocation. That doesn’t change Jamie’s co-worker’s opinion though- malloc is slow.