"Write Everything Twice" and "Because What If I Need It Later"

April 1, 2009

Programmers are concerned with the number of lines of code they write in their programs. There is this idea that the fewer lines of code you write the better. This is a hold over from the days of tiny hard drives measured in Kilobytes, a far cry from today's standard of measuring hard drives in hundreds of Gigabytes or even Terabytes. We no longer need to convolute our programs with abstractions simply to save on the number of lines. This is why I'm advocating the "Write Everything Twice", or the WET principle. The next time you have a method or function in your code that does something and you want to use that function in another place in your code, but have it function slightly different, just simply copy and paste the original method and alter the copy. You've heard it said time and time again. Disk space is cheap, which in turn means lines of code are cheap.

Another closely related principle is "Because What If I Need It Later", or BWIINIL (pronounced ba-WIN-null). Many programmers are lazy in that when they develop an application, they sometimes only write the code for that features that they think they need at that moment. What they don't realize is that there is significant mental cost of switching back into the context of that part of the code a later date. Any time you are writing code you should say to yourself "What else might I need later?".

One particularly good way to apply the BWIINIL principle is to get the book Design Patters: Elements of Reusable Object-Oriented Software, which is more commonly know as the Gang of Four book, and attempt to apply every pattern in the book to every method you write. This will allow you to insert layers upon layers of abstraction and indirection that may seem like overkill at first, but will come in handy some day when you are adding new features.

Here's an excellent real word example taken from Uncyclopedia. We'll all familiar with this trivial implementation of HelloWorld in Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

This kind of code is often included in books about Java, which is understandably watered down for programmers first learning Java. But if you really want to graduate to that next level and write a professional, enterprise-ready implementation of Hello World, you need this:

interface Printer {
    void print(Message message);
}

class Message {
    private String message;

    public Message(String message) {
        this.message = message;
    }

    public void print(Printer printer) {
        printer.print(this);
    }

    public String toString() {
        return message;
    }
}

abstract class AbstractPrinterFactory {
    public static AbstractPrinterFactory getFactory() {
        return new SystemOutPrinterFactory();
    }

    public abstract Printer getPrinter();
}

class SystemOutPrinterFactory extends AbstractPrinterFactory {
    public Printer getPrinter() {
        return new SystemOutPrinter();
    }
}

class SystemOutPrinter implements Printer {
    public void print(Message message) {
        System.out.println(message);
    }
}

class HelloWorld {
    public static void main(String[] args) {
        Message message = new Message("Hello, World!");
        AbstractPrinterFactory factory = AbstractPrinterFactory.getFactory();
        Printer printer = factory.getPrinter();
        message.print(printer);
    }
}

Also make sure to include UML class diagrams that shows how these components interact. Once you learn to embrace the WET and BWIINIL principles and apply them to your everyday coding, I'm sure you'll quickly see an increase in the quantity...er, I mean....quality of your code.

Posted in Technology | Tags AprilFools, Java | 4 Comments

Ninja Patching Trumps SVNHub

April 1, 2008

Earlier today, I though SVNHub was the best April Fools out there, but I think the award goes to Ninja Patching. It still doesn't top my all time favorite though/

Posted in Technology | Tags AprilFools | 0 Comments

SQL on Rails

April 2, 2007

SQL on Rails takes the cake for geeky april fools jokes. This is powerful stuff:

DECLARE @stuff = SELECT * from the_internet WHERE content LIKE '%criteria%'

"This is actually what google used when they first came on the market"

Take the time to watch the screencast, it's hilarious.

Posted in Technology | Tags AprilFools, Rails | 0 Comments