Comment On Switched on Loops

Long time readers may remember a post from a few decades ago (in Internet time) entitled The FOR-CASE Paradigm. Since originally posting that, I've received quite a number of similar submissions but have avoided using them simply because it would feel like a duplicate post. There's really only so much you can say about a for-switch loop. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Switched on Loops

2005-03-23 13:34 • by dubwai

Is the first one Java?  That would just be:


String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};


C# probably have a similar construct.  And I can't remember if C++ does.  WTF!

Re: Switched on Loops

2005-03-23 13:41 • by Mike R
>>Shudder<<



That second block of code reminds me of a project I used to work on at
a previous employer.  The code was littered with non-sensical
varaible names, while loops that included the comma operator in the
conditional statement, and a big for loop that looked exactly like this.

Re: Switched on Loops

2005-03-23 13:49 • by Michael Link
Exhibit A is just dumb but doesn't cause much headache. I would send
the developer to some training. Exihibit B is much more worse because
probably the whole application is tangled in this for-switch-***. I
never would allow the developer of this beast to write one line of code
anymore until he has undergone some kind of Cobol exorcism.

Re: Switched on Loops

2005-03-23 13:49 • by Jeff S

Classic examples of people overcomplicating really simple things. 

Re: Switched on Loops

2005-03-23 13:51 • by Sweets
Thats what happens when you pay people 'by the line'.

But a real moocher would of thrown in some useless comments

//Case 0
case
0:
//this is setting days[0]="Monday"
//and then breaking out of the switch
daysIdea [I] = "Monday";
break;
I assume theIdea [I]  is in place of the [i], is that supposed to be there or a bug?

Re: Switched on Loops

2005-03-23 13:55 • by dubwai
31633 in reply to 31629

The second one looks almost like a state machine but what's the deal with:


case 1: break;


Why bother putting that in there?  Must have been maintained by someone who thought "I'm not messing with this piece of ***."

Re: Switched on Loops

2005-03-23 14:02 • by redtetrahedron

for( int i = 0; i < 7; i++ ) {
  switch(i) {
    default:
    case 0:


It's good that the developer included the default: in case the for loop went crazy and decided to go outside its bounds... [;)]

Re: Switched on Loops

2005-03-23 14:06 • by Anonymous
The real WTF here is that he didn't unroll that loop. It should have been like this:

String[] days = new String[7];
i = 0;
switch(i) {
default:
case 0:
days[i] = "Monday";
break;
case 1:
days[i] = "Tuesday";
break;
case 2:
days[i] = "Wednesday";
break;
case 3:
days[i] = "Thursday";
break;
case 4:
days[i] = "Friday";
break;
case 5:
days[i] = "Saturday";
break;
case 6:
days[i] = "Sunday";
break;
}
i = 1;
switch(i) {
default:
case 0:
days[i] = "Monday";
break;
case 1:
days[i] = "Tuesday";
break;

etc.

Re: Switched on Loops

2005-03-23 14:09 • by Beek
Actually, this would be a great idea for creating a code bloating
script.  Just turn any set of sequential statements into a
for/switch.  CA$H UP THE A$$

Re: Switched on Loops

2005-03-23 14:21 • by Andrew W
31642 in reply to 31623
Dude, even C has a way of setting all of the variables! I typically type it all the way out, for readability (when you're up late at night, sometimes you can do very weird things in code).

Think char ** argv in the "main" of a C program.

argv[1][]="Monday";
argv[2][]="Tuesday"..

Re: Switched on Loops

2005-03-23 14:24 • by Andrew W
31643 in reply to 31637
are you retarded? That would be the most code bloat I've ever seen. I hope you were being sarcastic...

Re: Switched on Loops

2005-03-23 14:38 • by Bustaz Kool
31645 in reply to 31631

Sweets:
Thats what happens when you pay people 'by the line'.


Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life.

Re: Switched on Loops

2005-03-23 14:44 • by Blue
31648 in reply to 31645
Bustaz, you beat me to it.  As I read this thread, the same exact
thought crossed my mind.  I'm curious about that also.

Re: Switched on Loops

2005-03-23 14:46 • by dubwai
31649 in reply to 31645

Bustaz Kool:
Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life


I've know that a lot of managment types think it's a reliable way to measure developer productivity.  More code == bigger raises and more clout.  It might explain all the copy / paste code I see.

Re: Switched on Loops

2005-03-23 14:48 • by StarLite
31650 in reply to 31645
I sure as hell hope not, will prolly make for useless and bloated code :S

Re: Switched on Loops

2005-03-23 14:49 • by rogueRPI
31651 in reply to 31645
Bustaz Kool:

Sweets:
Thats what happens when you pay people 'by the line'.


Has anyone ever really paid people 'by the line' of code?  The concept keeps getting mentioned (I assume facetiously) but I've never heard of it in real life.



I give you bulk rate on code!  Very cheap!  Free Viagr@ too!

Re: Switched on Loops

2005-03-23 14:59 • by Top Cod3r

You have to look at this code in context.  By using a loop, the developer is multi-threading the process, resulting in a more performant and scalable solution.  Another advantage of Exhibit B is it allows new steps to be inserted into the process by simply creating another number in the case statement.  Personally I would have numbered the steps 10, 20, 30, and so on.  That way if you need to put a new step in you could use 25 for example.  Its all about writing maintainable code.

Re: Switched on Loops

2005-03-23 14:59 • by Andy
31653 in reply to 31623
dubwai:

Is the first one Java?  That would just be:


String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};


C# probably have a similar construct.  And I can't remember if C++ does.  WTF!





Yes in C you can do:

char *szDaysArray[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};



and in pure C++ using the STL you have to use an itermediate array
because you can't directly initialize a vector. So in C++ it would be:



std::string daysArray[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

    std::vector vStr(daysArray,daysArray+7);



I'm not sure what C# has either but this is a definite WTF in just
about every language I've ever used. It's a shame it seems to be such
common practice.

Re: Switched on Loops

2005-03-23 15:01 • by Blue
31654 in reply to 31652
Anonymous:

You have to look at this code in
context.  By using a loop, the developer is multi-threading the
process, resulting in a more performant and scalable solution. 
Another advantage of Exhibit B is it allows new steps to be inserted
into the process by simply creating another number in the case
statement.  Personally I would have numbered the steps 10, 20, 30,
and so on.  That way if you need to put a new step in you could
use 25 for example.  Its all about writing maintainable code.





I recognize this too as sarcasm.  Perhaps I can develop the
ability to recognize it after all!  There IS hope for me yet!





Re: Switched on Loops

2005-03-23 15:01 • by Blue
31655 in reply to 31654
And I can now quote people in FireFox!  YAAAAAAY!



Re: Switched on Loops

2005-03-23 15:07 • by kwatz
This makes me think of Duff's Device:

http://www.lysator.liu.se/c/duffs-device.html

I suppose the "For-Case" paradigm is something like the opposite of Duff's Device.

Re: Switched on Loops

2005-03-23 15:11 • by loneprogrammer
31657 in reply to 31652
Sassy Sally:

 Personally I would have numbered the
steps 10, 20, 30, and so on.  That way if you need to put a new
step in you could use 25 for example.  Its all about writing
maintainable code.



Ah, that takes me back to my Apple ][ days.  Programs in BASIC
used line numbers, because there was no file editor, just a command line, and each line
typed in was inserted by its line number.  By convention, line
numbers went in steps of 10, so you could add more lines later
on.  If you wanted to add more than 9 lines between two old lines
. . .  Why would anybody want to ever do that?



Re: Switched on Loops

2005-03-23 15:17 • by Andy
31658 in reply to 31656
kwatz:
This makes me think of Duff's Device:

http://www.lysator.liu.se/c/duffs-device.html

I suppose the "For-Case" paradigm is something like the opposite of Duff's Device.




Duff's device has to be one of the coolest abuses of C of all time. I
use it for copying buffers of data from one file to another. This
For-Case thing is something I've never seen in any real life code but
from all the examples people post somebody is obviously writing this
stuff. What is even more amazing is that they actually continue having
jobs that involve writing code. Cross your fingers and hope they were
some first year analyst fresh out of college that was instructed as to
the correct way of doing these things and not some ten year VB veteran
coding in the cube next to you.

Re: Switched on Loops

2005-03-23 15:18 • by Alan
31659 in reply to 31652
This is probably the best way yet to reinvent line numbers and stuff them into a reasonably modern programming language.

Re: Switched on Loops

2005-03-23 15:25 • by Ron

If you want to see something awesome, check out Duff's Device:


http://catb.org/~esr/jargon/html/D/Duffs-device.html


An awesome piece of c hackery on top of it, implementing coroutines:


http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html


And then protothreads, better than sharks with laser beams on their heads:


http://www.sics.se/~adam/pt/


 

Re: Switched on Loops

2005-03-23 15:27 • by dubwai
31661 in reply to 31659

Anonymous:
This is probably the best way yet to reinvent line numbers and stuff them into a reasonably modern programming language.


It's not shown in this example but it's also a way to simulate gotos in languages that do not support them.

Re: Switched on Loops

2005-03-23 15:28 • by joodie
This is without a doubt the most idiotic piece of code I've ever seen.



Please please please tell me this is a joke.



Re: Switched on Loops

2005-03-23 15:31 • by joodie
31663 in reply to 31661
dubwai:

Anonymous:
This is probably the
best way yet to reinvent line numbers and stuff them into a reasonably
modern programming language.


It's not shown in this example but it's also a way to simulate gotos in languages that do not support them.





show me how, and maybe I'll believe you.



Re: Switched on Loops

2005-03-23 15:33 • by cm5400
:S  Ug.  Makes me want to upchuck seeing this kind of code.

Re: Switched on Loops

2005-03-23 15:35 • by cm5400
31665 in reply to 31664
cm5400:
:S  Ug.  Makes me want to upchuck seeing this kind of code.




Grrrr... Should be able to edit the posts...  The Emoticon does
not work for me... Could be Firefox?  Have to try in (gulp) IE.

Re: Switched on Loops

2005-03-23 15:36 • by Gary Henson
31666 in reply to 31662
With regards to sample
A, it's not a joke, it is Java, and the problem was largely due to the
fact the developer had just been sent on training. He'd apparently just
seen how "useful" case statements could be, and decided that maybe he
should be using more of them...




Thankfully I worked for that company 4 years ago, and haven't seen anyone do this since.


Re: Switched on Loops

2005-03-23 15:39 • by Andy
31667 in reply to 31623
dubwai:

Is the first one Java?  That would just be:


String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday""Saturday", "Sunday"};


C# probably have a similar construct.  And I can't remember if C++ does.  WTF!



C# syntax is the same as java....


-Andy

Re: Switched on Loops

2005-03-23 15:46 • by dubwai
31669 in reply to 31663
joodie:
dubwai:

Anonymous:
This is probably the best way yet to reinvent line numbers and stuff them into a reasonably modern programming language.


It's not shown in this example but it's also a way to simulate gotos in languages that do not support them.




show me how, and maybe I'll believe you.


You might not believe me even if I show you how?


for (int i = 0; i < 100; i +=10; i++){
    switch (i) {
        default:
           break;
        case 0:
           doSomething();
           break;
        case 10:
           doSomthingElse();
           break;
        case 20:
           if (doSomethingMore()) {
               i = 9; // goto 10
           }
           break;
        case 30:
           if (isFoo()) {
               i = 19; // goto 20
           }
           break;
    }
}

Re: Switched on Loops

2005-03-23 15:59 • by joodie
31670 in reply to 31669
dubwai:

You might not believe me even if I show you how?


for (int i = 0; i < 100; i +=10; i++){
    switch (i) {
        default:
           break;
        case 0:
           doSomething();
           break;
        case 10:
           doSomthingElse();
           break;
        case 20:
           if (doSomethingMore()) {
               i = 9; // goto 10
           }
           break;
        case 30:
           if (isFoo()) {
               i = 19; // goto 20
           }
           break;
    }
}



That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?


But, I believe you. Even so, I don't know of any language that does
a C-like switch (and no exceptions), and doesn't have a goto, which would make it completely useless except for really pathological cases.


Re: Switched on Loops

2005-03-23 16:06 • by skicow

Exhibit B doesn't even make sense! The programmer knows enough to procedurlize (sp?) their code, and knows how to use a for loop and a switch statement....so WhyTF would they do this?! is it so that there is a slight - and I mean bloody slight - pause between the calling of the individual procs? That would explain the:


case 1:
        break;


This code just makes me cry. [:'(]


 

Re: Switched on Loops

2005-03-23 16:11 • by igor1960
31672 in reply to 31669

The problem with that thread is: looks like you all here has nothing else to do but to demonstrate how stupid others are.


I'm not new in programming and trust me put alot of stupid things in my code and was paid "by lines" and/or "by time" and/or "by output" and/or "by speed" and etc. during my career.


I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.


On the other hand and being serious here: whatever language you choose -- I agreee you saved somespace in not typing that "laughable" switch statement and not confusing others, but instead used specific language/compiler feature and just use declarative approach -- end result is still the same: "During runtime equivalent of that long case was executed"... So, what exactly are you laughing about?!...

Re: Switched on Loops

2005-03-23 16:11 • by Stan Rogers
31673 in reply to 31670
Of course it's allowed. Is it a good idea? Not usually -- it's normally
a side-effect of something else (generally using assignment "=" instead
of equality comparator "=="), but I'd be willing to bet that just about
everybody has modified an iterator variable in a loop in every language
that has 'em at one time or another.

Re: Switched on Loops

2005-03-23 16:17 • by Top Cod3R
31674 in reply to 31672
Anonymous:

I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.



Are you the author of Exhibit B?

Re: Switched on Loops

2005-03-23 16:25 • by dubwai
31676 in reply to 31674
Anonymous:

Are you the author of Exhibit B?



Yeah, hit a nerve I think.

Re: Switched on Loops

2005-03-23 16:28 • by dubwai
31677 in reply to 31670

joodie:

That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?

But, I believe you. Even so, I don't know of any language that does a C-like switch (and no exceptions), and doesn't have a goto, which would make it completely useless except for really pathological cases.


You can do what I have shown in Java, which has no goto (goto is reserved but cannot be used.)


This is actually a technique that was once suggested (seriously) to someone who wished to convert a large amount of goto filled code to Java.

Re: Switched on Loops

2005-03-23 16:33 • by joodie
31678 in reply to 31673
Stan Rogers:
Of course it's allowed. Is it a good idea? Not usually 




My main concern was that a for loop in C is such an "interesting" mix of actions, it would qualify as a WTF itself.



I'm not all that surprised that it's allowed in C, but on the other
hand, I would not be terribly surprised if a compiler translated



for (i=0; i <10;i++) {

  i++;

}



to (pseudocode):



int i=0;

LOOP:

if (++i < 10) {

  int some_var = i;

  some_var++;

  goto LOOP;


}



either.



god I hate the "rich-text" editor in this forum software.



Re: Switched on Loops

2005-03-23 16:34 • by dubwai
31679 in reply to 31672

Anonymous:
I understand the reason you may laugh at presented samples, but trust me -- your laugh has nothing to do with the real "brush fire extinquishing" you have to deal with sometimes in your everyday programming life. So, kindly excuse me, but there is no reason to joke about somebodies bad programming practices.


Apparently you equate 'brush fire estinguishing' with garbage code.  The code given was clearly not a fix.  It takes planning and a lot of work to construct such a foolish construct.


Anonymous:
On the other hand and being serious here: whatever language you choose -- I agreee you saved somespace in not typing that "laughable" switch statement and not confusing others, but instead used specific language/compiler feature and just use declarative approach -- end result is still the same: "During runtime equivalent of that long case was executed"... So, what exactly are you laughing about?!...


The complete incompentence of the developer(s) who wote this.  I spend a lot of time fixing code that is as screwed up as this or worse.  I get to vent some frustration.  Sorry you feel threatened by this.  It kind of makes you look like an incompetent.

Re: Switched on Loops

2005-03-23 16:36 • by Andy
31680 in reply to 31672
Anonymous:
I understand the reason you may laugh at
presented samples, but trust me -- your laugh has nothing to do with
the real "brush fire extinquishing" you have to deal with sometimes in
your everyday programming life. So, kindly excuse me, but there is no
reason to joke about somebodies bad programming practices.




Sure we've all done stupid sh!t in our code. I hate going back and
looking at code I wrote when I first started, it's embarrasing. We all
start somewhere. However roasting bad code in public forums does serve
a purpose. People can come and laugh and also learn what not to do. I
like coming here and seeing mistakes(especially non-obvious ones) in
languages that I don't know well. So when I do use those languages I
don't make the same mistakes.

Re: Switched on Loops

2005-03-23 16:38 • by joodie
31681 in reply to 31677
dubwai:

joodie:

That's terrible, but it made me smile. :-) Is modifying the iterator count during a switch even allowed/documented in C?

But,
I believe you. Even so, I don't know of any language that does a C-like
switch (and no exceptions), and doesn't have a goto, which would make
it completely useless except for really pathological cases.


You can do what I have shown in Java, which has no goto (goto is reserved but cannot be used.)


This is actually a technique that was once suggested
(seriously) to someone who wished to convert a large amount of goto
filled code to Java.





Ok, but who the hell would use GOTOs for that kind of code? I mean,
OK, if you don't have exceptions, by all means use gotos if you need
to break out of a stack, but jeez, even if you don't have a goto,
there's still break and continue in java...



Re: Switched on Loops

2005-03-23 16:46 • by igor1960
31682 in reply to 31679

The only reason I've replyed was that as I undertsood the original joke placed at the beginning of the thread was to show that government workers are on break 7 days a week. Now, somehow thread moved into discussion of "programming practices".


Now, in your next responce you may name me not just being incompetent, but rather "being incompetent government worker". And I'm not threatened by this. Please, continue.


As to whether I'm really competent or not: It's not you who decides. What matters is how much we are making and are we really happy. You frustration obviously shows that you are not.

Re: Switched on Loops

2005-03-23 17:06 • by b0b0b0b
I realized that (it seems) nobody has congratulated Alex on the excellent name for today's post.  Good job.  [:)]

Re: Switched on Loops

2005-03-23 17:18 • by dubwai
31687 in reply to 31682
Anonymous:

You frustration obviously shows that you are not.



I'm happy about somethings and not about others.  I'm rather unhappy that the MO for IT managment these days is to outsource (offshore and onshore) major developement to incompetent developers and then make full time developers maintain it.

Re: Switched on Loops

2005-03-23 17:20 • by dubwai
31688 in reply to 31681

joodie:

Ok, but who the hell would use GOTOs for that kind of code? I mean, OK, if you don't have exceptions, by all means use gotos if you need to break out of a stack, but jeez, even if you don't have a goto, there's still break and continue in java...


No argument here.

Re: Switched on Loops

2005-03-23 17:46 • by igor1960
31689 in reply to 31687

Here I agree -- MO screews everything.


However, your criticism an claim that original code was stupid hypothetically may not be the case. In fact it may not be as stupid as it looks like.


Just imagine that String class is not really string class as we understand it -- maybe it's more complicated then just string and which is more important it just can't be initialized by just "" object (let's assume it's a structure having 100 members that are filled based on time of day passed).


Or, In addition -- that class could be implemented just as a singleton (or maybe it just a proxy to singleton) -- So, any declarative initialization described as a "proper solution" will initialize real singleton to "Sunday"? Isn't that what we want?


So, my point is: it's always much efffortless to criticize -- this criticizm doesn't pay your bills though.

Re: Switched on Loops

2005-03-23 17:50 • by Chep
31690 in reply to 31670
Is modifying the iterator count during a switch even allowed/documented in C?


It breaks MISRA-C:2004 rule 13.6 (which
makes :1998 rule#67 required), but otherwise in classic C or classic
C++, it only raises seasoned eyebrows.



Here's a nice one in CORBA:



IDL:




struct Foo {
   unsigned long dmy;
    // that's it
};

interface Bar {
    void fred(in Foo foo);
};




server-side:




class SomeBarImpl: public POA_Bar, some_more_junk {
public:

     void fred(const Foo& foo) throw (CORBA::SystemException) {
           int day = foo.dmy & 0x000000FF;
           int month = (foo.dmy & 0x0000FF00) >> 8;
           int year = (foo.dmy & 0xFFFF0000) >> 6;
}
};




client side:




void someCaller(Bar* bar, int day, int month, int day) {
     Foo foo;
     foo.dmy = day | (month << 8) | (year << 16);
// there are pipe (OR) signs but the WTF forum eats them?
     bar.fred(foo);
}




at least it's both Y2K and Y10K compliant.



« PrevPage 1 | Page 2 | Page 3Next »

Add Comment