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.

Dec 2012

Recursive Whitespace Removal

by in CodeSOD on

C is a double edged sword. On one hand, it's simple and powerful enough that, given enough effort, you can accomplish just about anything you want. However, this power is limited insomuch that you don't have many of the friendly helper-functions that exist in higher level languages, such as string manipulation for example, unless you go ahead and create them yourself.

Case in point - consider the below function used to trim trailing spaces. Submitted by Victor, this code apparently runs in a system that processes financial transactions in real time. For the sake of the system and its users, I hope that there isn't a lot of whitespace.


Ancestors

by in CodeSOD on

It’s important to have an understanding of genealogy; it can give you a connection to history. Even in code, we find a need to connect with our parents and their ancestors.

Kevin found some code that needed to connect a PictureBox with the Form that it’s displayed on.


Time Flies

by in CodeSOD on

Alan's supervisor forwarded him a curious ticket - a recently hired employee was training on how to use the customer profile web page and the new user felt that that the countdown timer on the page wasn't behaving quite right.

What made the issue so curious? The 6 year old, internally produced site was used by tons of people day in and day out to support the business - heck, even Alan had used it briefly during his testing.


One Version

by in CodeSOD on

Brian browsed the most recent check-in by the lead architect, and noticed that it referenced a file called TagFile.java, which didn’t actually exist. A quick search of source control showed that pretty much every project had its own version of this file. They were all basically identical, aside from the values in the static initializers:

import java.util.*;
public class TagFile {
    public static String       dateTime="2012-06-12 14:21:19";
    public static String       simpleDate="2012/06/12";
    public static String       builder="user1234";    
    public static Hashtable    hash=new Hashtable();
    public static StringBuffer sb =new StringBuffer();
    
    //initialize static values
    static { 
            hash.put("User ","user1234");
            hash.put("Build Date","2012-06-12 14:21:19");
    }
    
    //SNIP: some property accessors
    
    public static void main(String args[]) {
        System.out.println(printHashtable());
        System.out.println(getDateTime());
        System.out.println(getBuilder());
    }
}

A Bit Misguided

by in CodeSOD on

It’s hard to get too far as a programmer without dealing with bit-masks at some point in your career. Barry’s co-worker made sure to build a nice, easily re-usable block of code to help with that. This simple block can simply be copy-pasted anywhere bit-masks are used. And it is.