The FOR-CASE paradigm

« Return to Article
  • cooper 2004-07-01 14:03
    Maybe Im not getting a joke here, but hy not just iterate over the 5 objects rather than have a conditional?
  • Steele Price 2004-07-01 14:39
    Exactly, you only need 1 line in the Case Statements, then use a WITH block below it. <br> <br>Some people just don't think in terms of objects... The even bigger question is do all these classes inherit from a common base class? <br> <br>VB Needs a refactoring tool that shows new coders how to do refactoring, most VB programmers don't even know what refactoring is. <br> <br>It would be really nice to see how people CORRECT the mistakes found in this site in the most efficient way. <br> <br>It's great to laugh at the &quot;most rediculous item of the day&quot; but it would even be more interesting for coders starting out to be able to see these fairly common mistakes and learn how to avoid/correct them in highly efficient ways.
  • cooper 2004-07-01 15:07
    <br>&gt;&gt;Some people just don't think in terms of objects... The even bigger question is do all these classes inherit from a common base class? <br> <br>That's what I was thinking, was maybe there was some non-inherit reason you couldn't do that in VB, and had to ref the properties directly.
  • fogelman 2004-07-01 15:17
    I love it. <br>And then you can also re-use Icnt and acheive object re-use. No byte left behind! <br> <br>It would be interesting to see if the scrubbing/ comparison of the same aleged address field for each case uses the same loop construct with favoring fixed Icnt #.. <br> <br>But stuffing arrays with each field from the record returned by the 6 different sources really gets under my skin. If one wanted a bloated copy of the data fetched they should just create a SOAP object.
  • ben.lovell@bbc.co.uk (Ben Lovell) 2004-07-01 15:21
    What Marc didnt tell you is that that was his code ;) Only kiddin Marc
  • Marc Holmes 2004-07-01 15:41
    Thanks Ben, as if my 'reputation' is not bad enough. <br> <br>I have no idea what this guy was thinking, but then you should see his UI :p <br> <br>I suppose he needed the CASE statement otherwise he'd have ended up running each piece of code 5 times, right? ;) Wait a minute... D'Oh!
  • cablito 2004-07-02 12:48
    this will never beat of the now former programmer who was able to code such a complex subroutine, with so many nested Ifs (he did not know about elseif or iif) all within a loop (scannind data records storing them in undeclared variables). <br> <br>It was so intense VB took exceptionally long to compile his project. <br> <br>Best of all his subroutine started with <br> <br>ON Error Resume Next <br> <br>
  • Jim 2004-07-04 13:56
    Isn't it wonderful when people are measued by the number lines of code that they have produced.
  • @ 2004-07-07 23:30
    You will be happy to know that this programming style isn't restricted to Basic, isn't restricted to Windows, and isn't restricted to programs running in user mode. Some Linux device drivers aren't even coded by volunteers, some are coded by professionals who work for the device makers. And yet the device works. It's astounding. <br> <br>What's in a name, in C we have to call it the select-case paradigm, but it still smells the same. What about the name of the product, or me? Sorry, I don't want to be fired.
  • Kiliman 2004-07-08 17:30
    This is how I would have done it (in C#). Note: I'm making an assumption that these are all instances of the same class (based on the identical properties and methods). <br> <br>// create array to hold list of items <br>SomeClass[] items = new SomeClass[] { ManObj, RETObj, DISObj, AGYObj, LAWObj, LiAgObj }; <br> <br>// loop through items <br>for (int i = 0; i &lt; items.Length; i++) <br>{ <br>// get item from array and then call methods, properties, etc. <br>SomeClass obj = items[i]; <br>obj.GetRecords(CaseNumber, ConnObj); <br>TxtKeyContact[i] = obj.KeyContact; <br>TxtAddName[i] = obj.Name; <br>TxtTelephone[i] = obj.Fax; <br>. <br>. <br>. <br>} <br> <br>Simple loop without any messy if or switch-case statements. And if they need to work with a new object, they simply add it to the array. <br> <br>Kiliman <br> <br>
  • xeron 2004-09-03 15:45
    There is a group of programmers who code in ASM, and write the most efficient code... at a 2600 meeting someone brought a 15-20 minute graphical presentation on a floppy and ran it on a laptop (years ago)... it was pretty impressive. If anyone knows the name of these programmers or has a link to the presentation I'd like to see it again.
  • Anonymous 2004-10-14 19:26
    Demosceners.
  • VB Syntax 2006-05-03 09:12
    Anonymous:
    You will be happy to know that this programming style isn't restricted to Basic, isn't restricted to Windows, and isn't restricted to programs running in user mode. Some Linux device drivers aren't even coded by volunteers, some are coded by professionals who work for the device makers. And yet the device works. It's astounding.
    <br>
    <br>What's in a name, in C we have to call it the select-case paradigm, but it still smells the same. What about the name of the product, or me? Sorry, I don't want to be fired.


    select-case? You're fired!
    In C, i'd call this construct a for-switch.
    Language-agnostically, I'd probably call it a loop-switch.
    How about iterative switch? A "switcheration?"
  • Azkoyen 2006-05-03 10:10
    I don't know whatever language that's written in, but shouldn't there be some sort of a 'break' statement at the end of each case block?&nbsp; If there IS a break statement in that language, and it's missing, then it means the code above results in the following execution:<br><br>START<br>first pass, exec all blocks starting from 0<br>second pass, exec all blocks starting from 1<br>third pass, exec all blocks starting from 2<br>fourth pass, exec all blocks starting from 3<br>fifth pass, exec all blocks starting from 4<br>sixth pass, exec last block only (5)<br>END<br><br>If the language does need a break line, then the code in those blocks are getting executed mutiple times, and it's an even worse WTF.<br><br>Still though, I'd love to come upon a situation that requires a breakless select in a for loop.&nbsp; That'd be cool.<br>
  • GoatCheez 2006-05-03 14:14
    Azkoyen:
    I don't know whatever language that's written in, but shouldn't there be some sort of a 'break' statement at the end of each case block?&nbsp; If there IS a break statement in that language, and it's missing, then it means the code above results in the following execution:<br><br>START<br>first pass, exec all blocks starting from 0<br>second pass, exec all blocks starting from 1<br>third pass, exec all blocks starting from 2<br>fourth pass, exec all blocks starting from 3<br>fifth pass, exec all blocks starting from 4<br>sixth pass, exec last block only (5)<br>END<br><br>If the language does need a break line, then the code in those blocks are getting executed mutiple times, and it's an even worse WTF.<br><br>Still though, I'd love to come upon a situation that requires a breakless select in a for loop.&nbsp; That'd be cool.<br>
    <br><br>Unless they intended to do it that way in the first place! I doubt they did tho lol....<br>
  • aikimark 2006-05-03 14:47
    <P>
    Anonymous:
    <BR>Simple loop without any messy if or switch-case statements. And if they need to work with a new object, they simply add it to the array. <BR><BR>Kiliman <BR>
    </P>
    <P>With the "txt" prefix of the assignment statements, I suspect that the data is going to an array of textbox controls on a form.&nbsp; As such, we can't really apply the easy fix of converting these separate txt_____ arrays into a single array of user-defined datatypes or objects.&nbsp; If that were the case, then only one assignment statement would be required.</P>
    <P>This is a VB WTF code example, so Kilman's suggestion would most likely look a little&nbsp;like this:</P><PRE>Dim objThing As Object</PRE><PRE>For Icnt = 0 to 5</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>Select Case Icnt</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>Case 0</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>ManObj.GetRecords ...<BR>set objThing = ManObj</PRE></BLOCKQUOTE><PRE>Case 1</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>RETObj.GetRecords ...<BR>set objThing =&nbsp; RETObj</PRE></BLOCKQUOTE><PRE>Case 2</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>DISObj.GetRecords ...<BR>set objThing = DISObj</PRE></BLOCKQUOTE><PRE>Case 3</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>AGYObj.GetRecords ...<BR>set objThing = AGYObj</PRE></BLOCKQUOTE><PRE>Case 4</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>LAWObj.GetRecords ...<BR>set objThing = LAWObj</PRE></BLOCKQUOTE><PRE>Case 5</PRE>
    <BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE>LiAgObj.GetRecords ...<BR>set objThing = LiAgObj</PRE></BLOCKQUOTE></BLOCKQUOTE><PRE>End Select</PRE><PRE>txt_____(Icnt) =&nbsp; objThing.______<BR>txt_____(Icnt) =&nbsp; objThing.______<BR>txt_____(Icnt) =&nbsp; objThing.______</PRE></BLOCKQUOTE><PRE>Next</PRE>
  • B 2007-12-14 09:36
    Azkoyen:
    Still though, I'd love to come upon a situation that requires a breakless select in a for loop.&nbsp; That'd be cool.

    Do you think Duff's Device satisfies your conditions?
  • Peter B. 2007-12-16 17:28
    I feel like being pedantic, so I'm going to say that it would be better to do your for block like this:

    for (int i = 0, int j = items.Length; i < j; i++)

    That way is a lot faster if you have to do it on a large list.
  • ExistentialWTF 2007-12-16 18:11
    Maybe the six objects are written by six developers who bitterly refuse to acknowledge a common ancestor.

    VB needs ducktaping.
  • EoN 2007-12-17 07:00
    Peter B.:
    I feel like being pedantic, so I'm going to say that it would be better to do your for block like this:

    for (int i = 0, int j = items.Length; i < j; i++)

    That way is a lot faster if you have to do it on a large list.


    Thank god someone pointed this out. Everyone else who posted anything before this should hop on-board the failboat. YOU FAIL.
  • randomscener 2007-12-18 10:18
    xeron, wtf really? :D
  • zzo38 2008-01-06 21:55
    I think there are 2 mistakes:

    (1) The programmer doesn't understand object-oriented programming.

    (2) The programmer is stupid, why is the FOR and CASE in there anyways?

    Probably better is:
    oA = Array(ManObj,RETObj,DISObj,AGYObj,LAWObj,LiAgObj)

    Hopefully this way will work (although I don't know because I haven't tested it) However, this is late binding.

    Also, I think they should use tabstrips instead of like they do now.
  • @ 2008-02-04 03:16
    VB Syntax:
    @:
    You will be happy to know that this programming style isn't restricted to Basic, isn't restricted to Windows, and isn't restricted to programs running in user mode. Some Linux device drivers aren't even coded by volunteers, some are coded by professionals who work for the device makers. And yet the device works. It's astounding.

    What's in a name, in C we have to call it the select-case paradigm, but it still smells the same. What about the name of the product, or me? Sorry, I don't want to be fired.

    select-case? You're fired!
    In C, i'd call this construct a for-switch.
    Language-agnostically, I'd probably call it a loop-switch.
    How about iterative switch? A "switcheration?"

    You're right, I don't know what I was thinking. In C it's a for-case, the same as in VB. This was in a device driver from a hardware maker, running in kernel mode in Linux. The code was unbelievable, and yet it worked.
  • LD 2010-03-22 03:57
    This just makes me want to cry.
  • oksupra.com 2010-06-19 03:08
    Supra shoes are so popular all over the world. Whatever you take on Supra being in the Supra Skytop Shoes market. Now Supra Strapped Shoes also very popular attention. It is nice that they actually took the time to make Supra Skate Shoes that work well. Supra shoes is a brand that has been inspired, designed and marketed by passionate individuals. We have brought to you the fullest selection of Supra footwear at cheapest price. Overload Skateshop carries a wide range of Supra Shoes to fit your 9-stair kickflips.


  • Spearhavoc! 2010-10-13 12:02
    oksupra.com:
    Supra shoes are so popular all over the world. Whatever you take on Supra being in the Supra Skytop Shoes market. Now Supra Strapped Shoes also very popular attention. It is nice that they actually took the time to make Supra Skate Shoes that work well. Supra shoes is a brand that has been inspired, designed and marketed by passionate individuals. We have brought to you the fullest selection of Supra footwear at cheapest price. Overload Skateshop carries a wide range of Supra Shoes to fit your 9-stair kickflips.


    I had no idea.

    Wait, a bot? HOW DID YOU GET PAST THE CAPTCHA?!
  • CrushU 2011-06-15 21:00
    One better;

    // loop through items
    for (int i = 0; i < items.Length; i++)
    {
    // get item from array and then call methods, properties, etc.
    doStuffOnItem(items[i]);
    }
  • jay 2012-01-03 07:50
    oh good you got this far, now turn it into a state pattern.

    :o)