giza: Giza White Mage (Default)
[personal profile] giza
A few of the jobs I've worked at over the years have reqired me to maintain code written by those before me. Sure, undocumented code is a pain, but some things end in me shaking my head while muttering "WTF?" to myself over and over.

Here are a few examples:

1) Calling srand() in a for loop. That seeds the random number generator. You don't need to keep reseeding the generator on every iteration of the loop!

2) Writing code like this:

try {
   function_call();
} catch (Exception e) {
   throw (e);
}


Do you not understand how exceptions work? Throwing an exception you just caught is totally redundant!

3) Writing code like this:

if (condition) {
   return (true);
} else {
   return (false);
}

return (false);


The astute programmer will note that the final return statement is never reached. Do you not understand how return statements work? o.O

4) Things like this:

$file = fopen($filename);
$line = fgets($file);


Is it that hard to check the return value of a function? Do you like seeing file permission errors all over the webpage? I don't!

Related reading: The Chronicles of George (aka, The System is "Havening" a Problem!)
Also related: The Daily WTF

DURR HURR

Date: 2006-12-27 04:17 am (UTC)
From: [identity profile] bikerwalla.livejournal.com
Add more random to the random number generator!

Re: DURR HURR

Date: 2006-12-27 04:51 am (UTC)
zeeth_kyrah: A glowing white and blue anthropomorphic horse stands before a pink and blue sky. (Cyberme (toon))
From: [personal profile] zeeth_kyrah
You could just pull a higher-order variant of the Mersenne Twister, but that would require compiling for a larger maxint (insert 64-bit processor here). Oh, wait, people already do that.

(no subject)

Date: 2006-12-27 05:00 am (UTC)
From: [identity profile] whyrl.livejournal.com
For 3), why not just:

return (condition);

(no subject)

Date: 2006-12-27 06:11 am (UTC)
From: [identity profile] wesha.livejournal.com
That's a good interview quiestion. The answer is: because condition may possibly be not boolean. But you can always do:

return (!!condition);
(deleted comment)

(no subject)

Date: 2006-12-27 10:55 am (UTC)
From: [identity profile] wesha.livejournal.com
Nobody said it's C. It might as well be PHP or Ruby. And here's an example where you must do it like that:

function main_body() {
value = figure_something_out();
do_lotsa_stuff();
if (value) do_more_stuff{};
}

function figure_something_out() {
tmp = create_huge_object();
if (tmp) do_some_stuf(tmp);
return (tmp);
}

In this example, tmp is used in return from figure_something_out() as boolean, i.e. the author expects that function to return false if the obect creation failed. It will work exactly like that, with one little problem: if the object creation succeeded, it will not be destroyed upon return from figure_something_out(), because its usecount doesn't reach zero as there's still one reference to it by value, and will unnecessarily dangle in the memory till the end of main_body(), which might be many, many light years away. However, if you do return (!!tmp), the said object may rest in peace, for there will be no references to it and it will be destroyed when the scope moves away.
(deleted comment)

(no subject)

Date: 2006-12-27 03:06 pm (UTC)
From: [identity profile] giza.livejournal.com
I do believe my head would explode if I saw "!!" anywhere in a piece of code.

Plus, nesting that many logic operations on one line must be a real bitch to debug. (for those of us who don't think in LISP, that is :-P )

(no subject)

Date: 2006-12-28 12:48 am (UTC)
zeeth_kyrah: A glowing white and blue anthropomorphic horse stands before a pink and blue sky. (Default)
From: [personal profile] zeeth_kyrah
This is why Python has the copy module. Of course, it's also an argument (made to language designers) for why the return value of a function should be a copy and not the original object being returned.

(no subject)

Date: 2006-12-27 08:28 pm (UTC)
From: [identity profile] sakayra.livejournal.com
I know that people use to write "if (condition)" with a non-boolean "condition" all the time, and I hate it all the time. If it's not boolean, it's not worth calling it a condition. If it's actually a test for 0 or NULL, then I write "if (NULL != pointer)" or something similar -- in my book, much more readable than these sick non-boolean-embracing "if"s that C introduced.

And you can easily "return NULL != pointer" which is perfectly fine (although I agree that an explicit "if" allows me to add more code more easily if I intend to expand later).

(no subject)

Date: 2006-12-27 03:01 pm (UTC)
From: [identity profile] giza.livejournal.com
Because I hate nested code like that. I might have to expand the if clause some day to add an edge case. I might want to put a debugging statement in the if statement. I might want to add an additional function call in the if statement. Who knows?

The sole reason to do "return(condition);" might be for optimization purposes. But only after all other methods fail.

(no subject)

Date: 2006-12-27 11:09 am (UTC)
From: [identity profile] jlick.livejournal.com
In my programming classes we were told to always put a return/exit/etc at the end of our functions/procedures/etc even if the program was never supposed to get there. The reasoning is that if you borked your logic, some default explicit action would happen. In your example it is fairly obvious what will happen but if you have several layers of ifs and elses there may be a possibility that a particular (presumably rare) condition might result in no explicit action being taken.

(no subject)

Date: 2006-12-27 02:51 pm (UTC)
From: [identity profile] wolffit.livejournal.com
My Java compiler won't even let me compile if you have unreachable code, or if I have a function that fails to return a value.

As for #2... Useful for debugging, where you want to set a breakpoint "closer" to where the exception occurs. Wouldn't do it in production code though.

(no subject)

Date: 2006-12-27 02:58 pm (UTC)
From: [identity profile] giza.livejournal.com
> if you have several layers of ifs and elses there may be a possibility that
> a particular (presumably rare) condition might result in no explicit action
> being taken.

When I wrote complicated code like that, I try to avoid having returns buried in the code for precisely that reason. Instead I set a value and have a single return($retval) at the end of the function.


(no subject)

Date: 2006-12-27 04:07 pm (UTC)
From: [identity profile] shockwave77598.livejournal.com
Putting the open brace anywhere but in a tabbed line by itself drives me bonkers.

if (test){
loop = 0;
while (loop){
if (condition){
return true;}
counter++;
}}

Trying to read this sort of source gives me a headache.

(no subject)

Date: 2006-12-27 06:44 pm (UTC)
From: [identity profile] nius.livejournal.com
Image

Actual problem ticket from last semester.

Am I inside the tavern?

Date: 2006-12-28 01:31 am (UTC)
From: [identity profile] kinkyturtle.livejournal.com
"If there are any internets there, I wanna DO 'em!"

(no subject)

Date: 2006-12-29 03:37 am (UTC)
From: [identity profile] luciffur.livejournal.com
If little snippets of WTF code didn't yank your chain, try stepping yourself through some of the shit they have for the Obfuscated contests.

Really. The best one so far has to be a program of zero bytes that outputs zero bytes. Geeks all over the world are probably sploding their pants right now.

But coming back to demon source (yes! I program!) it drives me balls-to-the-wall whenever I see nested lambdas in Lisp/Python. One lambda is enough, but more than two lambdas for a function, and you're just fucking with other programmers.

Srsly.

Profile

giza: Giza White Mage (Default)
Douglas Muth

April 2012

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags