- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
There is a configuration setting to make $_GET and $_POST not global, so you are forced to declare it global or pass it :) It's supossed to force you to do "the correct thing".
In the default configuration, "index.jsp?action=delete" automagically creates in index.jsp a variable called 'action' with content 'delete'. PHP is funny and damn easy.No need to retrieve the variables from the request, because PHP does it for you! :P Of course, this is dangerous because visitors can set any variable to anything in your program.
Admin
and you're well known to be a tosser, it's been seen on many other posts on this site. Your point?
Admin
Seriously, the code is not the WTF. I imagine I wrote the original code. 'switch' cannot do everything 'if' can, so there's probably a reason I used 'if' in the first place. For whatever reason, the code ended up the way it was, as a long chain of 'if' and 'ifelse'. However, it works and is tested.
And now O.C. tells me, I have to use a switch, because... uhm... because... well, because he says so? I would either turn in exactly the code submitted ("You wanted a switch?! You get a switch!") or implement a refactoring tool, prove it correct (this might take a week or so, at the very least) and then apply "turn-if-into-case" exactly once. ("I had to be absolutely sure not to introduce an error in the transformation O.C. requested.")
See the WTF? Yeah, stupid code review practice it is.
Admin
Scheme is a Lisp dialect (nobody uses the original Lisp nowadays, people use dialects built from lisp with structures such as loops already included [Lisp doesn't have anything like while or for loops originally, it only has something like 7 statements anyway], and an OOP implementation). The most common dialects of Lisp are probably Scheme and CLisp (Common Lisp), but there are many more.
Smalltalk, on the other hand, is a completely different beast (and - as a side note - the language that birthed unit tests thanks to Kent Beck's sUnit, which was later reimplemented in Java by the same Kent, thus spawning jUnit and the whole xUnit legacy).
Admin
The general idea is to put up stuff that is absolutely indefensible - where doing it right would be easier and quicker as well as more efficient and clean.
Your mind must be a sad place then, because what's actually here are people that will quite often admit to having made similar mistakes, and nearly always try hard to find justification for the code besides simple stupidity and incompetence.
Recognize a bit too much of your own coding practices here? What's more retarded and pointless, making fun of other people's really bad mistakes and discussing how it should be done ideally, or getting all worked up and morally outraged about some totally harmless site on the internet?
Admin
Last time i checked, the only thing switch/case couldn't do that if/else could was non-equality (not as in "!=", as in "not ==") comparisons. A common operation, but one which doesn't occur in this code.
On the other hand, switch/case feature fallthrough, which would allow here for a much cleaner syntax overall, and an enhanced readability (compared to the current version, or a version using in_array in order to get rid of the duplicate code)
And using a switch/case structure prevents you from hitting the "=" comparison issue.
There is a quite cool process that i've known has been in existence for the last few hundreds of millenia (sp?), it's called 'a question'.
It's widely used when you don't understand the reason for an instruction, it allows you to get more informations about the aforementioned reasons and stop being a dumbfuck
Your idiocity?
Admin
Whoa! Lighten up Francis!
Admin
I think you're referring to the register_globals configuration option. Yes, disabling this stops the variable $action from automatically appearing your scripts global variables, but this configuration option by no means affects the globality or superglobality of the $_GET or $_POST arrays. (In fact, if they were not global, how would you be supposed to get at them?)
But you're right, that's what it does and why you should have it disabled.
Admin
Funny enough, driving a nail into a piece of wood is not considered an art, but designing and constructing opulent buildings is and always has been. I leave the moral of this up to the reader.
Admin
That was obviously a typo - not a grammatical error.
Sincerely,
Richard Nixon
Admin
Just as a BTW, I'd like to present a situation where I feel that
?:
is better thanif
-then
-else
-- Where you're doing the same thing with the result of the conditional, regardless of which branch is taken. For instance, given:my ($id, $name, $change, $page) = split /\s*,\s*/, $line;
(ie $id, $name, $change and $page are the first four CSVs from $line)
...you can do the obvious:
if (!$id ) { die "No id" }
elsif (exists $types{$id} ) { die "Id clash" }
elsif (!$name ) { die "No name" }
elsif ($change ne "+" && $change ne "-") { die "Change not +/-" }
elsif (!exists $pages{$page} ) { die "Page nonexistant" }
...in which case you can be glad the thing you're doing with all the values is called something nice and short (ie
die()
). Yes, you could muck about with assigning the results of each of those blocks to$err
or something, but then you've got single-use variables floating around, and I prefer to avoid that.Instead, I would do:
CHECK: {
die
!$id ? "No id" :
exists $types{$id} ? "Id clash" :
!$name ? "No name" :
$change ne "+" && $change ne "-" ? "Change not +/-" :
!exists $pages{$page} ? "Page nonexistant" :
last CHECK # escape the die()
}
This saves you from an unncessary variable, having to type "
die
" or "$err =
" over and over again, and the - in my mind, unncessary and crossed-eyes-inducing - noise of repeatedelsif
s and their associated punctuation. Yes, it does involve using loop control to escape from the parameter list of a function to stop it being called, which looks disturbingly hackish, but it's so much easier on the eyes.Let's see what y'all think's Evil And Wrong (TM) about this, then. I'm sure there'll be something.
Admin
It's not for that case that elseif is considered bad...It's for the case where you have 20 more elseifs, and a few nested if statements. Besides that, switches are generally more readable; your pretty formatting makes the elseif look just as readable as the switch, but with an elseif block, you're generally counting {}'s, whereas with a switch, you're just going between big obvious breakpoints.
Admin
It does:
Case Expressions
Admin
What he wrote was more or less Python, no braces there, please move along.
(Oh, and i don't really see where you get "big obvious breakpoints" in switch/case statements)
Thanks sir.
Admin
Why is he typecasting in PHP? This guy is retarted.
Admin
We have been clearly led to believe that the coder was new to this lanauge, while O.C. is an expert. While there are many situations where switch/case cannot repalce if/elseif/else, given the above I would assume that O.C. is correct when he says this code can be changed to switch/case
In compiled languages a switch/case is turned into a hash table for large amounts of cases (about 10). Therefore a switch/case is much faster than if/elseif/else when there are many cases.
I don't know how php does things, but someone examined the output from the C# compiler last time we had this discussion and verified that it really does create a hash table.
siwtch/case is also more readable because it is clear from the start that you are only looking at one variable. (Note, for some languages this doesn't apply) With an if/else you need to watch for the variable being exampled changing, which makes reading code hard:
if x==5
doSomething()
else if y == 5
SoemthingElse()
else if x == 6
doSomethingElse()
...
Of course if you really need to mix variables to get the logic correct you need the if/elseif sequence. (and some comments so I understand why...)
Admin
I agree that both are equally bad/good. Long switches are hideous monstrosities as well. Their performance advantage over the else if chains hardly justifies their existence. If an else-if chain smells bad enough to justify going to a switch, then you may as well go into a strategy or command design pattern.
Admin
Even for strings? I thought compilers could only do that for ordinal types.
Admin
<Pun forewarning>
Because he's switching to the dark side, just in case...
Admin
Or a jump table.
Even in xBASE (which uses expressions), it is useful. I sometimes have to iterate through the four possibilities of truth values of two conditions. I find it much clearer to write
do case
case p and q
...
case p and !q
...
case !p and q
...
otherwise && !p and !q
...
endcase
than cascading if-else statements. Less often, I have have to handle more combinations. Cascading if-elses are thoroughly nasty then.
Sincerely,
Gene Wirchenko
Admin
I was under the impression that it was to keep a consistent coding style in the codebase. I mean, christ, who hasn't come across that project maintained by 5 people before you, all of whom had distinct coding styles and preferred to precision-insert changes instead of rewriting a block? Welcome to readability hell. Style standards are a good thing.
On the other hand, sometimes the coding equivelent of a middle finger is also a good thing. =D
Admin
No. That is also bad code.
if(IsTrue(true) == true)
Admin
It most certainly CAN be an art form. Developing elegant and efficient code takes skill and drive. I'd suggest that many programmers put into their code what people put into painting, music, or other "arts."
Admin
Actually, no, you can't. Not even jump tables work here. If you had values for 1 and 20008, your jump table would be larger than any CPU would support and would have a lot of junk pointers to the default routine. It always compiles to an if/else chain. Strings don't fit in registers, so could you imagine the size of a jump table if you representing the strings as integer values? No CPU has registers that large, and I'll go out on the bill gates branch here and say that no cpu ever will have registers that large, and certainly will never support an accompanying jump table (could you imagine a jump table for all possible strings? Not freaking likely).
Admin
I think it works for him, too. I bet he thought exactly the same thing as you. I don't believe that he didn't know how to use a switch statement, I think the programmer figured that for an if/else with only THREE outcomes, it wasn't actually worth a rewrite into a real case statement.
This looks like a case of rebellious programming, not bad programming.
Admin
Admin
Actually in C I believe the switch statement has fall-through for each of the cases, so e.g.
switch(c) {
1: { ... }
2: { ... }
3: { ... }
}
Each one just "falls though" to the next case? In this way switch looks more like a series of gotos. All cases should be mutually exclusive anyway, so else isn't required either.
Admin
In PHP, you have to prepend always all variables with "$", or PHP won't recognize it as a variable. Same with arrays.
In unix-like shells like bash, sh, tcsh, zsh, etc. you have to do the same, except when doing an assignment.
Admin
You know what binary search is? You know what a hashtable is? Apparently not, otherwise you wouldn't write such nonsense.
Admin
Aha. This is the kind of person that if you ask him "Say hello to Alice, Peter!" responds with: "Hello to Alice, Peter!".....
Admin
As a professional PHP developer I resent your generalization. Not all PHP developers employ bad coding practices. We are actually some coders that put pride and thought into our code.
Remember, it's not the tool that makes a bad coder.
Admin
Yep, I don't understand this either.
The world is full of strange things, you know ...
Admin
I work with C and C++ which only allow switch on ordinal types so I'm, not sure.
However I see no reason you can't do a hash table on a string. The has function is a little more complex, but it doesn't have to be perfect. There are plenty of papers on hashing of strings, just pick something that works when you implement the compiler.
Admin
Why won't a branch table work? You just need to be a little intelligent about it. For your example you don't jump on the full word, just the least significant bit.
foo[] = [(function for 20008), (function for 1)
foo[x & 0x0001]
Of course your functions then need to test for to be sure x is the correct value, so for this simple case it is a pointless optimization.
The C# compiler was tested some months back. When the number of cases is less than 10 it turns a switch into an if/elseif. When the number of cases is more than that it builds a hash/jump table.
Notice that we did not specify any particular table implimentation. A jump table with all possible values does not fit into memory. However compiler writers are smarter than that. Both hash tables, and binary searches will fit into memory (If it won't you have bigger problems, and need to think about hand optimized assembly).
Admin
Certainly there are programmers who use PHP and don't write poor code but by saying that PHP is just a tool and the people are responsible, you fail to take into account the often-present culture that surrounds a language and the users of a language. In that respect, I think the assessment of PHP which you objected to is correct.
Sincerely,
Richard Nixon
Admin
<FONT face="Courier New" size=2>was i the only one who caught the =/== mistake? or was that just a typo?</FONT>
Admin
It isn't that you are too old, but that your experience has likely been in the dos/windows world or the mainframe world. The family of unix shell languages that evolved from the Bourne shell use the dollar sign to indicate a variable, much like dos uses the percent signs. Perl was developed as a better unix scripting language and so absorbed many syntactic elements of the Bourne shell languages. PHP in turn was developed as a better Perl for the web, and so absorbed many syntactic elements from Perl.
It is interesting to observe how many widely used languages were specifically designed to broadly resemble some preceding successful language. Anyone who is comfortable with shell scripting, sed, awk, etc. can naturally absorb Perl in small easy to digest chunks. Meanwhile the Perl developer can use PHP and Python more easily than a non-Perl developer.. Likewise C begat C++, which begat Java and Java's near clone C#. Meanwhile languages like Ruby are slower to gain acceptance. There is a certain feeling of comfort when much of the syntax is recognizable.
Admin
Not quite ;p
Admin
Yeah, that's much better than the readable switch-version ;-) Job security via code obscurity
Admin
Just saying, but whitespace is not everything. I am one of those people that prefers the coding style
if (x)
{
...
}
Just because that lines up the brackets, and actually puts the block on the same level as the rest.
And a quick question:
(x)
?
{
...
}
:
{
...
}
Would that compile? If so, that's my counter argument ;-)
Admin
heh... looks like something I would have done... 'you want a switch statement!? I got your stupid switch statement right here buddy...'
-lo
Admin
Hey, uh.. all your corrections that have been made, they don't stay true to the original code.
Admin
I discourage people from trying PHP because it turns out dickheads like you who think they know it all and attempt to correct real programmers.
$_GET is not a String[][]. String[][] -- even though it doesn't exist in PHP since it's simply called 'array' -- would be indexed via $_GET['foo']['bar'], so you can see why you're wrong without me even explaining type to you. You also confuse declare and define, more evidence you only know PHP. (I refer to your comments here.)
Also, PHP does not create an empty variable whenever you mistype a variable name. I present sample code, $foo = $bar all by itself:
PHP Notice: Undefined variable: bar in test.php5 on line 2
Learn C and take an English class, then try to hock a programmer discussion. Seriously.
Admin
Nope. Need an x. If you throw in an
int
x
=
0
;
before it...then it should, yes. Talk about token fun!
Admin
Being a junior or transitioned is one thing. . . lacking common sence is another
Well here is one more for the desert (from another "transitioning" programmer):
<FONT color=#0000ff>String</FONT> fName = <FONT color=#800080>null</FONT>;
<FONT color=#800080>if</FONT> (fname == <FONT color=#800080>null</FONT>). . . . .
The
"=="
I can understand. . . after all, he is "transitioning", but . . . . WTFAdmin
Hrmm...
def IsTrue(true):
if(IsTrue(true))...
Admin
Was this anonymized to hide a Delphi snippet perhaps? Not sure what other languages might be affected, but Borland's "native" string type in 32-bit environments was implemented as an instance of AnsiString class. Objects were not allowed in case statements, so the code just wouldn't fail. In such a case, the WTF would be that O.C. didn't know this.
Admin
Yeah, you may want to replace it too...
Admin
No - he was just being cute or a smart a**. "OK then I'll use switch, but I'll do it my way". Any of those reviewing his code the second time would have realised this.