Why I Believe That Programming is an Art

I find programming quite fascinating – the way a few lines of code can complete really complicated tasks is quite remarkable, especially considering the speed at which most programming languages run. However, I also know many people who not only do not care about programming, but absolutely despise it. I find this rather strange. After all, who wouldn’t want to make their life easier by automating everyday tasks?

Programming does requires a great deal of patience and even after that, you will occasionally encounter a wave of frustration resulting from a seemingly invisible bug. It also requires discipline – A programming language isn’t like a spoken language. No mistakes are tolerated. Even the most insignificant missing semi colon sign will result in a big fat error. And that is exactly what I love about it. The fact that a specific order of words (also known as syntax) will always process input in the same way and produce output accordingly makes programming the perfect job for those that love logical thinking.

Where’s the ART in programming?

Knowing that programming is so logical, where exactly is the art in programming? After all, there’s no real creativity involved is there? Simply follow a number of rules and use the correct syntax and you can complete even the most complicated of tasks. Wrong! While programming does involve lots of logical thinking, this does not mean that one specific task must always be solved with the same piece of code. The same operation can be completed in a number of ways. Which method used completely depends on the programmer and is in fact what defines a good coder and a bad coder.

To give you an example, say I have been given three numbers and I have been asked to find their sum using a programming language. I will use PHP to demonstrate that there are different ways to achieve even such a simple thing.

Method 1

<?php
$iNumber1 = 5;
$iNumber2 = 10;
$iNumber3 = 2;
$iResult = $iNumber1 + $iNumber2 + $iNumber3;
echo $iResult;
?>

Method 2

<?php
function addNumber($iNumber1,$iNumber2,$iNumber3) {
    $iResult = $iNumber1 + $iNumber2 + $iNumber3;
    return $iResult;
}
$iNumber1 = 5;
$iNumber2 = 10;
$iNumber3 = 2;
echo addNumber($iNumber1,$iNumber2,$iNumber3);
?>

Method 3

<?php
function addNumber($iNumber1,$iNumber2) {
    $iResult = $iNumber1 + $iNumber2;
    return $iResult;
}
$iNumber1 = 5;
$iNumber2 = 10;
$iNumber3 = 2;
$iSum = addNumber($iNumber1,$iNumber2);
$iResult = addNumber($iSum,$iNumber3);
echo $iResult;
?>

Method 4

<?php
class Number {
    private $iResult = 0;
    private $iNumber1 = 0;
    private $iNumber2 = 0;
    private $iNumber3 = 0;

    function setNumber1($iNumber1) {
        $this->iNumber1 = $iNumber1;
    }
    function setNumber2($iNumber2) {
        $this->iNumber2 = $iNumber2;
    }
    function setNumber3($iNumber3) {
        $this->iNumber3 = $iNumber3;
    }
    function addNumbers() {
        $this->iResult = $this->iNumber1 + $this->iNumber2 + this->iNumber3;
    }
    function getResult() {
        return $this->iResult;
    }
}
$oSum = new Number();
$oSum->setNumber1(5);
$oSum->setNumber2(10);
$oSum->setNumber3(2);
$oSum->addNumbers();
$iResult = $oSum->getResult();
echo $iResult;
?>

Method 5

<?php
class Number {
    private $iResult = 0;
    private $aNumbers;

    function __construct($aNumbers) {
        $this->aNumbers = $aNumbers;
    }
    function addNumbers() {
        $iSum = 0;
        foreach ($this->aNumbers as $Number) $iSum += $Number;
        $this->iResult = $iSum;
    }
    function getResult() {
        return $this->iResult;
    }
}
$aNumbers = array(5,10,2);
$oSum = new Number($aNumbers);
$oSum->addNumbers();
$iResult = $oSum->getResult();
echo $iResult;
?>

That should be enough. As you can see, there are several ways to accomplish the same task (there are definitely even more ways than I showed you). I’m sure you will agree that the last two examples seemed unnecessarily long. For the task we were trying to do, they obviously are.

So why bother with the long version?

Here’s where Loose Coupling, Strong Cohesion comes in. In programming, the looser the Coupling, the more you are able to modify individual components without modifying others. On the other hand, strong Cohesion means your components have very clearly defined and specific roles and do not do everything themselves but instead use other components for logic not directly related to the component’s primary responsibility.

Since the above example has addNumbers as a separate method, this means our class has strong cohesion – each method has a specific aim. Had we included the logic in getResults, this would mean that the class would only ever be able to add numbers. As it is now, we can easily add a new function called multiplyNumbers.

Additionally, since we are passing an array to the constructor, and not a list of parameters, we can easily vary the number of numbers we pass. This makes our class have loose coupling. It is flexible and expandable. We can use it to add two numbers, three numbers or even a thousand numbers if we choose.

Whenever coding components, you always need to keep three keywords in your head. Re-usable, maintainable and above all, easily extendable.

Doesn’t that over complicate things?

The reason I just explained the above is because it is important to realize that an application that does not use strong cohesion and loose coupling will still work fine – but is it good programming practice?

When there are so many different ways to complete the same thing, it is all too easy to go for the quick option. However, you will find that not always going for that quick option and looking at the bigger picture will improve your programming skills and save you time down the line when you discover that the function you had created needs to be used elsewhere. The ability to re-use an old function and easily extend its functionality is exactly what makes programming so enjoyable and rewarding.

And this is exactly why I think programming is in fact, an art.

Don't forget to check out more posts from the Tutorial of the Week section.

blog comments powered by Disqus
.