Empty files and templates

Posted in:

I've noticed in Python and in Django I almost always create a module or a template by starting with a completely empty file, and typing. To non-programmers, this might seem a bit obvious, but those who have worked with a range of programming technologies, it's actually quite remarkable.

For instance, for all the .NET applications I do at work, there are templates for every type of file you might want to create, and I assure you that none of them are empty. From memory, the one for a C# console application looks something like this:

using System;

namespace YourProjectName
    public class Class1
        public static int Main(string[] argv)

Until now, the only time I had to type that out was when writing a "Hello world" C# app – since then I've always used Visual Studio to create the files – after all, why bother typing out all that code that essentially does nothing? (BTW - I'm pretty sure I've made mistakes there - especially that STAThread bit. I can't write an application that does nothing in C# without making mistakes.)

However, that example is pretty mild. For an aspx page, it's much worse – two files for a start, and while I might be able to have a reasonable go at the codebehind (.aspx.cs), I can't begin to remember all the tags that go at the top of the .aspx file. These files are always created from templates, or, more usually, copy and pasted. It's the only practical way to do it.

By contrast, whenever I create a new Python module, it starts as literally an empty file – I don't even copy and paste imports. There is this wonderful feeling that you are about to create something – rather than hack around with something you made for another purpose. Every line of Python you write is to do with the programming task before you, and not satisfying compiler requirements. As someone pointed out, a Python program that compiles but does nothing is an empty file, which says a lot about the philosophical differences between Python and languages like Java and C#.

Similarly, Django templates start out as empty files. I almost always then type something like this:

{% extends "myapp/standard.html" %}

{% block content %}
{% endblock %}

Each line of that template means something and is an essential instruction, and is therefore easy to remember. When what you want to express has been encapsulated down to the level of something like a language construct or something as simple as a function call, then you've arrived, as far as I can see. You can only further reduce things by adding lots of implicit rules – like default variables in Perl, or all the built-in rules in Makefiles (neither of which can I get on with at all – they seem like a good idea when you are writing, but not when you are trying to read or understand).

I guess I have to talk about the fact that Django does ship with some template files for creating new projects and apps (as does Ruby on Rails), which kind of goes against what I've been saying. However, the purpose of the templates is mainly to set up a directory structure to enable Django to find things. For example, the 'application template' consists of 3 files, one of which is completely empty, while the other two between them contain a single line of Python code (from django.db import models) and two single-line comments.

The 'project template' is more substantial, but the biggest contribution is the settings file i.e. configuration. I guess it is inevitable that a framework like Django is going to need need some configuration, and even then, most of the contents of settings.py can be removed if you don't need that functionality – it is there for convenience really.

Anyway, the end result of working with tools like these is beautiful, uncluttered code, and a great sense of satisfaction and craftsmanship. It seems that many other languages don't even attempt to make code beautiful, so I imagine there are lots of programmers out there who have no idea that they are missing out on anything in this respect. If you code but haven't yet tried Python, you really ought to have a look.

Comments §

Comments should load when you scroll to here...