How to Shoot Yourself in the Foot in Various Programming Languages

C:

shoot self in foot with gun;

If the gun wasn't loaded, you get a segmentation fault.


C++:

gun.shootAt(self.foot());

If the gun wasn't loaded, you still get a segmentation fault.


Java:

boolean success = false;
while(!success) {
  try {
    gun.aimAt((Target)self.foot().center());
    gun.trigger().pull();
    success = true;
  } catch (GunNotLoadedException e) {
    gun.add((Bullet)ammo.pop());
    success = false;
  }
}

Avoids segmentation faults, but if you don't have any bullets left, your program will crash with a NullPointerException.


FORTRAN:

stab self in foot with sword

Guns haven't been invented yet.


Assembly:

 

Any program you write will come back and shoot you in the foot eventually anyway, so why bother?


Scheme:

(let ((shoot-foot (lambda (gun target)
			  (if (loaded? gun)
			      (fire-at gun (extract-foot target))
			      (shoot-foot (insert bullet gun) target)))))
  (shoot-foot gun self))


Shell Pipeline:

aim -t `self --display foot` | xargs gun --shootat

You'll know it worked if your foot hurts. You'll know it didn't work if the pipe's exit status is not zero.


Make:

hole-in-foot: gun bullet
        load --use bullet gun
	aim-at --with gun foot
	fire gun

If you have no bullets, make will politely inform you that you are out of ammo, with

make: *** No rule to make target `bullet', needed by `hole-in-foot'.  Stop.

Then again, it will do much the same thing if you don't have a gun.


Perl:

shoot foot;

Perl will automatically find a gun for you, load it if needed, and, since you didn't specify whose foot to shoot, will default to shooting your own.