Simplicity Rules - Or Should It ?

Screen Shot 2020-09-28 at 7.14.36 PM

// Object oriented programming

/* 
 Winston
 - nickname
 - age
 - x 
 - y
*/
var Winston = function(nickname, age, x, y) {
    this.nickname = nickname;
    this.age = age + "yrs old";
    this.x = x;
    this.y = y;
};

var winstonTeen = new Winston("Winsteen", 15, 20, 50);
var winstonAdult = new Winston("Mr. Winst-a-lot", 30, 229, 50);

var drawWinston = function(winston) {
    fill(255, 0, 0);
    var img = getImage("creatures/Winston");
    image(img, winston.x, winston.y);
    var txt = winston.nickname + ", " + winston.age;
    text(txt, winston.x+20, winston.y-10);
};

drawWinston(winstonTeen);
drawWinston(winstonAdult);

so this code puts out the enclosed graphic - looking at the 2 lines

var img = getImage("creatures/Winston");
image(img, winston.x, winston.y);

it looks to me like it can be replaced with a single line as follows

image(getImage("creatures/Winston"),winston.x,winston.y);

in a similar vein, looking at the 2 lines -

var txt = winston.nickname + ", " + winston.age;
text(txt, winston.x+20, winston.y-10);

again, it looks like we can replace it with the single line

text(winston.nickname + "," + winston.age, winston.x + 20, winston.y - 10);

so my question is why - if simplicity is the best approach - the lesson was presented the way it was

“Simplest” doesn’t just mean shortest or putting everything on one line when possible, especially in the context of coding. It means least complicated and most easy to understand. Breaking things up like that can make things more clear, taking individual operations and putting them on their own line. It also adds an identifier to those operations that can further be used to clarify meaning or intent. Your example doesn’t show this the best with ‘img’ and ‘txt’ so much, but consider something like (which uses made up functions):

displayToUser(count(enemiesDead)+ timeRemaining + count(userLives))

Do you know what exactly is being shown to the user? If instead you used:

var userScore = count(enemiesDead)+ timeRemaining + count(userLives)
displayToUser(userScore)

Now you know that the calculation being made and displayed is the user’s score. It also breaks up the two things that are happening: generating a score and displaying the score. This makes it easier to read and adjust later if needed.

It can be a hard concept to grasp when you’re new to coding… or when you’ve been coding a while… or maybe after decades of coding up until you’ve actually had to deal with someone else’s code which is hard to understand (and maybe later find out it was your code all along!). But clarity in code goes a long way, even with the little things. And it’s something you’ll find yourself struggling with constantly.

Theres a famous quote related to this that goes:

There are only two hard things in Computer Science: cache invalidation and naming things.

– Phil Karlton

There’s also another variation of this that goes:

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

– Leon Bambrick

The main point is that, something as simple as naming a variable in your code can be one of the hardest things to. What IS the best name for a variable? What name makes the data and intent clear to the user reading it? The world may never know.

image

1 Like

Maybe there’s an idea that you don’t know simplicity until you know complexity. We can (and do) represent most of humanity’s knowledge as a sequence of zeroes and ones, but that’s not how we present it to people.

From your example, you’re saying a pair of statements can be represented with a compound expression. Absolutely!

Among other contributions, Lambda Calculus greatly simplifies anything you can write in a programming language down to something mathematically simple. If a programming language differentiates between statements and expressions: That’s great. But that differentiation almost never survives to the lower-level representation of your code when it runs.


so my question is why […] the lesson was presented the way it was

It’s pragmatically difficult to turn experience into a concise article, which is what people ask of educators. It’s also fine for you to ask these sorts of meta questions.

It’s also worth examining the things you’re not asking about. Why are there semicolons terminating each line? Do they matter? You reduced the spacing in the argument lists. Does that matter?

1 Like

Great explanation as always - thanks! I had thought that we were under an obligation to get the task done with a minimum of lines - that’s why I went off on that tangent. I do see the rationale, now that you have pointed it out, the simplicity of -

var img = getImage(“creatures/Winston”);
image(img, winston.x, winston.y);

versus

image(getImage(“creatures/Winston”),winston.x,winston.y);

even though, as you have pointed out, it’s not that big a deal in this particular instance. I was under the impression that I would have my knuckles rapped for using 2 lines when one could be used. It’s good to know that I don’t have to labor under this misconception any longer. I can see the utility of a few clear lines of code as opposed to one long complex line. Thanks again!