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 2007

The Black Box of Or

by in CodeSOD on

Tony has a problem. He has been charged with turning a stack of Python code into nice, shiny PHP. Now, I'm not one to trust a language that makes such a big deal out of white-space but having a style imposed on developers does have it's benefits. For one, the code is supposed to be more easily groked precisely because the reader only has one style to parse.

Still, it's been no picnic for Tony.

I didn't write the original non-documented, barely-commented code. I don't even know who did, so I employ a strategy of examining this code as well as its input and output. Once that starts making sense, I write a PHP function that does at least that and sometimes more. I'd like to share one exciting bit of code I came across during this process.

Enough String to Hang Yourself

by in CodeSOD on

Many people (especially, with articles like this) miss the distinction between the writer and the written-up. One of the hard things --- as an editor and as a coder --- is to not just blurt out all the answers: that ruins the fun for everyone at the sake of being only slightly less incomplete. When there is a "challenge" to re-write something, it's just for fun and we should probably refrain from making it personal.

That being said, I've got the hint that many of you like string functions. Here's one John found. Yes, it's C++; and yes, they're working with char*. I'll assume you can figure out why it's called strcmpi.


The End of Hex as We Know It

by in CodeSOD on

The abstractions we build in computer science are meant to sweep away painful implementation details for any program, to make them someone else's problem. On real systems it's not always that simple. In the real world, it is sometimes necessary to get down in the muck and reinvent conventions in order to reinforce the foundations of a particular project.

Take, for instance, the an example sent in by Corey.

I work on this fantastic system, where a particular DB table needs to store some 25 flags which are part of configuration data. Instead of using named bit fields, these flags are stored as "machine-hex" strings representing the bitmap. Machine-hex is like hex, except that it is [read] little-endian and ':', ';', '<', '=', '>', and '?' denote the digits after 9. Thus the string '4<1>:0' stores the flags 0000 1010 1110 0001 1100 0100.


When A Problem Comes Along, You Must skipit

by in CodeSOD on

An anonymous snippet comes from the land of Visual Basic, where people continue to prove on a daily basis that simplicity of syntax offers no protection against screw-ups:

Having recently started a new programming job, I've quickly discovered what most of the day-to-day grind entails: Deciphering and cleaning poorly written Visual Basic code before actually being able to perform my assigned work. Our codebase has plenty of the usual suspects: Variables with meaningless names like "AA" and "bbb", thousand-line functions (which approach VB's built-in limits), no-ops, unreachable code, gobs of copy-and-pasted code, and a few instances of this little construct (irrelevant code removed):

If {condition} Then

        ' ...code here

        Goto skipIt

End If

' ...more code here

skipIt:


Removing Spaces, the Easy Way

by in CodeSOD on

There are tons of functions in so-called "standard" libraries, but sometimes the function you want just isn't there. Luckily, string functions are so simple to write that anyone can do it!

Submitted for your consideration, an example from Randy, who says, "I think the design kind of speaks for itself." The following code demonstrates how to get rid of unwanted spaces.


Taking the U out of UUID

by in CodeSOD on

Paul N, spotted this ColdFusion code in a third-party shopping cart system he'd been tasked with modifying (to make it usable):

 


A Real Brazil Nut

by in CodeSOD on

Code snippets know no geographic boundaries.

Jose Sobrinho sends us a great abuses of IF statements all the way from Brazil. The best of his "pearls" is a real mind-bender:


Validating Email Addresses

by in CodeSOD on

The format for e-mail addresses is specified in a number of RFCs; it's a pet peeve of mine when people "validate" away perfectly valid addresses, for instance: websites that think all domains end in .com, .net, .edu, or .org; and agents that refuse to transfer mail with a + in the local-part. To that end, I wrote my own regular expression that (I believe) follows the specification, which I'll share below.

First, I'd like to share some code that Igor found, which he considers a masterpiece.


Global Spaces

by in CodeSOD on

Global variables have gotten a bad wrap. Like goto, comefrom, they are often considered harmful. Not everyone thinks action at a distance is spooky.

These days, they are often used as global constants, whether they're written that way or not. For instance, Rachel found the following global variable in some code she inherited.


Fun with Files

by in CodeSOD on

I love file systems. You can do all sorts of neat tricks, sharing state based on file existence and naming. If it ends in ".tmp", it's not done yet. If it ends in "_hostname", that's who's handling it. I wish I could remember who said that "back in the day, they had transaction safe databases: we called them file systems."

When doing tricks like these, it is important to have four operations: touch, stat, mv, and rm. Recently, Anne found code written by someone that just didn't think the standard functions in C# were up to snuff. Here's an advanced version of FileExists for your perusal.


One Step Forward...

by in CodeSOD on

When working with embedded systems, it is sometimes important to do dirty little hacks. There are many ways to call upon the dark magic, though a large number of them are pointer or string tricks, often sacrificing readability and portability for memory or performance. There is a local benefit.

As a contractor, James runs in to his fair share of funny things in firmware. The following code, however, did not make him laugh.


Local Mistakes

by in CodeSOD on

We've talked about localization before. Maybe it's just really hard to remember that some people use symbols differently than you do. Luckily, most modern languages have tools to help us deal with rules we never wanted to know about; in .Net, this all lives in System.Globalization.

Esteban recently came across a good example of how to be sensitive to the way other people use commas and periods.

What often seems to be the most difficult is the decimal separator, especially for the person who wrote this code. I found it during a review to verify that specification changes had been implemented in a lot of old code.


Turn it up to Eleven

by in CodeSOD on

Though it has been described as only a PDP-11 assembler, C stands in a place of honor, surrounded by children, legitimate and illegitimate alike. The reason is very simple: C is not just a PDP-11 assembler. It simplified things by giving us the ternary operator.

At his last job, Frank realized that the magic of cond ? var_t : var_f is that it's not just an operator, it's a fundamental building block of other operators. Forget if then else and temporary variables! Some languages let you write cond && amp; var_t || var_f or statement if condition but that's just syntactic sugar.


Nothing Final

by in CodeSOD on

When C# was created, they decided that C++ was wrong when it came to try-catch. Instead, they took the Java approach and used try-catch-finally. The reason C++ doesn't have this block is because it's unnecessary: since acquisition is initialization, most resources are local. They'll be cleaned up automatically, getting destructor calls as the stack unwinds. In Java and C#, no such luck; thus, finally.

Dave has been cleaning up a lot of C# code recently and found the following, excellent example of finally being used. The real question is, "Finally what?"


Rarely Just TRUE or FALSE

by in CodeSOD on

Our friend R.L. writes in with a twist on a common snippet, illustrating an important scientific fact: solutions in life are rarely just TRUE or FALSE ...

 


Laying the Foundation for i18n, Brick by Brick

by in CodeSOD on

In Europe, they do things a little bit differently. From what I understand, it boils down to this: they work less and play more; when not working or playing, they drive tiny little cars. Apparently, they all speak different languages too.

Jannik works for a well-known, innovative company somewhere on the continent. Because of the multiple-language problem, his company translates their website into multiple languages. The way their URLs are formed, going to http://www.company.tld/eng/products and http://www.company.tld/deu/products displays the same content, except the former is in English and the latter in ... Deulish?


Mr. Memory Leak

by in CodeSOD on

In any industry, there are amazing hacks that are passed down, generation to generation, at trade-shows and conferences. In organizations, these little bits of dirt solidify into idiomatic pearls and paradigms. These sorts of things can't be learned in school.

That's why one of Mac's colleagues took him in and decided to show him the dark secrets of programming. For instance, the best way to clean up variables, is illustrated below.


The Really, Really Long Way

by in CodeSOD on

Jeremiah V. writes to share with us another snippet fact: there is always a longer way to do something, no matter what language is used. In SQL-92 compatible databases, it is a challenge to have a dynamically-generated ORDER BY clause, but there are a number of ways to produce a reasonable implementation. For instance, a quick Google search turns up using a combination of CASE and CONVERT to handle a dynamic ORDER BY statement with different column types.

Jeremiah found the much, much longer version of a solution to this problem. Once he stopped laughing, he notes for us that many, many permutations are missing, save for some of the ORDER BY criteria being treated as a single unit. The CASE and CONVERT functions are avoided in this implementation, but the code size becomes enormous... let's hope they don't add a new column any time soon:


So Bad It Needs An Apology

by in CodeSOD on

My new hero John Connor submitted an epic code snippet.  Let us dissect this in two parts, because it is such a massive SOD that trying to tackle it all at once could cause brain trauma. 


Twelve on a Line

by in CodeSOD on

GRG writes: I've been trying to track down an error in a certain program, used by hundreds of people around the world every day to process gigabytes of very important data. Sometimes it works "fine", other times, not often, only about 0.41% off the time, it goofs up the data, from minor-league (0.22%) to big-time (80%). Apparently the users of this data are not very careful, as the program has had these bugs for at least six years.

It took quite a few days to track down the problem, as the program, for no good reason, resides in about 188 separate FORTRAN and C source files. Finally I found the source of the glitch. Let's see if you can spot the problem too.