The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Friday, January 11, 2008

I think I've turned into a programming curmudgeon

I hate dynamically typed languages.

Perl, PHP, Python, Lua, the whole lot.

Why?

Because programmers who program in such languages are muddleheaded thinkers who hate to declare variable types because they're too lazy to think and find it fun to “organically grow” their code bases.

It doesn't help that PHP (the current focus for my rage right now) is the ultimate in “scripting languages du jour,” where even minor releases are incompatible with each other.

I'm installing a PHP app (what that particular package is doesn't matter) but it's having problems connecting to the database (PostgreSQL in this case, and yes, it's one of the few PHP apps that actually acknowledge the existance of a database other than MySQL). So, I log into phpPgAdmin to make sure the appropriate PostgreSQL user can access the appropriate PstgreSQL database, only what do I get?

Warning: Invalid argument supplied for foreach() in /var/www/html/db/postgres/privileges.php on line 187

Alright … what's the line in question?

foreach ($privileges as $v) {
...
}

$privileges isn't mistyped (a common problem in a langauge where you don't have to declare your variables). I check some documentation and yes, that's the correct syntax, but comments from the peanut gallery are going on and on about foreach breaking on copies of data or something; stuff that isn't reassuring.

So I rewrite the code (remember now, I'm trying to install SomeRandomPHPApp, I am not trying to debug phpPgAdmin):

reset($privileges);   // XXX spc
while(list(,$v) = each($privileges)) { // XXX spc
//foreach ($privileges as $v) {	// XXX spc
...
}

And try my call again reloading the page:

Warning: reset() [function.reset]: Passed variable is not an array or object in /var/www/html/db/postgres/privileges.php on line 187

Warning: Variable passed to each() is not an array or object in /var/www/html/db/postgres/privileges.php on line 188

Okay, what exactly do I have? $privileges is obviously not an array. Okay, more debugging (“I'm not even supposed to be here today!”).

$privileges = $data->getPrivileges($object, $_REQUEST['subject']);
echo "TYEP: " . gettype($privileges) . " : $privileges"; // XXX spc

And what do I get?

TYEP: integer

Warning: reset() [function.reset]: Passed variable is not an array or object in /var/www/html/db/postgres/privileges.php on line 188

Warning: Variable passed to each() is not an array or object in /var/www/html/db/postgres/privileges.php on line 189

Oh lovely. getPrivileges() is now returning an integer, and the sizeof() function of PHP is returning a value larger than 0 (since the next thing done right after calling getPrivileges() is a call to sizeof() to see if getPrivileges() returned anything of any appreciable size) because an integer has a size, don't you know?

Oh, so what's the actual value of $privileges?

-3

Probably some internal error result deep from the bowels of PHP.

And not an array, like the programmer who originally wrote this crap expected.

Had there been some real typechecking going on I wouldn't be subjected to this type of error and the programmer would have been forced to think about the situation.

Hmm … actually, now that I'm reading up on sizeof(), I think the blame for this is defintely with the crack-addled developers of PHP. sizeof() is an alias for count(), which in part, reads:

Returns the number of elements in var, which is typically an array, since anything else will have one element.

PHP: count

“Typically.” Oh, I love that bit.

Okay, so if sizeof() (aka count()) will return a count of 1 for non-arrays and not signal any type of error because you “typically” use this on arrays, then why does foreach() barf on a non-array? Couldn't it just loop once? If sizeof() will treat a non-array as an array of one, why can't foreach()?

I mean, isn't that the purpose of a dynamically typed language? To act reasonably in any given situation? To not care if something is an array, list, vector, scalar, hashtable, or carrier pidgeon?

Hmmm … on second thought, that still doesn't absolve the programmer of phpPgAdmin—since there still is the issue of getPrivileges() returning an integer instead of an array of arrays (at least, that's what the code seems to be expecting).

And that still leaves my original problem currently unsolved.

Obligatory Picture

An abstract representation of where you're coming from]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

Obligatory AI Disclaimer

No AI was used in the making of this site, unless otherwise noted.

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: https://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

https://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.