Programming practices that annoy me
Dec. 26th, 2006 10:56 pmA 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
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)Re: DURR HURR
Date: 2006-12-27 04:51 am (UTC)(no subject)
Date: 2006-12-27 05:00 am (UTC)(no subject)
Date: 2006-12-27 06:11 am (UTC)conditionmay possibly be not boolean. But you can always do:return (!!condition);(no subject)
Date: 2006-12-27 10:55 am (UTC)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,
tmpis used in return fromfigure_something_out()as boolean, i.e. the author expects that function to returnfalseif 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 fromfigure_something_out(), because its usecount doesn't reach zero as there's still one reference to it byvalue, and will unnecessarily dangle in the memory till the end ofmain_body(), which might be many, many light years away. However, if you doreturn (!!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.(no subject)
Date: 2006-12-27 03:06 pm (UTC)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)(no subject)
Date: 2006-12-27 08:28 pm (UTC)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)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)(no subject)
Date: 2006-12-27 02:51 pm (UTC)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)> 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)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)Actual problem ticket from last semester.
Am I inside the tavern?
Date: 2006-12-28 01:31 am (UTC)(no subject)
Date: 2006-12-29 03:37 am (UTC)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.