Last minute geek

last minute tech news from around the net

You are here: English WTF Bring Your Own Code: A Foursome of Arrays

Bring Your Own Code: A Foursome of Arrays

User Rating: / 0
PoorBest 

So, fun fact about myself: I didn’t know what the For-Case anti-pattern was until relatively recently, when there were a spate of articles condemning it as an anti-pattern. I’m sure I’ve probably used it, at some point, but I never knew it by name. It’s thought of as a textbook antipattern that generally implies a misunderstanding of for loop, case statements, the problem being solved, or some combination of all three. That said, there are certain problems that might be more clear to solve by using the For-Case. Like GOTO, it might be harmful, but its actual evil exceeds its reputation.

John A had a problem, and most unfortunately for him, this problem involved VBA macros embedded in an Excel spreadsheet. He needed to generate four arrays, that fall into this pattern:

    ' Generate 4 Arrays of the form (example n=4)
    '       | 1 |         | 0 0 0 |
    ' e_1 = | 0 |   n_1 = | 1 0 0 |
    '       | 0 |         | 0 1 0 |
    '       | 0 |         | 0 0 1 |
    '
    '       | 0 |         | 1 0 0 |
    ' e_n = | 0 |   n_n = | 0 1 0 |
    '       | 0 |         | 0 0 1 |
    '       | 1 |         | 0 0 0 |

Now, I don’t know what these arrays are for, and I’m only guessing at the pattern. John spent hours fiddling with this, and couldn’t find a great solution. So he reached for the For-Case. I’ll let him explain:

Sure I laughed when others implemented the vile For-Case paradigm. I said this will never happen to me. But today everything changed when I was staring at the screen trying to figure out how to most efficiently and cleanly fill in 4 arrays with the specified pattern. I thought of multiple sequential loops within nested loops and saw that there will always be some repeated statements. It all changed when I decided to set everything to zeros first and then set the non zero entries (I was trying to do all elements at the same time before).

Finally I succumb to the allure of the For-Case paradigm and it materialized in front of me in seconds. My fingers were typing furiously as if they knew when they had to do. Glory at last!. Small, clean and clear. I have betrayed my kind to achieve my most satisfying code fail so far.

Public Sub DefineProjectionArrays(ByVal n As Long, ByRef e_1 As Variant, ByRef e_n As Variant, ByRef n_1 As Variant, ByRef n_n As Variant)
    Dim i As Long, j As Long

    ' Generate 4 Arrays of the form (example n=4)
    '       | 1 |         | 0 0 0 |
    ' e_1 = | 0 |   n_1 = | 1 0 0 |
    '       | 0 |         | 0 1 0 |
    '       | 0 |         | 0 0 1 |
    '
    '       | 0 |         | 1 0 0 |
    ' e_n = | 0 |   n_n = | 0 1 0 |
    '       | 0 |         | 0 0 1 |
    '       | 1 |         | 0 0 0 |

    ' Fill arrays with zeros
    e_1 = NewArray(n, 1, 0#): e_n = NewArray(n, 1, 0#)
    n_1 = NewArray(n, n - 1, 0#): n_n = NewArray(n, n - 1, 0#)

    ' Set unit elements according to the pattern above
    For i = 1 To n
        Select Case i
        Case 1:                                 ' first row of e_1 and n_n have values
            e_1(i, 1) = 1#
            n_n(i, i) = 1#
        Case n:                                 ' last row of e_n and n_1 have values
            e_n(i, 1) = 1#
            n_1(i, i - 1) = 1#
        Case Else                               ' middle rows of n_1 and n_n have values
            n_1(i, i - 1) = 1#
            n_n(i, i) = 1#
        End Select
    Next i
End Sub

`

Now, I’m gonna go ahead and say it: this doesn’t really look all that terrible. It’s not a WTF. If anything, it’s a good example of how a For-Case statement can be used to make clear code. I can come up with minor tweaks to it to make it clearer and cleaner, but as it is, it’s fine. I wouldn’t want to curse the name of the developer who handed me this code.

But John feels bad about it, continuing: “And now challenge to the community of coming up with better coding solutions to this.”

Well, I welcome your better solutions to this problem, but what I’d really love to see is creatively worse solutions. Given the vague requirements (and the singular sample case), what’s the worst way you could implement this? In the language of your choosing, what’s the dumbest way to accomplish this goal? Bring your own code, either in the comments, or submit your WTF (use the title of this article as your subject line). We’ll take submissions over the next week, and on 5/18 we’ll run a followup article with the best submissions. The “winners” get bragging rights, and a pile of WTF stickers and other swag that we can mail cheaply.

[Advertisement] Otter, ProGet, BuildMaster – robust, powerful, scalable, and reliable additions to your existing DevOps toolchain.

Read all
Comment Policy:
We pre-moderate any comments and welcome all kinds of thoughts, supportive, dissenting, critical or otherwise. We delete or censor comments that are:

* abusive
* off-topic
* contain personal attacks, or against any company or organization
* promote hate of any kind
* use excessively foul language
* is blatantly spam or advertising

We do not discriminate based on the person who is posting, and we never censor comments for political or ideological reasons. We never delete an appropriate comment because we disagree with its viewpoint or ideology, and we never publish an inappropriate comment because we agree with or support its viewpoint or ideology.


Attention spammers: we manually approve all comments. Spamming and blatant advertising will NOT be published on this site and is deleted immediately, you've been warned, do not waste your time here.

Add comment

Security code
Refresh