- 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
Oh, I'll say the FIRST one
Admin
and this is only a WTF if the coder in question had proper control over all the other bits of code that may have called that function. I've done that when I either don't fully know or can't change other bits of the system and don't want to cause a hard crash in production.
I'd put a comment, though, and firmly resolve to go back and fix it properly some day (yeah, right).
Admin
And I thought my codebase was bad. At least the dead code would be commented out.
Admin
I'm curious: how would removing the dead code and replacing it with a comment along the lines of "This was really complex in the past but new requirement #xxx means that we don't need that any more because yyy" have caused hard crashes in production?
Admin
Look at that: if(container.widgets[i].settings.dateRange.relativeDate){ if (container.widgets[i].settings.dateRange.relativeDate === '-1y') ...
Why not assign a local variable instead? var date = container.widgets[i].settings.dateRange.relativeDate; if(date ){ if (date === '-1y') ...
The 2 excuses of these copy-and-paste programmers:
Excuse 1: It's faster for me: the first version takes me 10 seconds to type with IDE-autocomplete on, but the second version takes 12 seconds for me. My answer: Do you know that code is written once, but read 10 times in average? You save 2 seconds, but the company must pay for additional minutes caused by readers because it's harder to understand.
Excuse 2: It doesn't run slower, the compiler will see the duplication and inline it. My answer: The compiler is not allowed to. If you have a method getX() and call it 2 times then the compiler does not know if the class has changed the value in the meantime (like a time counter or iterator would do), or if the value is the same.
Excuse 3: I don't care about time, it just runs the same way, and important for me is that the first if-statement guards against a crash. My answer: No. The first version may crash inside the if-statement body whereas the second cannot. Your check for it is meaningless, because you try to access the value again which could become null in the meantime. Java-Snippet: if (a.getX() != null) { System.out.print(a.getX().getY()) } So when it goes inside the if-statement, another thread can interrupt and call a.setX(null). Then the thread continues and when accessing a.getX().get(Y) throws a NullPointerException!
Admin
No that would be fine, What I meant was deleting the entire function as it's clearly redundant/superfluous.
Admin
Remember, this is TDWTF.
Admin
For those that missed it, because I know I did on several passes trying to figure out what, exactly, was wrong with the function: the first line is "return true;" rendering everything after it useless.
Admin
I missed it at first. Sort of reminds me where I worked once, where although we used source control, for every change we were required to comment out the old code, not delete it. Additionally, we would add a comment to the code with the change number. Predictably, this led to a huge mess. To make matter more enjoyable, although we were developing on Windows, we used an MS-DOS client for the source control, which was very painful. Procedures also required that we have a word document for every change that was saved in a specific folder.
This was my first development job, I was quite worried at the time that I had made a dreadful mistake in my choice of career.
Admin
Wasn't a certain well known travel firm was it?
Admin
Afraid not. They made banking software - I will say no more. Sadly this was before TDWTF - there would have been numerous CodeSOD candidates.
Admin
You get the first set of requirements and write up the logic for them; then the second set come in and it's easier to scrap the first set and write it from scratch; then the third set come in and they're almost the same as the first; by this time you've decided not to delete code but to keep it around for when the mood swings back next week and it's needed again.
Sure, adding comments around each version would help, but it's commonplace to do this.
Admin
no, it is frist line!
Admin
That reminds me: I once spent 4 hours analyzing 1100 lines of COBOL source, which ultimately computed just one variable...which was zeroed immediately following the code block.
Admin
Yeah if people used whitespace, in this case a blank line under the "return true", properly then this sort of thing would be much easier to spot.
Admin
As there is no source history, I would be worried that someone had added the "return true;" to enable them to test something more easily, and forgot to reinstate the tests. S smart dev might use an #IFDEF DEBUG or language equivalent, but a smart dev wouldn't be working in such a place.
Admin
I once had some code like this in one of my experimental development branches:
const int numberOfTimesBobChangedHisMind = 7;
if (numberOfTimesBobChangedHisMind % 2 == 0) { //... } else { //... }
Admin
MiserableOldGit, having the call could be useful in the future. I have some class code where a given method is but a return. Sometimes, that is all that is needed, but the hook is there for those subclasses that need something there.
Admin
I see Nagesh has a new job.
Admin
Implementing specs like walking on water: much easier when frozen
Admin
Code should not exist in isolation - it is only one part of a complex eco system.
Why does the organization want to invest in something [Epic]? What is the overall value representation [Feature]? What are the individual value/interactions? [Story/PBI]? Those are the "why" for the code...
How is the value going to be delivered, what actions need to be taken [Task]? This is the "what" for the code.
Then small incremental changes [Commits] that are each 100% focused on a single Task, and has a detailed comment bridging the gap of what is known from the task and what is clear from a diff on the code.
Have this proper ecosystem in place (and this has been around for well over a decade, it is not "new") and such situations are completely avoided.
Admin
Any good linter would throw an error about unreachable code for this... I guess they're not linting their code.