Forum Moderators: coopster

Message Too Old, No Replies

if (false){ read this }

a stupid way to negate code

         

httpwebwitch

5:37 pm on Apr 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a rant.
I inherited a pile of stuff from another developer.

Usually when there's some code in a script that you want to take out, you can "comment it out" (prepend "//" to the line), or just delete the stuff. This developer's favourite method is to enclose the code with:

if (false){
command, command, etc.
}

It's infuriating! I'm going through pages of stuff trying to figure out what it's supposed to do, wondering why there are obvious syntax errors, trying to figure out why it's not working, and ... then I notice these subtle "if" statements scattered throughout like dandruff.

Sheesh!

timster

6:39 pm on Apr 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm...I bet the guy does a lot of FileMaker development. You see that sort of thing all the time in FileMaker scripts, since there's no other way to comment out a block of code.

I myself am occasionally guilty of this abomination against Perl (but I try to expunge it before I set the code loose):

=pod
"commented" out code
=cut

Speaking of Perl, your problem looks like a job for regular expressions. I've got to step into a meeting, but I'll try to get back to you with a way to clean up your code in a hurry.

timster

1:19 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use at your own risk, of course, but this regex might be able to clean out those "if (false)" blocks. Just run the script with the input file as the argument.

If you don't do Perl, this could ported to PHP pretty easily.

#!/usr/bin/perl -w
undef $/;
while (<>) {
s#if\s*\(\s*false\s*\)\s*\{[^\{\}]*(\{[^\}]+\})*[^\{\}]*\}##gs;
print
}

gethan

1:24 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



100 signs of php spaghetti code [webmasterworld.com] - If you get a moment away from tearing your hair out you might want to see how many of the other 99 you find ;)

I must confess I do things like while debugging code - but if it got in to a release I'd be mortified! sneaking away ... > grep if (false)

ergophobe

3:01 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



At the very least I do

if (DEBUG)

and set the DEBUG constant to true or false depending...
Not that I hold myself up as the one true way - after all, I started the spaghetti code thread after looking at something I had just written!

Tom

john_k

3:23 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I feel your pain.

However, it IS a convenient way to comment out larger blocks of code when they contain other comment blocks. I find this happening most when the client can't make up their mind about whether to do something one way or the other. Or their "final decision" is likely to be reversed.

As everyone has said though - it shouldn't find its way into production code.

btw - I often find this accompanied by a s**tload of older file versions that have names including "temp", "old", "backup", "temp2", etc. A good source control app is in need when you start running into this.