Derrick Pallas

Apr 2007

That'll Show the Grader

by in CodeSOD on

When I was grading for the Software Engineering course, I saw some pretty awful things. Most of the students were actually alright programmers; in fact some were pretty good. The trouble was that neither the really good ones nor the really bad ones came to lecture, so it was impossible to tell who the bad ones were until after it was too late to help them. Mostly, this was because we only began grading the term project --- interactively, as milestones --- late in the term.


Most groups still ended up getting the application to run but there were always one or two students that couldn't explain the system they were supposed to have helped design. During these evaluations, there were three types of teammates. The first was the person that couldn't sit still, wanting to pick up the slack. The second was the person that could only sit still because they, though fairly competent, knew that the application didn't really do what it was supposed to. And then there were the teammates that took sadistic pleasure in watching their randomly assigned partner suffer.



Eliminating Shady Characters

by in CodeSOD on

"These characters are real troublemakers," writes Cody. "You know the guys. Hair slicked back, boots, leather jackets. Or else, pants hanging down to their knees, pagers going off. Wouldn't it be great if we could just get rid of them? Wouldn't that make life a whole lot easier?"


One of the developers he works with thought so too. These punks were messing with the system, pretending to be foreigners. That's when the coworker took the law into his own hands and decided to "get ridoff" them himself.


/**
 * This function is called from jsp to get ridoff some
 * of the special characters
 */
public static String removeSpecialChars(String a_sValue)
{
    if (a_sValue != null)
    {
        a_sValue = a_sValue.replace('\'', ' ');
        a_sValue = a_sValue.replace('\"', ' ');
        a_sValue = a_sValue.replace('&', ' ');
    }

    return a_sValue;
}


The Case for Switch

by in CodeSOD on

Little more than a structured goto with a nicer getout, every C programmer knows (and many love) the switch statement. Even if you don't love it, there are obvious uses that produce cleaner code than strings of else-ifs and help prevent many simple kinds of errors.


Take for instance the following code, for which Gulliver writes in asking, "how many times do you have to check a condition?"


void ReplaceCommas (char *buffer)
    {
    int i,j;
    j=strlen(buffer);
    for (i=0; i < j; i++)
        {
        if (buffer[i]==',')
            {
            if (buffer[i]==',') buffer[i]='.';
            }
        if (buffer[i]==',')
            {
            if (buffer[i]=='"') buffer[i]='\'';
            }
        }
    return;
    }



Race and Government

by in Feature Articles on

The government: preserving rights and protecting justice. The Department of Motor Vehicles: our model of efficiency and equality. My car: newly titled and good luck finding parking in San Francisco.


While I was sitting at the CA DMV, I contemplated these things. Why did I register for an appointment at 9am on a Monday, especially when the lady behind the dispatchers desk didn't ask for any proof that I actually had a reason to be in the "Appointment Express!" line. No matter, it only took me 15 minutes to get in, hand in my homework, and get out. But all is not well at the DMV.



Time for Consultants

by in CodeSOD on

Because consultants work on many different kinds of projects, they have the ability to produce large libraries of boilerplate code. "Hey, we did something like this before. Just a couple of tweaks and we're done!" Code reuse makes everyone happy, especially management on both sides.



Strings in Hiding

by in CodeSOD on

With all this talk about char* and the struct hack, some of you may have gotten the (incorrect) impression that I hate C-strings. On the contrary, I think they're great. Many of my friends are character buffers! In fact, one of my favorite functions is strdupa. But with std::strings and StringBuffers out there, how can they compete?

Luckily Ben, one of our field agents, has found a good example of when a StringBuffer is overkill. Apparently, it was too much trouble to type TextToPrint[Index]=sb.ToString().


Comment in Earnest

by in CodeSOD on

Most Introduction to Computer Science classes teach students about the importance of comments. Some even teach the value of good comments. It is still not surprising to think that a lot of code is speckled with inanity. I've seen it, I think you probably have too. I'm pretty sure I didn't write it but who knows: the author didn't put his name anywhere in the file, just a note following n+=1 explaining that the statement "increments n."



History++

by in CodeSOD on

In the beginning was FORTRAN; Science created FORTRAN and loved it and saw that it was good. Many great and mighty works were done in this language as it spread across the face of the deep. This --- the favored language, imbued with the power of transformation --- understood the very mind of Science.


But, lo! there was another language stirring in the garden. It was called "C" and employed the dark arts and pointer arithmetic. Lacking a native complex type and at least six extra letters in its name, C became jealous of FORTRAN, the beloved. With it's spawn, C++, it struck out to win the heart of Science.



While-While

by in CodeSOD on

As Ken Thompson showed, you can never trust a compiler. His most famous example was of a trojan compiler, though he later refined the model. Lesser know is his example of the trojan-less compiler: it meets your sister in a bar, buys her a couple of drinks, and the next thing you know you've got a shotgun and a preacher. That's when the compiler skips town.


That, of course, is an extreme example; and as much as we all like extreme programming, it doesn't exactly reflect reality. Given a shortage of power drinks, you can often get less rowdy compilers to do what you want by speaking slowly and repeating key phrases. Realizing this, Anders sent in a perfect example, taken from a module loader.


Must Be Last

by in CodeSOD on

Software is a delicate thing. The myth of "portability" is detrimental in that it causes us to abuse intricate, beautiful code by turning it into generic, ugly tripe. If you develop something that doesn't require loving care to compile, you've failed your inner artist.

That being said, Francis would like to share the following work of art with you. Keep your hands to yourself, however, because the slightest touch could bring everything crashing down. Also, be sure to read all of our other snippets first, as this one must be last.



The End of the String as We Know It

by in CodeSOD on

I wish that there was something funny I could say about C-style, null-terminated strings. There isn't. "Putting a sentinel value at the end of a variable length sequence instead of a fixed-size count at the beginning: priceless." The fact is, they just aren't funny.



Back That URL Up

by in CodeSOD on

In the progression of web application developers, there is a phase during which the developer has the power to build something really useful but lacks the ability to create useful URLs. This phase is dangerous because if the application becomes widely deployed, you're likely to be stuck with a namespace that complements its lack of expressibility with an overabundance of complexity.

That aside, there is one thing constant in the ever-changing world of the web: root. For any domain, the document root unifies and binds all other resources on that domain. If you're ever feeling lost, that's where you want to head. Aaron, digging through a "legacy enterprise application, written in old-school VB6 ASP" found just the function for finding your way from any page to that place of calm.


The UNIX Philosophy

by in CodeSOD on

Recently, a friend of mine --- a chemistry graduate student at a prestigious university --- was complaining that his advisor was forcing him to use C++ instead of Fortran. The problem: he needed to g/re/p an input stream, crunch some numbers, and produce some output. "It's so easy to use the Fortran system call; is there even a way to execute command-line programs from C++?"

That of course is the UNIX Philosophy: to build a bunch of fundamental, pipeable tools and then call them with exec and friends. In that same vein, Paul sends along the following Perl code he found, "written by an ex-coworker."


Bringing LOGing Back

by in CodeSOD on
Linda didn't have much to say about the following code snippet. In fact, she sent nothing else; therefore, I feel like I don't need to say much about it. All I know is that if I ever need a logger to produce a stack trace in Java, this is how I'll do it.


Just in Case

by in CodeSOD on

 The best thing about HTML is that (historically) consumers are insanely permissive about what they accept. The most obvious thing is that --- casting off the iron fist of XML --- hypertext markup allows tags to be case-insensitive. It is widely accepted that this helps designers more fully express themselves on the web.

Of course, this causes problems for some tools. Take the example of the tool Ben was looking at. He wanted to learn more about regular expressions and, realizing that all HTML tools are probably written with regular expressions, decided to dig into one.


In the Shadow of Giants

by in CodeSOD on

It is not uncommon to feel intimidated when starting a new job. This is especially true when you think you've been hired as a programmer, show up, and are asked to do IT for "just a couple of months" until they hire someone to do that job.


Such is the situation in which Clint found himself, many years ago. The problem is that when they finally hire an IT guy, it may be hard for the other programmers to accept you into the herd. To them, you're just the guy that bounces the email server and makes backups.


Lucky for Clint, the other developers sat him down and walked him through the application he would be working on. Though many of the concepts seemed foreign, they appeared to know what they were doing. In fact, when one of the other developers complained that he was getting "inconsistent results," he asked Clint to take a look at his code.