Found in the codebase



  • foreach

    Hopefully this image shows up for you guys.

    I found this in one of our Perl modules.   I had to do the formatting, it was all randomly indented.  Sometimesa foreach loop would be less indented than the one before it, sometimes it would be indented an extra 12 spaces.



  • @tster said:

    Hopefully this image shows up for you guys.
    Not off the bat: FF complains about a bad security cert.



  • I wasn't able to see the image.  After getting the URL (it was https with an untrusted CA) I got this:

    <title>Runtime Error</title>

    Server Error in '/' Application.

    Runtime Error

    <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. </font> 



  •  Why an Image?  Is this not just code?

    Copy and paste it in man.  You're just as bad as our users, screen shot everything and past it into word so it gets resized to the point of unreadability with the statement "Why can't i add this style?"

    Jeesh forget the image and just tell me the style.



  • @KattMan said:

     Why an Image?  Is this not just code?

    Copy and paste it in man.  You're just as bad as our users, screen shot everything and past it into word so it gets resized to the point of unreadability with the statement "Why can't i add this style?"

    Jeesh forget the image and just tell me the style.

     

    good point.

    Here is the code (changed variable names to protect the guilty):

    foreach my $var1 ( keys %{$var0} ) {
          foreach my $var2 ( keys %{$var0->{$var1}} ) {
            foreach my $var3 ( keys %{$var0->{$var1}->{$var2}} ) {
               foreach my $var4 ( keys %{$var0->{$var1}->{$var2}->{$var3}} ) {
                 foreach my $var5 ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}}) {
                  foreach my $var6 ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}->{$var5}}) {
                  foreach my $var7 ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}->{$var5}->{$var6}} ) {
                                      foreach my $user ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}->{$var5}->{$var6}->{$var7}} ) {
                          foreach my $var8 ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}->{$var5}->{$var6}->{$var7}->{$user}} ) {
                  foreach my $var9 ( keys %{$var0->{$var1}->{$var2}->{$var3}->{$var4}->{$var5}->{$var6}->{$var7}->{$user}->{$var8}} ) {

     



  • Just going by the description, It sounds like your code editor has a different interpretation of the <tab> character than the original author's.  You can get some funky combinations of spaces and tabs which are invisible but the editor keeps on using them without making it obvious.



  • @tster said:

    Here is the code (changed variable names to protect the guilty):
    Looks like logarithmically smoothed indentation with an outlier there in the middle.  It's ok, though, the indentations got back to steady state pretty quickly.  I wouldn't worry too much.



  • while                   ( my($var1, $var1_href) = each %$var0 } ) {
    while ( my($var2, $var2_href) = each %{ $var1_href->{$var1} } ) {
    while ( my($var3, $var3_href) = each %{ $var2_href->{$var2} } ) {
    while ( my($var4, $var4_href) = each %{ $var3_href->{$var3} } ) {
    while ( my($var5, $var5_href) = each %{ $var4_href->{$var4} } ) {
    while ( my($var6, $var6_href) = each %{ $var5_href->{$var5} } ) {
    while ( my($var7, $var7_href) = each %{ $var6_href->{$var6} } ) {
    while ( my($user, $user_href) = each %{ $var7_href->{$var7} } ) {
    while ( my($var8, $var8_href) = each %{ $user_href->{$user} } ) {
    while ( my($var9, $var9_sref) = each %{ $var8_href->{$var8} } ) {

    Hope that helps.  Not that this is legible code, mind you - but it's a simple translation which won't break anything (unless, of course, you're using ${"${var}_href"} somewhere in that mess), and looks a lot better.  It should also perform a little better, as you're no longer redereferenceing like crazy in the inner-most loop.  (Note: my formatting's very eccentric to make this very eccentric code a bit more legible - ideally, one would never have code which would benefit from this sort of formatting.)

    I'd still veto it in code I was reviewing, unless it was an update to something like your above code (in which case, it eeks in under the "it's an improvement" rule.)  Given the $user section, I would guess that it is unlikely that the program really needs to evaluate all combinations of the hash keys.  In any event, those hashes don't need to be very big for this O(n^10) segment of code to be exceedingly slow.

    Edit: I forgot to mention... I've seen worse.



  • @tgape said:

    I'd still veto it in code I was reviewing, unless it was an update to something like your above code (in which case, it eeks in under the "it's an improvement" rule.)  Given the $user section, I would guess that it is unlikely that the program really needs to evaluate all combinations of the hash keys.  In any event, those hashes don't need to be very big for this O(n^10) segment of code to be exceedingly slow.

     

    If I remember right, this almost always only runs 1 time (only 1 value for each of the keys).  I just don't understand why the original developer didn't see hwat he had just written and though "you know, there is probably a better way."



  • @tster said:

    If I remember right, this almost always only runs 1 time (only 1 value for each of the keys).  I just don't understand why the original developer didn't see hwat he had just written and though "you know, there is probably a better way."
    Of course, it's hard to say without knowing the developer, but I know I've written similar WTFs, but just coudln't be bothered to go back a deWTFify working code.



  • @tster said:

    foreach
    You can see why they backed out some tabls though: They wanted each line of code to be 132 characters or less.  Maybe they're old COBOL developers.  I can see them going MUAHAHA I CAN START MY CODE BEFORE COLUMN 8!!!!



  • @tster said:

    If I remember right, this almost always only runs 1 time (only 1 value for each of the keys).

    Ok.  In that case, the fix is to stop using hashes in place of scalars.  That has the effect of completely eliminating the loops.

    @tster said:

    I just don't understand why the original developer didn't see what he had just written and though "you know, there is probably a better way."

    By the third foreach loop.  Of course, the answer is, they do this just to annoy us.  And to get us to post their exploits here.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.