‘AND’ and why you should avoid it in programming and in life.




This is one of the most useful tips about programming I’ve seen. It has deep roots in programming philosophy and in philosophy in general. I find I can apply it not only to programming but in other areas such as data management, time management and just life in general. What is this magical paradigm? Let me explain.

Writing functions and Methods: the old (bad) way

When one first starts out programming, it’s easy to encapsulate a lot of functionality in one code block. I would often find myself writing one PHP function that would fetch things from a database, process them, insert changes into the database, generate HTML blocks based on the data,  etcetera. It was also very common to find I’d written a piece of javascript that gets elements, does an ajax query, processes the data and then loops over those elements inserting data and creating events for those elements.

Not only is this way of writing code confusing, it also makes tracking bugs nigh on impossible and I won’t even breach the subject of maintainability. There’s a harsh lesson to be learned when you first have to go back to a piece of badly written code, be it yours or someone else’s, and you can’t for the life of you work out what goes where, in what order it happens and what piece of code actually does what.

If you do this, you’ll often find functions like this:

function getResultsAndProcessAndInsertAndGenerateHTML(query){
// Escape Query
$query = someEscapeFunction($query);
// SQL statement
$sql = "SELECT * FROM table WHERE table.param LIKE " . $query;
// open connection
$con = mysqli_connect("example.com","peter","abc123","my_db");
// executre query
$result = mysqli_query($con,$sql);
// loop over results
$html = "<div>";
while($row = mysqli_fetch_array($result))
{
$html .= "<div>" . $row['id'] . "</div>";
}
$html .= "</div>";
// close connection
mysqli_close($con);
echo $html;
}

Not pretty…this one function is responsible for doing 5 different things:

  • escaping the query
  • creating the SQL statement
  • executing the statement
  • looping over the results
  • generating the html

This might seem okay for a one of, specialised function, but it really is bad programming. Let’s say you now had 10 of these sorts of functions scattered throughout your application and you needed to change your MySQL database password and/or username. Especially if the code is spread across multiple files, this can become a nightmare.

But I use config files, so that doesn’t really matter! – anonymous

Okay, I hear you, you can eliminate a lot of the code-ache by using config files, this is true, but bear with me. Now, let us propose that you want to migrate your database from MySQL to PostgreSQL. Not so easy now is it?

Abstraction

Picasso abstract painting

I’m going to say this loud and clear so everyone can hear it. With a few exceptions:

When describing a block of code, if you have to use the word ‘and‘, you’re probably doing it wrong.

By block of code, I’m generally talking about functions, methods, events and other callable items, but it applies to imperative programming structures as well, to a certain extent. Any programming language that has code blocks (most high level languages) should adhere to this paradigm.

But what does that have to do with Abstraction? This is just breaking code into smaller, more manageable chunks. – anonymous

Ok, I hear you again anonymous, it does seem like we’re just breaking our code down into a greater number of defined steps, but there is greater work at play here. Abstraction is one of the most  important concepts in programming (and, I find, in life in general). You may be familiar with the idiom DRY, Don’t Repeat Yourself. Taking our code above, we could split it into no less than 5 different functions. A function for:

  • escaping the query
  • creating the SQL statement
  • executing the statement
  • looping over the results
  • generating the html

So now, we have some flexibility to our system.

You can’t fool me! That’s exactly the same list as before! – anonymous

Okay, you caught me, but hold on. Let’s go back to our situation where we had the same code repeated 10 times throughout our application. Instead, now, we have a modular pattern that we can re-use. We can build one function for escaping the SQL that can be re-used. We can make one function that creates the SQL statement, one that executes it and just one for looping over the results. The only thing that actually changes on each implementation is the generating the html function (although, maybe that could be re-used as well).

After all this, we have a set of four abstract functions that perform one specific piece of functionality, and you’ll notice that their functionality can be clearly defined without using the word ‘and’. This is really important, because if we want to change any little aspect of this application, we can now do it with relative ease.

Are you migrating to PostgreSQL? No problem, just change the executing the statement function (and possibly the generating the SQL function). Has a new standard for escaping queries come about? No problem, just change the escaping the query function. All this can be done without having to change the front end part of the code (generating the HTML content).

Frameworks

At this point, you may be saying:

But hey, that means writing an awful lot more code and doing an awful lot more thinking if I’m just working on a simple project. Isn’t there an easy way? – anonymous

You’ve hit the nail right on the head there.

You’re just flattering yourself, you’re writing these little excerpts, you can’t just pretend someone else is actually asking these questions. – me

Anyway, this is where frameworks really come into their own. In general, programming frameworks provide an excellent level of abstraction of commonly used functionality. Some frameworks strive to abstract your work entirely so that you can focus on the front end development of a site without having to worry about the back end implementation at all.

Drupal

Drupal

An example of this would be Drupal. While Drupal is more of a CMS, the way in which is works means that people can interface with the cogs and gears while still having a layer (or three) of abstraction between them and the actual, under the hood workings of the site.

MVC

Other frameworks intend to create a ‘convention over configuration’ style of application building that encourages this abstraction without taking away any of the low level access. One of the more popular patterns is the MVC  (Model, View, Controller) architecture. This particular method of programming is very popular and is used a lot in software to split the functionality of an application into three separate parts, the Model which is responsible for data, the View generates the actual output and the Controller which is responsible for passing and parsing messages between the other two (see what I did there?).

Zend

Zend

One very popular framework that adheres to this architecture is the Zend Framework 1. Zend Framework 2 has moved onto a slightly different pattern, but still applies the same principle. Zend provides a huge basis of functionality for building websites and an enormous library of functions to use in your application, from Database access to Web service access, from creating a REST API to simply creating web forms.

These abstraction layers make it very easy to go in and add functionality, maintain code, find bugs, change functionality, etcetera. There are lots of frameworks out there that have similar architectures:

  • Yii
  • CodeIgniter
  • CakePHP
  • FuelPHP

(nice list of frameworks here on the wikipedia page).

Each have their ups and downs, their pluses and their minuses, but I find Zend has excellent documentation, a huge library of functions, a huge user base and a great community (it also has 1000’s of publicly available libraries on places like GitHub).

Other types of  Frameworks

If you don’t fancy building your whole site using a framework, then there are plenty of other options that can still make your life a hell of a lot easier. Some come in the form of libraries, some as frameworks, but you’re kind of blurring the line between library and framework sometimes.

Doctrine

One of my favourite frameworks is Doctrine. Doctrine is a database abstraction framework that will completely change how you interact with databases. It’s really two things, an ORM (Object Relational Mapper) and a DBAL (DataBase Abstraction Layer). Never again will you have to write a piece of SQL, woohoo! Doctrine can really help you to build normalised database structures with great access to the data by using objects instead of the data itself.

I won’t go into details here, but I suggest reading through their documentation to get an idea of why you can’t live without it.

There are numerous other libraries and frameworks out there, but I just had to mention Doctrine explicitly. Someone has already composed a great list of useful PHP libraries and frameworks so I’ll direct you there.

In life

We can take this ethos and apply it to life in general. There are many situations where we are found having to organise people (in the workplace), possessions (at home), tasks (for oneself). The act of breaking things down into simple, abstracted steps can be a powerful tool to raise morale and to see tasks through to completion.

In fact, it’s so important I’m going to shout it out, loud and clear:

When describing a task or aim, if you have to use the word ‘and‘, you’re probably doing it wrong.

In the army

Roman-Legionary-Soldier-Stock-05

An officer in the army was recounting to me how orders were given out when instructing soldiers. It all revolved around one central ethos: you can only ask people to do one thing at one time. It was the job of the officers to take commands from above, for example:

Major, I want you to get supply X to position Y by 18:00 tomorrow.  – Colonol

It would then be the job of Major to break that down into smaller sub-tasks:

Squad A, I want you to get the trucks ready for 26 soldiers for 8:00 tomorrow

Squad B, I want you to plan a route for 26 soldiers from Z to Y by 8:00 tomorrow




Squad C, I want you to make sure the equipment is ready for 26 soldiers for 8:00 tomorrow 

Of courses, the Colonol would himself have a greater task given to him from above:

Colonol, I want you to secure the base at position Q by 12:00 Tuesday. – Major General




And the chain continues, though the further up you get, the more you start to get into the politics of commands, which is for a whole other article (dealing with clients, anyone?). This is very similar to our programming technique of breaking down a task into abstracted sub-tasks. You might have a squad of mechanics, a squad of marksmen, a squad of techs, etc. This directly relates to having one function for doing X, a function for Y and a function for Z.

You wouldn’t create a unit that can only do one, very specific and particular task such as: taking supply X to point Y along with platoon A, so why would you do it elsewhere?

Notice how none of these commands have the word ‘and‘ in them. This leads to effective, fast and hard working soldiers.

In the workplace

Unhappy Employee

I hear a lot of people talk about their experiences in the workplace as being daunting, confusing, and generally a miserable experience. Raise your hands if you’ve ever said one of these things:

  • There aren’t enough hours in the day
  • I have so much to do, but not enough time to do it
  • I’m supposed to do XYZ but I don’t know where to start
  • The people around me all seem to know what they’re doing, but I don’t have a clue

Depending on where you are in the organisation of things, you’ve either heard people say at least one of these things or have said it yourself, I can almost guarantee it.

It’s important to note that this isn’t the single cause of all problems in the workplace, but a lot of problems can be traced back to this notion.

Let’s take an example:

Worker Ant, the new EU guidelines have come in. I need you to take a look at them and see if you can understand it and if you have the time, I want you to create a new budget and create a new budget application form and send it out to the shops for the end of the month. – Bossman

Or maybe:

Worker Bee, we’re trying to improve customer relations, I want you to research our customers and try to come up with a new ad campaign. And if you could also create a sample ad and upload it to the shared workspace, that’d be great.Bossman

In these tailored examples, Worker Bee and Worker Ant have been given a whole set of things to do. Especially if you’re new to the job, these requests can be very daunting and leave you sitting at your desk not really knowing in which direction to turn or where to start.

You can help to make the workplace more efficient and effective, whether your Bossman or Worker Ant/Bee, by breaking these tasks down into further sub-tasks.

Bossman

If you’re Bossman you could, instead of issuing confusing, complicated orders, you can help out your employees by creating a list of tasks or issuing the tasks one by one:

  1. I want you to read the new EU guidelines by tomorrow afternoon (they can be found on the EU gov website)
  2. Can you make a list of what guidelines apply to us?
  3. Now that you understand the guidelines, can you alter our budget to fit the guidelines by the end of the week?
  4. Great! Thanks, now if you could create a budget application form that the shops can use, we’re almost there.
  5. Fantastic, now that’s all done, we can send them out to the shops.

Of course, you don’t want to be micromanaging things all the time, so once the employee has become used to how things work, you should start being able to give out simpler tasks:

  • Can you create a new budget form based on the EU guidelines that came out today?

And a seasoned employee should know how to break this task down. Which brings me to:

Worker Bee

If you’re receiving complicated instructions from above and have no idea how to deal with it, the using this paradigm can really be a lot of use to you. It follows the same guidelines above: split a task down in to manageable chunks and treat each of these tasks separately.

Example

The year is 2013, and let’s say you’ve been given the task:

Task: Set a price for the existing widget X for 2014.

Sounds like a simple enough task, but

  • How do you do it?
  • Where do you start?
  • Who should you speak to?

Again, especially if you’re new to a place, this can be very daunting and make you feel miserable because you have no idea how things should be done. Let’s break this down into some simple sub-tasks that will turn a large, complicated task into a set of easy to manage problems:

Step 1: How was the last price set?

Great, you’ve given yourself one simple task. There’s a clear starting point and a clear end point. You could even go and break down this task:

Step 1a: Find out who was responsible for setting the price last year.
Step 1b: Speak to them to learn how the price was set.




So we move on to the next logical step:

Step 2: Determine what has changed since 2013 that might alter the price.

This is great! You can just create a list of things you need to look at:

Step 2a: Has demand changed?
Step 2b: Have costs changed?
Step 2c: Has your/another company released a new, competing product?




Etcetera, obviously details will change depending on the industry. Then, finally, we have the last step:

Step 3: Use this information to set the price for 2014.

Which of course can be broken down again, but you should get the idea by now.

Your ability to take a task or a goal, analyse it, and break it down into manageable chunks is what will separate you from your peers. That’s so important, it’s worth repeating:

Your ability to take a task, analyse it, and break it down into manageable chunks is what will separate you from you peers.

Other applications

You can apply this ethos to almost anything. There are so many places where simplifying things can help tremendously in improving effectiveness and efficiancy.

Workshops

Take workshop tools. Nearly all the tools in a workshop have one task. When you get a magic, do-it-all screwdriver, hammer, blowtorch, quad bike, nail trimmer, pliers, circular saw, it tends to do all of those things really badly. If you need a drill, use a drill. If you need a screwdriver, use a screwdriver. If you’ve ever had those ‘all in one multi-tools’, more often than not you’ve probably found yourself reaching for the real equivalent of the tool you want rather than the multi-tool.

Home organisation

I’m sure we all have places in our homes that are an absolute mess. I think everyone has a cupboard for ‘chucking things in that you can’t find a place for’. But, if you give things/places a simple, defined role, it tends to be a lot simpler to keep things tidy. As my grandpa used to say:

A place for everything, and everything in it’s place

Writing

If you’re writing something, it can really help to break down parts of your writing to clearly communicate one thing. It can muddle up readers and make it really confusing if you’re trying to get more than one thing across in one paragraph. Have a clear plan of what you want to communicate in the next block and go back and check that you haven’t tried to put too much in one place.

Study

If you’re studying at school or university, it can feel extremely daunting to have to learn everything. By being able to break down your study into manageable chunks, you can increase your learning potential tenfold.

  • Today I will learn Homogeneous Differential Equations
  • Tomorrow I will learn about Green’s theorem
  • The next day I will learn the proof for Euler’s identity

and so on. If you break it down into manageable chunks, you can get a lot further than if you try and take it on as a whole.

Relationships

If you’re having relationship troubles, try breaking down the problem into smaller sub-problems. You’ll often be able to look at the problems in a different way and it will help you to analyse what the true cause(s) of those problems are.

Conclusion

When you start scratching at the surface of the word and, you peek through to a much simpler and much more effective life. Beneath it are layers of techniques that can help you in not just programming but in every day life. The power of abstraction that getting rid of the word and gives us is immense; ridding ourselves of this burden enables us to tackle tasks that are much larger and more complicated than we could ever tackle as a whole.

I’d like to hear your thoughts on this philosophy, if you have any opinions then I’d love to hear them in the comments and if you have something really interesting to say I’ll update the article or include it in future posts.

Do you have any other features of programming that help you in every day life? Share them with us by writing them in the comment box below!

One Reply to “‘AND’ and why you should avoid it in programming and in life.”

  1. Great article and well written and captured the essence of why so many people lack focus in their lives and I enjoyed reading it and…. (oops, and perhaps I didn’t learn a damn thing)

Leave a Reply