Quantcast
Channel: a computer hobbyist's notebook - Pseudocode
Viewing all articles
Browse latest Browse all 4

Abstraction for Newbies

$
0
0

This post isn't for any specific reason, but it's been bouncing around in my mind for a long time. That higher levels of abstration is a "good thing" is given among experience hackers, people into functional programming, and people who write SQL. At least up to a few years ago, there was some hostility toward increasing abstraction, particularly from the anti-SQL set. I railed about this against and entry in a Symfony book. This is another review of the issue.

Instead of a rant, here's an explanation.

Back in the old days, people learned Basic as a first language. People using MS Office still do. So we'll start with a FOR loop in Basic. This was one of the first control structures invented. This loop increments the value of each element of an array:

FOR I=0 to 10
  AR(I) = AR(I) + 1
NEXT I

Then, along came C, which also had a For statement. This was copied by Javascript, which is one of the most popular languages, and a "first language" for many. Here it is in Javascript:

for(i=0;i<=10;i++) {
  ar[i] = ar[i] + 1;
}

For in Javascript looks clunky, because the C designers knew for was just a special case of while. Instead, Javascript has a better for:

for(i in ar) {
  ar[i] = ar[i] + 1;
}

PHP (and Perl) have a foreach. Foreach is basically like for:

foreach($ar as $key=>$value) {
  $ar[$key] = $ar[$key] + 1;
}

You could eliminate the { and } for this one-liner.

SQL goes one level of abstraction higher. It eliminates the looping syntax. In SQL, all data is stored in tables; that's kind of like an array. Since all the data is tabular, there's no need to use the array syntax at all because it's assumed.

UPDATE thetable SET ar = ar + 1

That's one less line of code, and no variables.

Functional programming does a similar trick. The latest versions of Javascript have some features from functional programming, and one of them is map, which applies a function to each element of an array.

ar = ar.map( function (x) { return x + 1; } );

In Scheme, this is:

(map (lambda (x) (+ 1 x)) ar)

In Scheme you don't typically re-assign variable values, so I've omitted this part. (The Scheme I'm using is Guile.) Also, you typically define funcitons, so using map looks more like this:

(define (plusone x) (+ 1 x))
(map plusone ar)

That's a totally unrealistic example, of course. All the languages allow you to define functions, but not all of them have map or update, which eliminates the entire control structure. Why is this good?

Well, first off, here's a control structure with an error in it:

for(i=0;i<10;i++) {
i = 1;
}

This loop will never end, because the variable keeping track of the loop has been modified.

for(i=0;i<10;i++) {
i++;
}

This loop will skip every other element. This is a more typical problem.

The most common problem is checking boundaries. We should be testing for i<=10 to be equivalent to the first FOR loop above. We should start with i=1. But, in C-style, that's atypical. We start with 0 and continue until i==end.

for ... in ... is better. Best of all is map. If you have data in a database, don't even load the data into your app: just issue an SQL statement to perform the update.

The problem with frameworks and code generators with an anti-SQL bias is that they encouraged programmers to prefer loops to these more abstract forms that eliminate loops, and prefer fetching data over running SQL statements.

This is not meant to single out Symfony, which seems to be a good framework. It's more the anti-SQL bias expressed in that book.


Viewing all articles
Browse latest Browse all 4

Trending Articles