Configurational programming

When I faced YUI's programming style 2 years ago, somehow I was amused by its elegance. I refer elegance as the configurational approach, that you have to explicitly configure things up, instead of writing down the steps of a process.

For example, here is how you state your column definitions for a DataTable widget instance:

var myColumnDefs = [
 {key:"Title", label:"Name", sortable:true, formatter:formatUrl},
 {key:"Phone"},
 {key:"City"},
 {key:"Rating.AverageRating", label:"Rating", formatter:formatRating, sortable:true}
];

and later on you can say:

var myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs, ...

I'll try to explain why I find this topic so important that it has already influenced my style of programming.

Expectations instead of steps
A configuration like this is a contract between the programmer and the API. The programmer writes down it's expectations from the program and not the exact steps of how to reach that goal. As I came from C# I see this style as an interface. I like interfaces because they are clean, and hiding implementation details.

Config objects vs. parameters
I think there is a huge difference between giving configuration for something versus giving parameters (at least for me). I see parameters as a superset of configurations because parameters can contain not only expectations but details demanding you to understand the code behind the function.

For example, consider this function:

function rangeLowerCase(from, to, elements){
     var els = [];
     for(var i = from; i < to; i++){
        els.push(elements[i].toLowerCase());
     }
     return els;
}

rangeLowerCase(0, 5, ["H", "E", "L", "L", "O"]); //-> ["h", "e", "l", "l", "o"]

When we call rangeLowerCase above we have to give the beginning of the process explicitly with the "from" parameter. But that's pops up lots of questions. Where the implementation starts? From zero or 1? At the end of the day, we have to read the code or documentation to understand what's going on.

So the "from" is not a configuration but a parameter. In my vision a configuration would be like

rangeLowerCase({ "take-first" : 5, "list" : elements});

Here you don't need to read the implementation but you know what will happen... okay that's not true, but at least you can expect it. You don't have to know where we want the first index to start. It's totally upon the developer of the API.

Elegancy

How many lines of code do you have to change when you want to modify its behaviour? I always ask this question when talking about elegancy and I try to hold that number very low in my solutions. Considering the previous example, it is self-explaining how many lines you have to change to take the first 10 elements instead of 5.

To sum it up, a configuration should improve the understanding of the code at first glance so improving the maintainability and clarity. It's more about expectations rather than implementation, so it's closer to the customer's thoughts, and easier to maintain.

Why don't they teach these things?

After university, it's hard to get on with LIFE in capital. What I can tell from my experiences, schools didn't teach me important know-hows but interesting theories. However I find very challenging and hard to fit in everyday life. See the details below.

Time

It's not a common or easy task to manage your time. I mean I work 8 hours a day of course, but what about the rest? Questions bugging me on what to do after my duty, beside sports. I have a lot of ideas about projects and what to learn next, but how should I priorize them? For me, a day just not enough to do everything. Yes, you can say, maybe I'm an overachiever but that's how I work.

So nobody told me at university that there is a course on time-management. Because there wasn't. If I had known back then about Getting This Done, maybe I would have entered in this area with more confidence. So, I highly recommend GTD along with these productivity tools.

Money

Okay so money is unavoidable in everyday life. So you should at least keep track of it and plan with it. It's not too suprising when I say nobody taught me that. Especially when we talking about how to do it. I'm still in the experimenting phase. For example, there are bills what you should store and track, and also there are expenditures what you should not track but limit, say.. for a month. I did not found any usable books on a system like that, but let me know if you do.

However, one thing I actually learned during school. To ask and experiment. Asking questions is the most important part of my life, literally. For me, it means that I will have answers and I strongly believe in that. I haven't regretted this so far.

So if you are learning in school right now, keep asking questions. Whether you are interested or not. Because this is the way how you will learn, generally. You have to excercise that so many times that it will be a part of you without realizing it.