Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
User Journal

Journal Chacham's Journal: Stupid Code: Using if() to set a value 4

Here's the latest example of something i have seen way too often:

if(getListView().getCount()==checkedItemCount) chk.setChecked(true); else chk.setChecked(false);

What's the point of obfuscating your code with an if()? This isn't conditional. You want to set it to the same boolean value as the evaluated expression. Obviously, the clearest way to write this (without changing names) is:

chk.setChecked(getListView().getCount() == checkedItemCount);

I often wonder why people define variables for one-time use. The answer is (at least, sometimes) that it allows for easy testing or expansion. I still think that's kind of stupid (because it's an easy fix later, and not worth the cost (of non-simple code) in redundancy) but i understand the mindset. Better said, it's a valid view that i just happen not to agree with.

And now, a digression: I worked for one company where my team lead did not like me. I was hired based on a phone interview with the supervisor (and team?) and just showed up for work one day. The team lead was smiling and happy until he saw me. His face dropped and he moped around getting me through HR or whatever. I have been the subject of prejudice before, but it was usually more subtle. To his credit(?) i don't even think he noticed he hated me. For assignments, i was to report to a pseudo-team lead.

One day we needed to process files. A COBOL guy (a pretty nice guy) wanted an elaborate system with queues and whatever to do this or that. It was insane. But, he's a COBOL programmer, so that was expected. I told them to use perl, because processing text files was a perl thing, and it would work perfectly in our case. For some reason--which still escapes me--they actually listened to my suggestion, and the decision was made to hire a perl programmer to write the script. (It is a large company known for bureaucracy and laziness, to anyone who has ever worked there.)

Well, no one knew how to interview the guy, so i was to ask questions. I wrote a list with many questions and expected answers, some answers being specific, some with the general feel, so everyone would know what i was asking and what i was looking for and not be bored during the interview. When the interview was over they told me they hired someone. The team lead, armed with a list, no longer saw any reason to include me. Oh well, we're going to get someone good, right? (Can you tell where this is going?)

I may have started coding the script before he got there. I don't remember. In any case, the script was small. It took me a while as i did not actually know perl (well). This was not something i hid from anyone.

He was hired and was to report to me. A really nice guy who moved to the area for the job. He talked of his past mainframe coding and i liked him. Though, after a bit of time, it became obvious he was having issues. Another team member and i started to coach him on the logic and the like, reviewing his code and giving advice. But he just kept failing. We broke it down to be super simple, until we simply suggested he wrap a block of code in an if() statement. He couldn't even do that. We had to do the awful thing of recommending he be let go. I felt horrible. If only. Or is that, only if().

The relevance of the story ends here. Nonetheless, it lived on. I got the code working myself, and it did the job. But, the aforementioned coworker (maybe he was asked) rewrote it in VB6. He reported to the team lead.

This discussion has been archived. No new comments can be posted.

Stupid Code: Using if() to set a value

Comments Filter:
  • Where the first is a valid construct, because the values of true and false in objects like a check box are NOT the same as the boolean values of true and false.

    • by Chacham ( 981 ) *

      The expression must evaluate to true or false, and those are both valid values for the checkbox's method, regardless of if there are also others.

      If an If()'s true block set something to true, and it's false block set's it to false (or vice-versa) and nothing else, there's absolutely no reason to use an if().

      • i was thinking mainly of the surprise I got in VB 3.0, where true evaluated to -1, false to 0, but a checkbox value was 0,1,or 2 (unchecked, checked, or grey box). This code would work fine for unchecked boxes, but cause an error for checked boxes.

        There are a few other languages and checkbox controls that are like this, which is where the script kiddie likely learned the pattern.

        • by Chacham ( 981 ) *

          OIC. Nah, java is just a redundant language which promotes this form of idiocy.

          Anyway, in this case, it's not the value of the checkbox. It's a method that accepts true or false as its argument. Hence, my argument. :)

This file will self-destruct in five minutes.

Working...