Comment On wtflib.php

"While updating on some delightfully unorganized PHP code (no indentation at all, split over hundreds of randomly named files, many included dozens of times), I kept running across comparisons such as: [expand full text]
« PrevPage 1 | Page 2Next »

Re: wtflib.php

2008-06-05 08:15 • by XioPod
tacostand.php

Re: wtflib.php

2008-06-05 08:16 • by Sunday Ironfoot
function ReturnFirst()
{
return "fist";
}

Re: wtflib.php

2008-06-05 08:22 • by Chucara (unregistered)
You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

Brillant!

Re: wtflib.php

2008-06-05 08:23 • by real_aardvark
I'm very impressed by the use of the explode() function.

I'm not too familiar with PHP, but this appears to be the finest example of self-documenting code that I've seen for a long while...

Re: wtflib.php

2008-06-05 08:25 • by ParkinT
function WTFPost($postType) {
if $postType == "Wooden Table"
return "WTF";
if $postType == "QuickBasic"
return "WTF";
if $postType == "Enterprise"
return "WTF";
if $postType == "Paula Bean"
return "WTF";
return "The real WTF is...";
}

Re: wtflib.php

2008-06-05 08:33 • by snoofle
Forgive me as I am PHP challenged:

so: truth('Absolutely No Fucking Way')
and: truth('FALSE')

both yield true?

Re: wtflib.php

2008-06-05 08:38 • by ParkinT
198945 in reply to 198944
snoofle:
Forgive me as I am PHP challenged:

so: truth('Absolutely No Fucking Way')
and: truth('FALSE')

both yield true?

That is correct.
Actually, his function only tests for FALSEHOOD.
Of course, specific cases of falsehood ("no", "false", "off").

Re: wtflib.php

2008-06-05 08:38 • by SchizoDuckie (unregistered)
198946 in reply to 198941
Chucara:
You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

Brillant!


Actually, this makes me think that those scripts were ported from some kind of other language, or generated by some 'multi language' tool. Now that is *some* feature! You just have to output the functions like MatchField instead of language native crap and you run everything through your own lib! Portability yay!

Re: wtflib.php

2008-06-05 08:43 • by Anonymous Cow-herd (unregistered)
TRWTF is that his truth test can't handly FileNotFound.

PHP Considered harmful

2008-06-05 08:51 • by foo (unregistered)
... not because there's anything wrong with PHP, but because so many stupid people use it. When you lower the bar so low all kinds of garbage gets in.

Re: wtflib.php

2008-06-05 08:51 • by jo42
Ok, so which widely used, open source PHP-based project is this from? phpBB? Wordpress? Drupal? ;-p

Re: wtflib.php

2008-06-05 08:52 • by SunTzuWarmaster (unregistered)
Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?

Re: wtflib.php

2008-06-05 08:56 • by Renoir (unregistered)
...but you see, my new, improved, wheel's much better. It's only got four sides. And if you leave it on a hill, it doesn't roll away...

Re: wtflib.php

2008-06-05 09:01 • by ambrosen (unregistered)
Well, it's actually the best written code I've seen here in a while.

Pointless and wasteful, but readable.

Re: wtflib.php

2008-06-05 09:04 • by Nevermind me (unregistered)
198958 in reply to 198941
Too bad if want to use the same field name for POST & GET methods at the same time :(

You'd have to do it the unportable, un-future safe 'normal' PHP way.

Re: wtflib.php

2008-06-05 09:05 • by Ghengis Kode (unregistered)
Sometimes I see code here that make me think it's about time we adopt procedure SDMBS-T
Stupid Developer Must Be Shot - Twice

Better to shoot once in the head and once in the butt. Just to play safe because we have no idea where his brain might be.

</homicidal>
</maniac>

Re: wtflib.php

2008-06-05 09:09 • by Kuba (unregistered)
198962 in reply to 198953
SunTzuWarmaster:

As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?


Disclaimer: I fail to see the sarcasm.

So, um, this isn't C/C++. In php, there only way to get memory leaks is by doing known (hopefully) no-nos, such as circular references. php is a latent-typed, garbage collected language, you know...

As for wastefulness, a good compiler can do compile-time evaluation of certain functions, and this one would be a good candidate. Although such optimizations are more common in languages where the compiler can interpret the code during compilation (Scheme, Lisp, ...), php (just as any other language system) could in principle optimize this function out as follows:

1. the function has no side effects
2. the function is stateless (another way of saying 1, perhaps)
3. 1,2 ==> the function can be evaluated at compile time for constant arguments
4. the function has constant arguments
5. 3,4 ==> the function call can be replaced by the result of evaluating the function at compile time

This can be thought of as compile-time macroization.

Having written a hacked-up Lisp-like system where this optimization is always done bar compile-time cut-offs, I'd say that your point is kinda moot in principle, but valid for IMHO broken implementations of most languages, whether otherwise thought of as "cool" (say OCaml) or "uncool" (say C).

Cheers!

Re: wtflib.php

2008-06-05 09:13 • by I_Smoked_It (unregistered)
198963 in reply to 198953
SunTzuWarmaster:

Do people seriously think that computers are magic?


Yes, and the magic works until you let the smoke out.

Re: wtflib.php

2008-06-05 09:22 • by XIU
198969 in reply to 198944
snoofle:
Forgive me as I am PHP challenged:

so: truth('Absolutely No Fucking Way')
and: truth('FALSE')

both yield true?


Actually it returns whatever you gave it.

Re: wtflib.php

2008-06-05 09:27 • by Zecc
198970 in reply to 198945
ParkinT:
snoofle:
Forgive me as I am PHP challenged:

so: truth('Absolutely No Fucking Way')
and: truth('FALSE')

both yield true?

That is correct.
Actually, his function only tests for FALSEHOOD.
Of course, specific cases of falsehood ("no", "false", "off").

Actually, False (boolean), '0' (string), 0 (integer), 0.0 (float) and Null (Null) - and maybe others I can't recall - will be considered false too, if interpreted as booleans.

Not that this contradicts anything you said.

Re: wtflib.php

2008-06-05 09:28 • by Anon. (unregistered)
Argh, my head
explode()
s.

Re: wtflib.php

2008-06-05 09:30 • by mr-sk (unregistered)
dumbest, shit, ever:

function queryStringToArray($queryString)
{
global $GL_strlen;
global $GL_strpos;
if ($GL_strlen($queryString) == 0) {
return array();
}
$queryArray = array();
$args = explode("&", $queryString);
for ($i = 0; $i < sizeof($args); $i++) {
if ($GL_strpos($args[$i], "=")) {
list($param, $value) = explode("=", $args[$i]);
$queryArray[$param] = $value;
} else {
$queryArray[$args[$i]] = "";
}
}
return $queryArray;
}

Re: wtflib.php

2008-06-05 09:33 • by lyolik (unregistered)
198974 in reply to 198941
Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).

Re: wtflib.php

2008-06-05 09:37 • by Bob (unregistered)
198976 in reply to 198943
They should have taken a picture of the monitor with the code in the IDE, using a film camera, then gotten it developed, then taken a picture of the photograph using a digital camera, (with the photograph on a wooden table)...

Oh yeah, and the code is supposed to make it more like Lisp, making it more Enterprisey.

Oh, and was the programmer's name Paula?

Oh, and I wrote a CGI daemon in QuickBASIC which runs a forum:

http://samanddeanus.no-ip.org/forum

Sources:
http://samanddeanus.no-ip.org/start.bas
http://samanddeanus.no-ip.org/reply.bas

Re: wtflib.php

2008-06-05 09:40 • by Someone You Know
198978 in reply to 198974
lyolik:
Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).


You may want to have your sarcasm detector adjusted.

Re: wtflib.php

2008-06-05 09:52 • by Guillaume (unregistered)
This code has the truth !

Ahhhh ! PHP... Producing noob programmers since the beginning.


CAPTCHA was 'nobis'... no(o)bi(e)s

Re: wtflib.php

2008-06-05 09:53 • by Bob (unregistered)
198984 in reply to 198978
Someone You Know:
lyolik:
Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).


You may want to have your sarcasm detector adjusted.


Scheme/Lisp? Then they'd be built in FUNCTIONS so ha.

Re: wtflib.php

2008-06-05 10:01 • by Munger (unregistered)
function truth($fieldValue)
{
if ($fieldValue == "no" || $fieldValue == "false" || $fieldValue == "off") {
return false;
} else {
return $fieldValue;
}
}


Laugh if you will, but I use this particular construct when checking info which can originate from a Web page (check box/ radio button), a database ('t' or boolean), or a language construct (boolean).

Why repeat the entire test all over the place when a simple function call will do? And if you need to add another test for true/false, then you simply adjust the one function.

But I would return 'true' in the else.

Re: wtflib.php

2008-06-05 10:17 • by Otto (unregistered)
I find it amazing that nobody noticed that you could write the queryStringToArray function like this:

function queryStringToArray($queryString) {
parse_str($queryString, $newarray);
return $newarray;
}

It's always a bad sign when people write functions to duplicate built in functionality.

Re: wtflib.php

2008-06-05 10:21 • by Just Some Guy (unregistered)
if (MatchField($login->Value("admin"), "==", "1")) {


I'm betting that at one point there was a database involved somewhere, or that they planned to have one. The first value was supposed to be the name of a column, and the function's original name was something like CheckSessionValue.

Or maybe they're just enterprisey.

Re: wtflib.php

2008-06-05 10:33 • by aliquam (unregistered)
that function
MatchField($field, $cond, $value)
would be soo much faster with a switch statement :/

Also I guess it's good that ReturnTrue() doesn't return false...

Re: wtflib.php

2008-06-05 10:35 • by real_aardvark
198995 in reply to 198991
Just Some Guy:
if (MatchField($login->Value("admin"), "==", "1")) {


I'm betting that at one point there was a database involved somewhere, or that they planned to have one. The first value was supposed to be the name of a column, and the function's original name was something like CheckSessionValue.

Or maybe they're just enterprisey.
But that's hardly enterprisey at all, is it? I mean, the operator is a literal, for gawd's sake.

Real Enterprisey (c) would be as follows:

if (MatchField($login->Value("admin"),

$db->getOperator (__FILE__, __LINE__),
"1")) {

This allows for instant configurability via a database, and has the added benefit that you can add distributed grid networking by ensuring that $db refers to an object encapsulating some sort of RPC.

I'm still a little concerned about the "1" though.

Re: wtflib.php

2008-06-05 10:42 • by KenW
198999 in reply to 198953
SunTzuWarmaster:
Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?


Very impressive post. I'd almost believe that you had a clue about the subject, except for the fact that you're spouting all that stuff about assembly instructions, registers, and jumps, and totally missing the fact that this is PHP, which is interpreted. In other words, all you did was post non-applicable information.

Nice try, though. If it hadn't been so far out of context, it might have been an impressive post.

Re: wtflib.php

2008-06-05 10:44 • by real_aardvark
199000 in reply to 198941
Chucara:
You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

Brillant!
PHP being PHP, this is sadly not a remote possibility.

Any language that can produce a syntactic wart such as "===" is quite capable of such behaviour.

I look forwards to PHP6 with trepidation.

Re: wtflib.php

2008-06-05 10:46 • by A Few Good Coders (unregistered)
function truth($fieldValue)
{
return "You can't handle the truth!";
}

Re: wtflib.php

2008-06-05 10:48 • by N. K. (unregistered)
199003 in reply to 198994
Hah, this is not really all that bad. On my previous job, people would most likely write it as:

/**

*
*
* @param $field
* @param $cond
* @param $value
*/
function matchField($field, $cond, $value) {
if ($cond == '<')
return ($field < $value) ? true : false;
if ($cond == '<=')
return ($field <= $value) ? true : false;
if ($cond == '==')
return ($field == $value) ? true : false;
if ($cond == '!=')
return ($field != $value) ? true : false;
if ($cond == '>=')
return ($field >= $value) ? true : false;
if ($cond == '>')
return ($field > $value) ? true : false;

if ($cond == 'like')
return strpos($field, $value) === false ? false : true;
if ($cond == 'in')
return strpos($value, $field) === false ? false : true;

return false;
}

/**
*
*
* @param $expression
*/
function isTrue($expression) {
return $expression == true ? true : false;
}


aliquam:
that function
MatchField($field, $cond, $value)
would be soo much faster with a switch statement :/


I hope you are being sarcastic.

Re: wtflib.php

2008-06-05 10:53 • by Alex (unregistered)
199006 in reply to 198989
Otto:
I find it amazing that nobody noticed that you could write the queryStringToArray function like this:

function queryStringToArray($queryString) {
parse_str($queryString, $newarray);
return $newarray;
}

It's always a bad sign when people write functions to duplicate built in functionality.


Yeah, PHP has so little built-ins anyway right ? You'd have to print a 500 page book only to list them all...

This guy was bad for sure... Really... But please stop saying "Oh, but there's a function for that in PHP, loser". I'm pretty sure you've done a function or two that replicates a PHP built-in.

Re: wtflib.php

2008-06-05 11:15 • by ClaudeSuck.de
199010 in reply to 198974
lyolik:
Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).


FORTRAN uses .LT., .GT., .EQ. and so on

Re: wtflib.php

2008-06-05 11:24 • by ClaudeSuck.de
TRWTF is that he explodes the string (poor thing) instead of using his indexof function. To be useful it should be used more often.

Re: wtflib.php

2008-06-05 11:25 • by ClaudeSuck.de
199017 in reply to 199000
real_aardvark:
Chucara:
You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

Brillant!
PHP being PHP, this is sadly not a remote possibility.

Any language that can produce a syntactic wart such as "===" is quite capable of such behaviour.

I look forwards to PHP6 with trepidation.


BTW, maybe this guy wrote all that stuff in PHP version 0.1 and all our wisdom wasn't applicable at that time???

Re: wtflib.php

2008-06-05 11:30 • by Spectre
So the obscure function checks for $plaintext, but if it's not found, adds urlencode($plaintext)?

Re: wtflib.php

2008-06-05 11:40 • by Bob (unregistered)
199028 in reply to 199010
ClaudeSuck.de:

FORTRAN uses .LT., .GT., .EQ. and so on


Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.


Re: wtflib.php

2008-06-05 11:51 • by ClaudeSuck.de
199034 in reply to 199028
Bob:
ClaudeSuck.de:

FORTRAN uses .LT., .GT., .EQ. and so on


Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.




This means than that his function is really useful as he could compare then even in Klingon

Re: wtflib.php

2008-06-05 11:51 • by real_aardvark
199035 in reply to 199028
Bob:
ClaudeSuck.de:

FORTRAN uses .LT., .GT., .EQ. and so on


Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.


I'm beginning to lose the will to live.

Perhaps it's time to give up programming in any language whatsoever and just express myself in unadulterated predicate logic.

Re: wtflib.php

2008-06-05 11:53 • by ClaudeSuck.de
199036 in reply to 199035
real_aardvark:
Bob:
ClaudeSuck.de:

FORTRAN uses .LT., .GT., .EQ. and so on


Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.


I'm beginning to lose the will to live.

Perhaps it's time to give up programming in any language whatsoever and just express myself in unadulterated predicate logic.


Don't worry, just keep going until Xmas 2012 and all your problems will vanish all of a sudden.

Re: wtflib.php

2008-06-05 12:01 • by Thunder (unregistered)
199038 in reply to 198953
SunTzuWarmaster:
Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?


Heh, that was a nice attempt at sounding intelligent and knowledgeable. Failed, but nice attempt. What, exactly, are you talking about that's relevant to this article? The code would have generated far more than "FIVE" instructions in all cases, because it's not running natively to begin with.




Re: wtflib.php

2008-06-05 12:07 • by Mhendren
199042 in reply to 199000
It is unlikely that PHP would change the meaning of "<", ">", or "==".

PHP being PHP though,it is easily conceivable that they would deprecate those operators in favor of (respectively) "lessThan", "is_greater_than", and "equals"

Re: wtflib.php

2008-06-05 12:11 • by kenrick (unregistered)
oh man, i just had to work on a site for a client and found that very MatchField function in the codebase!

it must be listed on some crap php development site somewhere. sad how the interwebs corrupts completely.

Re: wtflib.php

2008-06-05 12:18 • by NCBloodhound
A more robust version of IsTrue in c#:

public bool IsTrueN(bool value, int times)
{
for(int i = 0;i<times;i++)
{
value = !!value;
}
return value;
}

making the wtf even wtfier.

Re: wtflib.php

2008-06-05 12:23 • by real_aardvark
199049 in reply to 199042
Mhendren:
It is unlikely that PHP would change the meaning of "<", ">", or "==".

PHP being PHP though,it is easily conceivable that they would deprecate those operators in favor of (respectively) "lessThan", "is_greater_than", and "equals"
... and thus my most excellent Real Enterprisey (c) solution to precisely this problem.

In fairness to PHP, though, it's not really an Enterprisey language, is it? Java is so much better at this sort of thing.
« PrevPage 1 | Page 2Next »

Add Comment