Derrick Pallas

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.


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.


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?"


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.