Addition by Subtraction

December 06, 2009 - perspectives

Generally speaking, when we think of adding value, we usually think of what we can add to make improvements. But another way we can make improvements is to remove what's not essential. By trimming out what's not needed, focus is shifted back towards what's important. In some cases, these subtractions can actually add up to a whole lot.

This perspective is usually directed towards design. And for good reason. Simpler designs are friendlier, easier to use, and generally yield better outcomes. But we don't always share this perspective when thinking about software. Frameworks, apis, even programming languages themselves can all benefit from simpler perspectives. Strangely enough though, complexity here is often touted as an asset instead of a fault. In fact, the implicit reasoning is usually that things need to be complicated for them to be worthwhile.

Sometimes we even inflate simpler problems into harder ones. This serves as an excuse to implement more complex solutions. Unfortunately we can do this without even realizing it. And the more abstract the original problem is, the more room there is to introduce complexities. In some cases, it might be easier to err on the side of solving none of the abstract problems instead of trying to solve them all.

So if we find ourselves several layers removed from the original problem, it might be time to take a step back and evaluate how we got here. It might be the right path, or it might not. But if it's the wrong path, then we should turn around.

Simplicity

August 26, 2009 - perspectives approaches

We try to keep things simple. After all, nobody starts a project thinking, let's make this one complicated for a change. Yet somehow, things like to end up that way.

I like to think of this as a shock, but it's usually not that surprising, all things considered.

Perhaps the most obvious reason is that programmers knowingly introduce this complexity themselves. This complexity is justified by rationalizing that it will anticipate future situations, and account for them elegantly. The goal here is to prevent future maintenance headaches, but ironically it can add complexity if the wrong abstractions are made.

The opposite can also be just as true. As code design debt accumulates, not acting early enough once a problem is recognized can also lead to increased complexity.

Programmers can also have drastically different opinions on subjects. If we take a look around the software world, we'll see that there are many operating systems, langauges, frameworks, and a myriad of all kinds of tools. Each one of these represents a particular area where a programmer identified a gap, and then proceeded to make an attempt at filling it in. This naturally leads to overlap among similar software. This overlap itself isn't bad, in fact it's probably a little bit healthy. Each competitor can focus on what it feels is important, and users can choose accordingly. But merely having more anything can contribute to its own kind of complexity to deal with. After all, it's easier to make a choice given fewer options.

But along with the lack of consensus on what's important, programmers also don't think the same things are unimportant. Some are willing to let documentation drift out of sync. Some aren't interested in repeatable builds. How about automated tests? Commented code? Consistent style? Design patterns? Stable back ends? Intuitive front ends? Practically everyone will agree that it's important to have many of these items, but not everyone will agree on which one is ok to let slip. These differences can help contribute to software complexity as programmers fight against each other.

Programmer 1: I never knew it would be so hard to push a car downhill.
Programmer 2: What do you mean, downhill!?

There's also the idea that things aren't worthwhile unless they are complicated. When things are simple and easily understandable, it's easy to suggest avenues of improvement. But if too many distractions are entertained, the codebase can start developing warts that can be difficult to deal with.

It is easy to make things. It is hard to make things simple.

Problems for our solutions

April 22, 2009 - perspectives

Constantly learning is important. Lots of disciplines today change at an extremely fast pace. Software is definitely one of those fields. As we learn new things, we try to integrate them into our existing mental frameworks. We think of different areas where we can apply this new knowledge, or if it can be used to simplify any existing solutions. I for one am constantly trying out new frameworks or languages by thinking of apps I can write with them :)

There is a caveat here though, particularly with newer programmers. Right after learning a new pattern, we have the propensity to use it more than we should. We force it to fit our problems, usually by throwing more patterns in the mix. The code ends up being far too complex for what it's supposed to be doing, and it's much harder to follow what's actually going on. In short, we end up with more complexity and less flexibility. That sounds like what we were trying to avoid in the first place.

Programmers also tend to get emotional and invested in their solutions. Admitting that we took the wrong path is hard to do. Software is the kind of thing that doesn't really have just one answer; we can solve our problems with just about any language or technique. Some are obviously better for certain problems than others, but just about any of them can be *made* to work. This brings me to a quote:

xml is like violence. If a little doesn't work, just use more of it.

I've got more quotes about xml, but I'll save those for another day :)

We all have our own opinions on what kinds of software or tools are useful, and many of us have very strong opinions on these matters. My opinions here tend to change on a daily basis :) But even though we don't like to admit it to ourselves, virtually every approach has its own tradeoffs. In fact, I'd go so far as to say that most of programming is just a series of tradeoffs.

The important thing is to use the best tools for the job. Having lots of opinions is healthy, and it only really becomes dangerous when we fail to consider other options. Bad options are valuable to us too, because they help us focus on what really *is* important.

Perhaps what's most striking is the fact that our problem solving methods in the software world are sometimes backwards. We have a list of solutions in mind, and we try to see which one fits the problem. From one perspective, this is completely natural. How are we supposed to think of solutions that we don't know of yet? But on the other hand, none of our pre-canned solutions may be a good fit at all. In this case, some research into the problem space may help clear the muddy waters and reveal better alternatives. But how do we know if this is necessary, and we're just not wasting our time? Well, I like to follow the mantra that if it feels like there must be an easier way, then there probably is. In any case, taking a step back and looking around again can be a healthy thing. After all, times change, perhaps faster now than ever before. Yesterday's pitfalls can become today's best practices.

When all you have is a hammer, everything looks like a nail.

Programmers and learning

February 18, 2009 - perspectives

May you live in interesting times

Never before has access to information been so easy, and so readily available. And we're starting to accumulate more and more at an incredible rate. What's more is that we are are able to summon information virtually instantaneously. But ironically, too much information can also have the effect of preventing any useful information from getting out. In addition to having a wealth of information readily available, we're also forced to filter out the noise to find the gem in a haystack, if you will.

But if we find ourselves constantly filtering the same kind of information, computers can help us here. After all, they're good at repeating tasks over and over again. And here is where programmers have a significant advantage.

Programmers can improve their rate of learning.

In particular, programmers are able to improve their workflows. They're trained to recognize repetitive patterns, and can make tweaks here and there to help things along. But when you can take baby steps quickly, you start to move fast.

An example that comes to mind is the tool for os x: quicksilver. If you're using os x and you haven't checked it out, you really should. If you're running gnome, gnome do provides similar functionality. These tools let you express the action you're trying to perform more naturally, almost in the form of a sentence. But command line users have enjoyed this richness for a while now. This is an example where the programmers are ahead of the curve, but it's good to see that there are efforts to help out the "less fortunate" :P

Throughout your life advance daily, becoming more skillful than yesterday, more skillful than today. This is never-ending.

What are we building again?

January 31, 2009 - approaches

When faced with a new problem, we start thinking of the different approaches we can take to solve it. We look back on previous experiences, and see if we've solved anything similar in the past. If so, maybe that same type of solution can be applicable to the current problem. If not, maybe it can be adapted. Pattern recognition can be a powerful tool to help us solve problems.

But with our previous experiences come our own biases as well, directing our trains of thought. If my previous problems all had a large focus on security, chances are high that I'm going to be keeping security in the back of my mind. While this is beneficial for preventing security holes, it can also work against us if I try to inject sophisticated security mechanisms when they aren't needed, just in case.

It's important to remember exactly what we're supposed to be building. Who is it for? What are they going to be doing with it? Is this new feature going to help? Is it necessary? The more frills we add on, the greater the chances that the motivation for what we're doing will be lost. Programmers can have a tendency to make things more complicated than they have to be.

At the onset of a project, this is easier to keep in mind. But as time goes on, we keep our heads deeper and deeper in the details, and we can lose the forest for the trees. We add tweaks here and there, spend some time on peripheral tasks, and then get caught surprised wondering why it is we're so far off mark.

It's important to note that what we're building can change over time. In fact, if it's a long project, it probably should change. If not, what we're building may no longer meet its demands.

So when we're lost, we should remember to take a step back, take in a deep breath, and ask ourselves what we're supposed to be building.

Ask a programmer to hang a picture frame, and you'll get a new wall instead.

Beauty vs Practicality

December 25, 2008 - perspectives

We can see software development as a duality with beauty on one side, and practicality on the other. Beauty is coming up with a masterpiece of a solution. Practicality is solving our problems with methods that are known to work. These don't have to be mutually exclusive, but they usually feel that way.

The advantage of the practical solution is that we know it will work. We've usually employed it in the past, and we're confident with it. Examples of brute force methods come to mind, although they don't necessarily have to be. The practical approach is particularly appealing to business, which almost without fail tries to avoid the worst possible scenario. Regardless though, we know we will end up with a working solution in a fixed amount of time, and not just with only scribbles on a whiteboard.

The problem with this is that things can get difficult to work with after a while. Like trying to find something on a messy desk, it's always easier to quickly go through the mess. But if we repeat this several times, then it's better to organize the desk appropriately first.

Finding an elegant solution can mean modeling our needs as a specific instance of a more general concept. We can also try to fit our needs into a pattern by thinking about them from a different perspective. Generally though, I think elegant solutions share the fact that they make our problems look simple.

Looking simple is key. When things are simple, we are able to have many more ideas freely dance about in our heads. Merely keeping complex ideas in our heads can be difficult, let alone finding ways to extend them. And at this rate, we can forget about debugging.

But beautiful solutions are more than just simple. They are a set of neat abstractions, naturally joined together. Each addition compounds on the previous, magnifying the strengths of the whole with a new dimension of power. This continues until the problem itself looks feeble in comparison, like it was created as a reaction to the solution.

Unfortunately, it's difficult to create these problem slayers. Our problems usually hide in dark corners, only revealing themselves at inopportune moments. It's only then that we realize that our methods are inadequate, and sometimes it can be hard to adapt them. It's at this point that we often see the obviousness of our shortcomings. But of course, it's always easier to predict the past than the future.

In the end then, we need a healthy mix of both beauty and practicality. We need to cultivate our magnificent visions, while listening to the sobering voice of reality. As Alan Kay said:

Just play it grand.

PHP - language of the web

November 26, 2008 - tools

Currently, php is the most popular language on the web, popular here meaning sheer numbers. There are number of reasons for this, some of them obvious in retrospect. But then again, a lot of things are obvious in retrospect.

Php is easy to get started with. I think this one most of all is the biggest reason for php's popularity. In its simplest form, php is just a powerful templating language. We can take some html, sprinkle in a couple of php tags here and there, and presto, we have ourselves a dynamic page.

Php is easy to deploy. This almost goes hand in hand with php being easy to get started with. Most web servers are already php enabled, so chances are good that things will "just work" for us out of the box. If they don't, php's popularity virtually guarantees that the solution will be easily found online.

Updates are easy to see. All we have to do is save a file, and reload our browser. As a programmer, I can really appreciate this one, since I'm used to having to restart a process and waiting for it to come back up before I get feedback.

It's easy to find somebody that understands php. I can say this for anything that's really popular currently, so it's not something that's specific to php. What php has going for it here is that there is a low barrier to entry, making it easier for a programmer to quickly learn php without having any previous experience.

If I were to sum up php's downside then, it would have to be its ability to scale in terms of complexity. Most changes are made in an ad-hoc manner, with little thought for the whole. With respect to systems, this is the wrong approach. We want the overall system to have a strong foundation, and have the individual pieces be the ones that take care of their own messes. Php emphasizes getting the individual pieces working, and they often have to know about each other's internals.

For simple systems though, this doesn't matter. If we're just building a doghouse, we don't need to spend the time thinking about having a strong core, because we won't need one. If we just slab things together, it'll hold. We only run into problems when we use this approach to build a skyscraper.

That being said, I think most sites on the web are simple. There's a way to put new data in, a way to control what gets displayed, which could be through a search or not, and maybe a custom workflow for interacting with the data. It's often the case that building up these small pieces from scratch is faster than trying to fit them into the context of a larger system.

Even for more complex systems, there is a benefit to prototyping a naive solution, and seeing where it breaks down. Sometimes it's not where we expect, and this information becomes tremendously valuable when trying to design a more robust solution.

There are attempts to overcome php's shortcomings too. There are many php based frameworks and content management systems out there. Each one of these makes certain separations for you, like MVC, to help ease the burden of maintenance.

There's no denying that php does some things well. After all, millions of web sites can't be wrong, right?

The Cost of Abstraction

October 26, 2008 - perspectives

Most programmers follow a progression as their skills improve.

Initially, we have a just make it work mentality. In this phase, there is no structure or attempt at creating any abstractions. If functionality needs to be duplicated in a slightly different fashion, the lines of code get duplicated. We happily trudge along in this phase until we are met with a project of significant complexity. Here we start to break down, and quickly learn that it becomes difficult to maintain.

In the next phase, which I like to call "abstraction envy", we start to learn the different ways we can architect our applications. We begin to learn the different designs, structures, and patterns that we can use to ease the burden of maintaining applications. As we learn more and more patterns however, we try to apply our newly found knowledge as widely as possible. Given that this is still a learning process, and much of programming is learned by doing, we often pick the wrong tool for the job. To use the popular metaphor, we use a hammer, but we're not hitting a nail. We slowly start to learn that their are some downsides to these patterns as well, and just because we understand them doesn't mean we should use them. If a little is good, that doesn't mean that a lot is better.

This next phase I'm about to describe is the middle ground between the first two. This ideal phase is essentially a zen state of programming. Abstractions are only used when they are necessary. And the abstractions introduced are not just for code reuse, but they create simple, yet powerful ways of thinking about our problems. These types of abstractions are more useful because they more closely match the conceptual, or real problems. You'll know when you see one of these because it'll seem like the problem was created for the abstraction instead of the other way around. Besides creating perfect abstractions for the parts of the application that need them, the flip side is just as important, if not more so. Abstractions are not created for the parts of the application that don't need them. Simple things are left simple, and complicated things are possible.

I think most programmers are in the middle state. If you ask a programmer his advice on a particular problem, you'll probably get an answer, and you'll probably get a lot more information about other problems that get created along the way. Sometimes these problems themselves require complicated solutions, and then these problems cascade.

That brings us to the heart of the problem really. Simple things should be easy to change. If they're not, something is wrong. But none of us writes code with the idea that we're making certain things harder. We always believe that we're improving things. We see some boilerplate, and we think to ourselves, gee, wouldn't it be great if we didn't have to do that all the time? Let's find a way to eliminate it. We move along happily, proud of how much code we've reduced. The problem lies in the future, when we're thrown a curve ball. It doesn't fit in our strike zone, but we've got to hit a home run anyway. We look at the code, and we realize that it would have been a whole lot easier to do if we didn't have to deal with those abstractions we added in earlier. We'll have to modify them significantly to make the new problem fit. In other words, the abstraction leaks, because we now have to understand the implementation.

And now we're faced with a tough decision. Do we make these modifications - modifications is a good word for this, it's usually a hack - or do we scrap them and go for a simpler approach. The hacks are usually easier and we feel more confident that they'll work, but they increase the entropy of the system which will make it harder for us to maintain. A bigger cleanup will take more time and is riskier, but is probably a good long term investment in some cases. Unit tests will help in this case, but like good abstractions, I find that it's just as hard to write or find tests at the right abstraction level to pull this off.

So how can we avoid getting ourselves into a mess like this in the future? I think the real answer is that we have to get ourselves tangled into a few webs of anarchy before we can learn how to avoid them. But in an attempt to answer the question, instead of erring on the side of introducing abstractions, we can err on the side of leaving them out. Simpler code is usually easier to modify, although that's not true for large pieces of spaghetti where a few well placed abstractions trim out a lot of the complexity. But my personal opinion is that it's usually easier - and more fun - to add in additional layers of abstractions rather than break free from existing ones. It also feels safer.

But perhaps what should guide us the most in these ambiguous cases is the principle of least astonishment. If the presence of a particular abstraction is surprising, then it probably shouldn't be in there. Another good question to ask ourselves is if the new abstraction actually makes things simpler. If the code is easier to understand with the abstraction, awesome. If it's a puzzle to figure out what is going on, and it's just there to save on lines of code, change it.

It's easy to make things. It's hard to make things simple

The Master and the Apprentice

October 14, 2008 - approaches

Always two there are, a master and an apprentice

When learning a trade, the master/apprentice relationship can be tremendously useful. It is a slow path, but an effective one. It's a great way to pass on the lessons from the past.

And not only is the knowledge of skills passed on to future generations, so is the knowledge of common pitfalls. As an apprentice begins to make mistakes, the master can correct them immediately, potentially avoiding much of the headache associated with recovering from the error. And we all know that the earlier an error can be recovered from, the smaller the consequences.

However, you don't see very much of this in the programming world. I think there are a few reasons for this.

  1. Being self taught is encouraged. If you can learn yourself just by rtfm, then you haven't wasted anybody else's time.
  2. Programmers all think that they can solve the problem better themselves.
  3. There tends to be a high turnover in the programming industry.

But then, each generation is doomed to repeating the same mistakes. And with programming, it's difficult to know when you're making a mistake. But, a master can point this out right away, and correct it before it becomes a major problem. This can save an enormous amount of time, and greatly speed up the learning process.

Programmers, and those expecting programming based solutions, are usually impatient. There's enormous pressure to release the next best thing yesterday. The programming field also changes rapidly, and it will probably continue to change at an even faster rate in the future. It should be no surprise then that programmers are in a rush.

But the flip side is that it takes a while to become a great programmer. We really need to experience many failures before we can identify how to architect successes. And contrary to popular belief, it takes years to become proficient.

If there are no silver bullets, then I think that encouraging master/apprentice relationships can help propel us as a whole in the right direction faster.

The Command Line

September 28, 2008 - perspectives tools

It's interesting to hear so many different opinions on the command line. Many see it as archaic. Others are scared of it. And then there's a few who prefer it to all other interfaces. But regardless of how you feel about it, there's no denying that it's here to stay, at least for the near future.

I've mentioned earlier that interacting with a computer can be likened to communicating with it. We usually "speak" to it in the physical sense through a keyboard or mouse. On the virtual or software side of things, the most popular interfaces are either through the command line, or a gui (wimp interface).

Command line interfaces allow for more direct communication with the computer. You type words that you want the computer to execute, and it returns the response. Going through a gui is like talking to an interpreter first, and then having the interpreter relay the information. This can be more useful if the interpreter can figure out what you mean and make a more informed request to the computer. But for informed users, interpreters just get in the way.

Where I think the command line really shines is its flexibility. At a moment's notice, you have access to virtually anything, all through a single interface. You have access to a large, powerful set of tools that can be widely used with one another. For example, what if I wanted to count the number of files in a directory that had an odd number of lines in them? That were edited in the last week. And have those sent out in an email. Biweekly. Granted, that's a contrived example off the top of my head, but one that is virtually effortless to accomplish from the command line, yet difficult with a gui that wasn't designed specifically for that.

Another useful feature of the command line is its inherent repeatability. Once a command has been run, it can be recalled, executed as is, or executed with slight variations, all with little effort. This is true for sequences of commands as well. And, if the sequences themselves start repeating in sequence, then they can be moved to a shellscript or function and run with a single command. In this way, the command line allows for the user to create new language that is better suited for the problem, in a bottom up approach.

Guis have their place too though. The knowledge required is usually much lower to start being effective with an application. And guis are a better fit to solve visual problems. It's much easier to work with a wysiwyg type app when you're producing a new design, rather than having to perform transforms with commands and then redisplaying.

But I think the audience is what makes the biggest difference. Programmers, and "expert users" have a tendency to prefer command line tools. Novice users are usually afraid of having something go wrong, and find comfort with gui applications. And for the die hard gui guys, all you have to do is tell them:

Smith and Wesson was the original point and click interface
Next