Skip to content

Home

GOTO isn't evil

In reference to Sara's patch to implement GOTO in PHP:

It saddens me that people are so easily brainwashed by their teachers/peers. Sure, from a high level perspective you don't need a GOTO statement, and the argument that it, when misued, can lead to unreadable code is a good one.

However, I feel that the inverse argument can also be true: complex code with lots of nested if statements and control loops can be also be hard to follow when you need to "get out of jail" fast. Without goto you often need to resort to tricks (in C) such as "do { ... break; ... while (0)", or if you have nested switch's inside loops, create a flag variable and take several steps out of each nesting level. There is no way in hell you can convince anyone of sound mind that those tricks make the control flow easier to understand than a GOTO.

No doubt the anti-goto brigade will campaign that the code could be split up into different functions etc. etc. etc., but it is quite often the case that real programmers need to have this stuff in one function; just take a look at kernel code written in C, for example.

Arguments against GOTO on the following grounds are, IMO, just a bit short-sighted:

  • I think GOTO is bad because some guy wrote a paper on it
  • I was taught not to use it. (But don't know why; it's supposed to be bad, or something).
  • I was taught not to use it because you can use something else.
  • You can use exceptions instead; who cares about performance

Regardless of who wrote the paper, or what you were taught, or your disregard for performance (which does have a big impact in the real world; Exceptions are expensive), GOTO is sometimes needed to get the most out of your code. I'll even go so far as to say that most of the people making these anti-goto comments have no practical experience either of using goto, or with dealing with someone elses misuse of goto; if you don't have experience, please shut the hell up :-)

GOTO is one of those tools that you don't always need, and one where it is hard to define precisely when you should use it (<insert anecdote about professor who determined only one extremely rare case you can't handle without goto>). It boils down to what is appropriate for the task (and language) at hand.

Use your brain and evaluate the project--don't just blindly follow what others have said in the past; make up your own mind. If you truly believe that a goto makes a certain section of code more readable, go ahead and use it. If you deduce from benchmarks that exceptions, nested loops with flag variables or whatever are too slow or otherwise too expensive, you have have a perfectly valid reason to use a goto instead.

I'll sum up with a "GOTO doesn't kill code, bad programmers do". Just because misuse of GOTO can be harmful, it doesn't mean that it shouldn't be part of your toolbox, and for that reason I would welcome a GOTO statement in PHP.

More on PDO

Just published today on the Oracle Technology Network is my article on PDO, giving an overview of what it is, installation, sample code, portability and so on.

Although it has a focus on Oracle, pretty much everything covered there should be portable to the other available pdo drivers with minimal changes (essentially just the parameters to your connection), if you are writing portable SQL.

Please do read it, even if you're only mildly curious, as the stipend I earned from writing the article effectively paid for some of my time working on PDO. As it was actually written a few weeks back (OTN has a long publishing process), the "Future of" section is missing details of the iterator (eg: foreach) support that Marcus keeps nagging me to merge in.

PHPScript

PHPScript is PHP's own ActiveScript interface. In practical terms, this means that you can use PHP from within any application that can host ActiveScript engines. The list of known hosts includes, and is not limited to:

  • Windows Scripting Host (WSH), cscript.exe/wscript.exe
  • "Classic" Active Server Pages
  • Microsoft Scripting Control, and by extension any application that loads that control
  • psvActiveScript Control for Delphi applications
  • Weaverslave an IDE
  • Kapsules a desktop widget engine

Installation

  • Download and install PHP 5
  • Download PHPScript and put it in your c:\\php5 directory
  • run regsvr32 c:\\php5\\php5activescript.dll

Usage

Windows Script Host

Create a .wsf file like this:

   <job id="test">
       <script language="PHPScript">
           $WScript->Echo("Hello");
       </script>
   </job>

or invoke your .php script directly from cscript.exe or wscript.exe using the //E:PHPScript option.

ASP (untested)

    <%@language=PHPScript %>
    <%$Response->Write("Hello"); %>

Script Controls

Set the language property to PHPScript

Script Components / IE

note: it is very dangerous to deploy PHPScript inside IE

Simply use <script language="PHPScript"> for your script tags.

Developers Notes

If you are developing code to run under PHPScript, you need to keep in mind the following differences from regular PHP:

  • echo doesn't have any practical effect. You need to call methods of the host to output information. You may still use output buffering etc., it just won't go anywhere if you flush it.

  • global objects added by the host are created as super-globals, so you need not reference them using the "global" statement.

  • PHPScript will not search for a default php.ini file. It will look instead for php-activescript.ini inside the folder of the .exe process that launched it.

ActivePHP

People seem to have only just noticed this thing, but it's been around for ages (2 years!). Yeah, it's probably broken in PHP 5 in a bunch of places. It's experimental, YMMV. Yes, I care if it doesn't work for you; maybe not enough to use up my spare time (not that I actually have any) debugging windows problems, but everyone understands that you get what you pay for, right?

Wrong

Our brief conversation took place the day before his somewhat "unfriendly" posts. I received 2 emails, not 6.

Andrew:

OpenSource is about contributing, so why not contribute something useful to the rest of the world instead of angry vibes and misrepresenting the people that have already donated so much of their time?

If you want to help yourself, take a look at the CVS and compare PHP 4 with PHP 5. It sounds like there is a buglet in the registry entries; it did work under PHP 4 with WSH, so why not start by comparing those parts?

PS: threaded PHP is a bad idea waiting to crash. That's one of the reasons why we strongly recommend that you don't use threaded SAPI under windows (and even under unix if you can avoid it).

Electric Sheep

It's gotta be the coolest screensaver ever, electric sheep is a continually evolving flame fractal animated screen saver, where your idle cpu cycles are used as part of a distributed computing network to render frames towards the next phase in the evolution (known as sheep).

The name is a little strange, but if you consider idle cpu cycles as dreams, and consider the novel "Do Andriods Dream of Electric Sheep", you'll get the idea. (If you're more into movies than books, "Blade Runner" is for you).

It is a little slow to pick up the first "sheep", but that appears to be a bandwidth problem on their central server. Perhaps they should also distribute the sheep downloads among mirrors, or even peers (Bit Torrent style)?

Anyway, the coolest thing about this screen saver is that is subtly changing the whole time; I have it running on a spare computer next to me and its fascinating.

Bouncing around the USA (again)

A couple of years back, we attended the wedding of my sister-in-law (Aimee Macauley) in Hawaii. We decided to extend our stay and were treated to a big cock-up on behalf of United Airlines that involved flying from Hawaii->SFO->Boston->NY->NY->LHR, instead of the somewhat more sane Hawaii->SFO->LHR.

Well, I've just been visting George (for business, and as it turns out, pleasure too) and my "travel-luck" (those are Dr. Evil finger-quotation-marks) strikes again. The flight from BWI to Boston was delayed enough by some weather (presumably the same lightning storms that delayed my inbound flight a couple of days earlier) to mess up my connecting flight back to LHR (you know; the important one).

This time I'm flying American Airlines and I have to commend them for much nicer service than United. I'm currently sitting in Chicago O'Hare waiting for my alternate flight to LHR. My arrival time has been delayed approx 5 hours, but it turns out that this actually a better schedule for both myself and Juliette+Xander; I get more time to "sleep" on the plane (Dr. Evil finger-quotes again), and they don't have to get up at 4am to collect me from the airport.

Back from LinuxTag

After a surprisingly long day of travel (each leg of the journey isn't that long, but the whole is greater than the sum of its parts), I'm back home in England.

I did't take any photos this year, but if you're curious, Tobias has rounded up a whole bunch.

I'd like to thank ThinkPHP for sponsoring my journey and stay at LinuxTag again this year; the critical mass of PHP brains at a conference usually results in something new for PHP, and it is good that ThinkPHP recognize and help this to happen.

So what's coming out of LinuxTag? We had a database meeting again this year; last year it spawned PDO, and this year we (most of the major PHP database extension maintainers) sat down and refined some aspects of it. Other cool stuff is hinted at here, and will be very useful if used wisely.

Dead laptop disk == more linux hacking

Update2: moved code to http://bitbucket.org/wez/toshkey/overview/

Update acpid now handles the brightness controls, displays the battery status in the ps list and emits power warnings once you're down to 15 minutes of power. I've also added a little non-root acpid client that will allow you to run your own stuff in response to hotkey events.

I suffered a dead (nearly; it's on its way out) laptop disk almost a week ago, and have been clawing my way back to normality.

As a side effect, I now own a Toshiba Satellite M30, which apparently has slightly more linux friendly hardware than my other Satellite (the one that's having issues).

One of the cool things is the toshiba_acpi module; it works in this model and allows access to the hotkeys so you can map them exactly as you like. Since running a standalone daemon for this sucks (you can choose either a python script or a slightly-overweight fnfxd), and since there was a feature request on the ToshibaAcpiDriver page for it, I've written this patch that adds toshiba key support to acpid (1.0.3).

Toshiba keys are exposed as button/toshiba events, followed by the 16-bit hex code for the key that was triggered, so stick some scripting magic into /etc/acpi/events and you're happily-a-mapping those keys.

Don't underestimate the power of xargs

I know this sounds lame, but I have never really needed it until now. I've recently been really busy with a new job taking up my working hours, and some renovation on our home taking up my time off, so I haven't been keeping on top of spam mail.

I run postfix as my MTA and have that apply spamc and anomy sanitizer to incoming mail before piping the mail into cyrus imapd. I use IMAP for mail as I could be using any of a number of different machines here to check my mail. This makes training the spam filter a little difficult, so I have a simple training-by-filing setup:

I have INBOX.junk, INBOX.junk.ham and INBOX.junk.spam. The first of these collects spam recognized by spamc during delivery. The user will then copy mail from junk into junk.ham if is was a false positive, or copy mail from INBOX into junk.spam if it wasn't recognized as spam.

Periodically, I run a script to train based on the contents of the ham and spam folders. Now, the problem I had was that I'd left way too much spam hanging around in the spam folder (the script doesn't do any pruning), and so it was failing due to the excessive number of command line arguments.

So, the moral of the story is: always use xargs for maintenance scripts, even if you don't think you'll need it.