31 July 2006
Keywords Compared to Functions
In programming, there are two language constructs. There are functions, both built-in and user-defined (thought built-in can always be user defined), and there are keywords. This makes sense– how could you define a function called “function”? Ignore the syntax problem…
function function ($args) { /* how to let code get in here??? */ }
As I realized during my never completed PScheme project earlier this year, there are certain things which simply cannot be expressed as functions because of how they’re evaluated. Take if, for instance. Most functions must evaluate all their arguments. But if will only evaluate the first argument, than whichever (if any) ensuing argument if necessary.
Keywords are increasingly necessary as languages are executed at ever increasing levels of abstractness from hardware. Objects could be expressed in procedural languages, but now there is an Object keyword. “Ah!” but you say. “I can define an empty class named Object!” And right you are. Except, your empty Object class actually implicitly extends Object (or in PHP, stdClass). The root object really truly is a keyword, because it cannot be expressed as a user function.
Though PHP gets a fairly bad reputation for being newbie friendly, or interpreted, or whatever else, its actually relatively straight forward in that it has a minimal number of keywords. Offhand, I can think only of these:
- print/echo
- include/include_once/require/require_once
- if
- do/while/loop
- function
- class
- stdClass
- eval
- and/or/not
- get_defined_vars
Get defined vars?
Get_defined_vars operates on variables in the current scope, not the function scope. How could you possibly track that? Its a useful function, especially when writing implicitly evaluated code. Once you get two or three $$’s floating around, you have no idea what is defined. But it is a magical function, because you could never write it.
Compare that to Perl. Perl effectively has these kind of statements, like shift. Surely you’ve seen sub escargot { my $obj = shift; .... } The funny thing about Perl is that through its use of implicit variables all over the place, shift is really just an ordinary function. It might as well be a keyword, because of how its being used, but you really can drill down deep enough to find a Perl-language implementation of the function in question. This is also true of Objects. Perl 5 Objects are implemented IN Perl. The package construct may be a keyword (I honestly don’t know).
But consider keywords to be magical. They are needed — we’re not programming in assembly anymore. And there are times they’re perfectly obvious. But keywords that cannot be debugged being used in creative ways makes things difficult for new programmers. Not new in a Junior sense, but new in a I’ve-never-touched-this-before sense. If you ever feel like you need to use get_defined_vars (and there are times! I’ve found it particularly useful in fixing variables scopes in in-house systems) consider at least wrapping it and heavily commenting the call. Posterity will think you.

