Home | Blog | Hacks

Dirty Hacks and other Awful Code

I've written my fair share of code that wouldn't pass any review. Some of it might be interesting or amusing to have a look at, so I decided to dedicate this post to exactly that.

s/Childrean/Children/g

This is the title of a commit of one of my university projects (Java and Spring web application - pure horror). Essentially, Someone (who could easily identified with git blame) introduced this spelling error, and everyone else (including myself) just went with it.

In the end, 84 childrean were found in 19 files, from Java classes, to JSF Facelets, to JUnit tests. Since code review was coming up, I came up with the shell snippet below (There are probably refactoring tools that could do that as well, but nobody from our team was proficient enough to know about those).

find . -type f -not -path '*/.git/*' -print0|xargs -0 sed -i 's/ildrean/ildren/g'
find . -type f -not -path '*/.git/*' -name '*ildrean*' -print0 | xargs -0 rename 's/ildrean/ildren/g'

Not only do these lines replace all the occurrences of the offending string inside the files; it also removes the spelling error from filenames.

The Perl/awk/grep abomination amalgamation

Another tale from university: In the computer graphics course (OpenGL on Linux - yay!) we were to implement loading models from .obj (Wavefront Object) files, with a given parser. That parser did not understand normal vectors, so a separate parser was provided for this task. That one however did not parse Wavefront files directly (where a list of nvs are mapped to their corresponding faces with a lookup table), but wants a unrolled list of vectors.

Since no further requirements (or suggestions) were given, I came up with a Perl script, that runs an inline shell script, that in turn runs inlined AWK. It even made it into the report on which our team was graded at the end of the semester :D .

#!/usr/bin/perl

use strict;
use warnings;

my @facemap = `
	awk '{
		if (\$1 == "f") {
			print \$2 "\\n" \$3 "\\n" \$4
		}
	}' < $ARGV[0]  |
	awk -F'/' '{
		printf ("%04d//%d\\n", \$1, \$3)
	}' |
	sort -n |
	uniq -w 4`;

my @norms = `egrep '^vn ' < $ARGV[0]`;

print `egrep -v '^vn ' $ARGV[0]`;
foreach (@facemap) {
	my @num = split '//', $_;
	my $normal_vector_id = $num[1]-1;

	print $norms [$normal_vector_id];
}

I'll just quote the report on what is going on there:

The script essentially removes all vn lines, then parses the f lines of the file, and inserts the normal vectors accordingly.

This is why I love Perl: I know no other language that lets you mix-and-match like this, and you can hack something together so quickly.

Rube Goldberg Makefile

It seems, most of my sloppy code has been written for university projects. This one comes from the documentation of the project described in s/Childrean/Children/g.

At the beginning of the semester, a functional spec was to be laid out and submitted as a PDF. LaTeX would be the ideal choice, but not everyone has learned how to write LaTeX. So we decided to go with Markdown, since that provides enough functionality and is easier to learn. Pandoc was then used to get a PDF out of that, and since it uses LaTeX as an intermediary format, no one will be any the wiser.
However, pandoc's default LaTeX output was not perfectly suited for our needs, and instead of figuring out pandoc's template feature, or how to include the style for a custom title page (the deadline was coming soon and I couldn't be bothered), sed and pdf-stapler (a drop-in-replacement for pdftk, which isn't in Fedora's repositories) were called to the rescue.

.PHONY: all clean titlepage konzept combine

all: remove titlepage konzept combine clean

titlepage:
	cd tex && pdflatex Titlepage.tex

combine:
	pdf-stapler del konzept.pdf 1 temp.pdf
	pdf-stapler cat tex/Titlepage.pdf temp.pdf Konzept_Final.pdf

konzept:
	pandoc Konzept.md -H head.tex --standalone -o konzept.latex
	sed 's|\\documentclass\[\]{article}|\\documentclass[a4paper]{article}|' konzept.latex > konzept2.latex
	sed 's|\\setcounter{secnumdepth}{0}|\\setcounter{secnumdepth}{2}|' konzept2.latex > konzept3.latex
	sed 's|\\maketitle|\\maketitle\\tableofcontents|' konzept3.latex > konzept.latex
	pdflatex konzept.latex
	pdflatex konzept.latex

clean:
	rm -f konzept.latex konzept2.latex konzept3.latex konzept.aux konzept.log konzept.out konzept.toc
	rm -f Titlepage.aux Titlepage.log Titlepage.thm tex/Titlepage.aux tex/Titlepage.log
	rm -f temp.pdf temp2.pdf konzept.pdf Titlepage.pdf tex/Titlepage.pdf

remove:
	rm -f Konzept_Final.pdf

There is no excuse, but it worked flawlessly (with the exception of the PDF links to pages being broken) - and saved us quite some time.