Comment On 1, 1, or 1, or if you really want, 1

It's a two-for-one day today that, despite the title, doesn't actually have to do much with ones. Or twos for that matter. No, it's more a case of redundancy in redundant cases. Oy, I'm gonna stop narrating after that one ... here's the first sample from Tristan Harmer [expand full text]
« PrevPage 1Next »

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 14:11 • by fluffy
 "You can buy it in any color, as long as it's black." -- Henry Ford

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 14:35 • by loneprogrammer

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 14:47 • by goober foober

Having deletes and any new states that may be introduced handled by the default case is a brilliant optimization saving untold slots in the jump table, and is also forward compatible.

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 15:12 • by skicow
I'm guessing (hoping?) these two code snippets were coded by forward-thinking coders for future enhancements or have had lines of code deleted from them and the switch and if/else statements not removed yet.

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 15:19 • by Name
At least the first programmer added comments



//Load Page Data




Re: 1, 1, or 1, or if you really want, 1

2005-03-31 15:26 • by cm5400

Bruhahahahahaha.  I love it when I see code like this.  It just makes my day.  [:D]

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 15:27 • by Kreiger
No, it does not beg the question. It raises the question.

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 15:49 • by Bustaz Kool
The second example simply covers both conditions: "Y" and Why Not?

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 17:22 • by C.Batt
Meh.



I'm betting that this is simply the results of rapid cut'n'paste code stubbing that the developer just hasn't returned to.



Big freaking deal.  (Unless of course this is in production-ready code, then shame on the developer)

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 17:29 • by ernestpazera

This code(found today by a coworker in production code) is (I think the) epitome of code that "does the same thing no matter what," and to really take the cake, that same thing is... NOTHING!


Comments are as found.

//--------------------------------------------------------------------------

//
//
void pal_serial_1_task(UNSIGNED argc, VOID *argv)
{
// byte tb[40];
// byte tbCnt=0;
UNSIGNED actual_flags;
STATUS status;
NU_Sleep(5);
TRACE("\n\rPort 1 running...");
while(1){
/* Retrieve events the event queue*/
status = NU_Retrieve_Events(&Port_1_Events, SERIAL_NEW_CHAR|SERIAL_TIMEOUT|SERIAL_SYNC, NU_OR_CONSUME, &actual_flags, 30); //
if ( status != NU_TIMEOUT) {
if ( actual_flags & SERIAL_NEW_CHAR) {
// signBuildMsgId(128,MSG_GETCONFIG, 0, 0);
// TRACE("\n\rPort1 New Char");
// addTimeout(4,DEF_TM);
}

if ( actual_flags & SERIAL_TIMEOUT) {
// processPortEvent(&uart_ports[4],PORT_EVENT_TIMEOUT);
// TRACE("\n\rPort 1 timeout");
}
if ( actual_flags & SERIAL_SYNC) {
TRACE("\n\rSync 1");
// processPortEvent(&uart_ports[4],PORT_EVENT_SYNC);
}
} else {
// signBuildMsgId(128,MSG_GETCONFIG, 0, 0);
// Message = DO_WAIT;
// status = NU_Send_To_Queue(&p5Queue, &Message, (UNSIGNED)1,
// (UNSIGNED)NU_NO_SUSPEND);

}
}
}

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 18:37 • by chad okere
Um, could just be they plan to change the functions later on. Or planned on it and never did.

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 20:08 • by goober foober
32140 in reply to 32134

It does worse than nothing. It spins in a hard loop forever. But hey, at least it sleeps for what appears to be 5ms before doing so.  I assume this is to allow all the other processes to finish up before it goes hog-wild. hehehe

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 20:55 • by reniM

i am not redundant


i am not redundant


i am not redundant


i am not redundant


i am not redundant


...

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 22:18 • by cjs
Ooo, those comments are great! I never would have been able to figure out what the LoadPageData() method does otherwise.



I'm almost getting to the point where I think we should remove comments from the language.



Re: 1, 1, or 1, or if you really want, 1

2005-03-31 22:24 • by Pat

imho It looks more like a copy-and-pasting frenzy gone wrong, rather than an ignorance of not knowing how conditionals work..


 

Re: 1, 1, or 1, or if you really want, 1

2005-03-31 23:26 • by Rick C
32144 in reply to 32133
At the place I'm working, I've actually seen code like that second example, and yes, it's in production.



Re: 1, 1, or 1, or if you really want, 1

2005-04-01 01:32 • by maribert
32145 in reply to 32143
What is remarkable about the first code example is that there is no "break" for the default case. So there is a little glimpse of hope ...

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 01:44 • by V.
See boss.  My code isn't so bad at all, look at this: HREF="" target="_blank">The daily
wtf




Re: 1, 1, or 1, or if you really want, 1

2005-04-01 03:12 • by jmo
I saw one similar once..... using your example, it was the equivalent of the following
switch (state)

{
case Enumerations.PageState.Edit:
//Load Page Data
LoadPageData(Enumerations.PageState.Edit);
break;
case Enumerations.PageState.Update:
//Load Page Data
LoadPageData(Enumerations.PageState.Update);
break;
case Enumerations.PageState.Add:
//Load Page Data
LoadPageData(Enumerations.PageState.Add);
break;
default:
//Load Page Data
LoadPageData(Enumerations.PageState.SomeDefault);
}
 

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 03:32 • by diGriz
This looks so familiar. A cow-orker (I 'inherited' his code in my last
project) used to code like that. But one of his lines is still stuck in
my head. I found it while I tried to do a little refactoring on his
messy source:



quantity += -1;



I'm glad he's back in testing again.

Re: I saw one similar once..... using your example, it was the equivalent of the following

2005-04-01 03:57 • by ammoQ
32150 in reply to 32147
Your example makes a bit more sense, because of the default branch.

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 07:57 • by Mike R

I think its clear the first item in this post is a stub for future development. It just looks unfinished. Though I do love the redundant comment.


The second example I could be less forgiving about. One explanation might be changing old, working code to do some new functionality, and not paying attention. You'd be suprised at how many strange bugs can pop up when you throw more developers at a project.

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 08:43 • by BitterLikeQuinine

I once wrote a CASE tool to trap this sort of thing called TERSE: -


"The Eliminator of Redundant Statements Eliminator".


Perhaps I should dig it out and mail it to these people.

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 08:47 • by Rick Scott
32159 in reply to 32148
diGriz:
This looks so familiar. A cow-orker (I 'inherited' his code in my last
project) used to code like that. But one of his lines is still stuck in
my head. I found it while I tried to do a little refactoring on his
messy source:



quantity += -1;



I'm glad he's back in testing again.




Laugh. Out. Loud. It is pretty hard to squeeze a WTF out of 2 assembly
ops worth of code! Thanks for letting me start a Friday with a laugh
<3

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 09:28 • by Robin Lavall&#233;e
32162 in reply to 32159
"Laugh. Out. Loud. It is pretty hard to squeeze a WTF out of 2 assembly
ops worth of code! Thanks for letting me start a Friday with a laugh"



Unless quantity is in a register, in which case only one op is needed:

dec reg;



Re: 1, 1, or 1, or if you really want, 1

2005-04-01 10:26 • by cm5400
32164 in reply to 32148

diGriz:
This looks so familiar. A cow-orker (I 'inherited' his code in my last project) used to code like that. But one of his lines is still stuck in my head. I found it while I tried to do a little refactoring on his messy source:

quantity += -1;

I'm glad he's back in testing again.


Hahahahahahaha,  must of never heard of "quantity -= 1;"  [:P]

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 10:39 • by bugmenot
32168 in reply to 32164
you must be really stupid to write



quantity += -1;



instead of



quantity -= 1;



I mean, this one is so obvious.

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 11:46 • by Charles Nadolski
32170 in reply to 32168
bugmenot:
you must be really stupid to write



quantity += -1;



instead of



quantity -= 1;



I mean, this one is so obvious.




you must be really stupid to write





quantity -= 1;





instead of





quantity--;





I mean, this one is so obvious.



:-D

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 12:01 • by sas
32172 in reply to 32168
bugmenot:
you must be really stupid to write



quantity += -1;



instead of



quantity -= 1;



I mean, this one is so obvious.


Almost as obvious as quantity--; but who cares?  Only the first one is funny.

Re: 1, 1, or 1, or if you really want, 1

2005-04-01 14:41 • by SteveOw
32180 in reply to 32134
ernestpazera:

        if ( status != NU_TIMEOUT) {
            if ( actual_flags & SERIAL_NEW_CHAR) {
//              signBuildMsgId(128,MSG_GETCONFIG, 0, 0);
//              TRACE("\n\rPort1 New Char");
//              addTimeout(4,DEF_TM);
            }


I saw a variation on this theme in a production program where the programmer found that a bug fix caused a new bug.  The fix was to comment out the new code and reinstate the old code, with a comment to that effect. [:'(]


 Better the devil you know...

Re: 1, 1, or 1, or if you really want, 1

2005-04-02 18:12 • by JJ
32212 in reply to 32172
And since we're being pedantic, we'd better write

--quantity;

since this doesn't introduce a temporary.



Talking about obvious...

Re: 1, 1, or 1, or if you really want, 1

2005-04-04 07:30 • by Purplet
32226 in reply to 32212
Any compiler will optimize x++ to be equal to ++x when the expression value is not used and there is no overloaded operator.



It all comes to style. Many prefer x+=1 instead of x++ (or ++x) because
they find it more readable. Can't find a reason for X+=-1 though.

Re: 1, 1, or 1, or if you really want, 1

2005-04-04 07:31 • by Purplet
32227 in reply to 32226
Of course I was thinking of --x, x-- and x-=1 :)



Re: 1, 1, or 1, or if you really want, 1

2005-04-04 17:14 • by Charles Nadolski
32247 in reply to 32227
Anonymous:


x-=1 :)





I think you just invented a new emoticon.

Re: 1, 1, or 1, or if you really want, 1

2005-04-06 08:24 • by Ion Todirel

...


state = null;


...


if(state != null) LoadPageData();


Re: 1, 1, or 1, or if you really want, 1

2005-04-10 22:31 • by Scott McKellar
Depending on the context, the first example may actually be reasonable.



Given that it's branching on "state", it's probably part of a finite
state machine -- or an event handler for a GUI, which amounts to the
same thing.



If I'm coding a finite state machine by hand, one way or another I need
to branch on the combination of state and event, and there are a lot of
combinations.  In order to keep the code manageable, I typically
do the branching in a rigidly stereotyped way.  The results may
include fragments that, considered in isolation, look just as silly as
your example, but that doesn't bother me.  It's more important
that the repetitive logical structure be immediately
recognizable.  Also this stereotyped pattern makes it obvious
where to put your changes if the state transition diagram changes.



If I'm feeling ambitious, or lazy, I might even write a code generator
in Perl or something to take over the tedium of writing voluminous
stereotyped code.  Again the results may look silly in isolation,
but I wouldn't care.  I'd rather rely on the compiler to optimize
away the redundant tests than trust my own Perl script to treat the
corresponding optimization as a special case.

Re: 1, 1, or 1, or if you really want, 1

2009-05-18 03:32 • by dfhj (unregistered)
Out of runes of magic gold? Need it in urgent? Yes, I can understand you. As the most important currency, without rom gold, you ever can’t do anything. So you need to buy rom gold from those most professional and loyal game online shops with years’ experience and have a good reputation among players. Is there any difficult? No, when you need the rom gold, please feel free to contact us, we are promising to offer you the cheap runes of magic gold with fastest delivery. Moreover, we are online 24/7, you can contact us any time with any question about. So why are you still irresolute? Come here to grab your cheap runes of magic gold now.

Re: 1, 1, or 1, or if you really want, 1

2009-05-18 03:34 • by warhammer gold (unregistered)
Crazy about running warhammer gold? Yup, it is so crucial indeed for us in Warhammer Online. Without it, we can even do nothing, without money to buy items, weapons and so on. So enough warhammer gold is substantial.
« PrevPage 1Next »

Add Comment