Tuesday, August 21, 2012

Missing Features

Here's a super-quick algorithmic exercise.
Your input is a list of integers, your output needs to be the number of times the number 3 appears in the list. Design an algorithm by this specification, but write it completely in English, without programming language syntax.
Go ahead, I can wait, this algorithm has barely 4 lines anyway.

Assuming a minimal grasp of both English and common sense, your algorithm probably has a bit along the lines of "If ai equals 3, increase counter by 1". Except for the hardcore programmers among you, I can't believe anyone's algorithm said "If ai equals equals 3", because that's not the sort of things humans say.
Then why the hell, pardon my Klingon, are programmers expected to make this stupid translation in their heads?

Don't tell me you've never had this problem. You sit down, writing a few thousands of lines in C, and after rigorous testing you find out that this one condition is always true, no matter what. And glancing at the code won't reveal this problem, either, because a line like...
if (a[i] = 3)
... simply doesn't look as wrong as it should.
Other languages might have a compilation/ runtime error, but it's still annoying; you still have to pay extra attention to one of the most basic things a programmer has to do.

I occasionally think about how existing languages could be made better, and this is something that often comes to mind, because it afflicts every language I can think of except for Pascal, where "A equals B" actually becomes "A = B" without need for strange translation.
You might assume I want to use Pascal's assignment operator (:=) as well, but I actually have greater plans.


Take a look at this simple piece of code:
a = foo() + bar();
Reading it out loud, you'd get something like "a equals foo plus bar". The first function is the assignment into a, the second one is foo(), the third one is bar(). Sounds legit so far?
Suppose we're very worried about our buggy code and we want every single low level function to print into the log, so the assignment will print "1", foo() will print "2" and bar() will print "3", and when you open the log you'll see "231".

I have a lot of respect for declarative, logic and functional programming, but nevertheless most work these days gets done in good old imperative programming. When my imperative code says "run functions 1, 2 and 3" it doesn't make sense to run 2 first, 3 second and 1 third. You've got side effects to think about here.

The whole thing might be inside a try/catch block, and out of functions 1,2, and 3 you might easily find that only number 2 ran. Maybe the assignment operator was overloaded and something strange happened while it was running. Maybe instead of + you have &&, and thanks to lazy evaluation your log says just "21". Even the + operator could be overloaded. There are so many options for this to do almost but not quite what the programmer/ reader would instinctively expect.

Personally, I'd apologize to PHP and Perl programmers and change the assignment operator to look like this:
foo() + bar() => a;
Now the functions are written in the same order that they're called, and the sign known as 'equals' is free to be used for, you know, equality.
It really is sensible, the assignment might be the desired end result, and everything else merely auxiliary functions, but what you do in practice is call a bunch of functions... and then, possibly, in the end, put the results inside a variable if you feel so inclined.


I wanted to write a full post about original features I'd include in my own language, but looks like I only have the => operator and even = for equality has been done (in the ancient and disused Pascal). There are many other features I want, but they all exist somewhere or another... so maybe next time I'll post about various features that make life better but, for some reason, don't exist in most languages.

If anybody reads this (which I doubt), feel free to share your favorite language features that most languages choose to gracefully ignore!

No comments:

Post a Comment