# IT and Donuts

• Progammers thrive on donuts, but what about when the donuts need a programmer?

Compute a gas station's ideal number of donuts of a give type for the coming week -- a representative line:

\$Idl{\$t}=[map{sprintf"%.0f",\$?(\$<\$m->{\$t]?\$m->[\$t]:\$):undef}map{ref(\$aS->[\$])&&\$ajS->[\$]/(1-(.08>1/(\$ajS->[\$]<\$D->[\$]&&\$ajS->[\$]>1.5?\$ajS->[\$]:\$D->[\$])?.08:1/(\$ajS->[\$]>2?\$ajS->[\$]:\$D->[\$_])))}(0..6)];

• @Sprinkles said:

Progammers thrive on donuts, but what about when the donuts need a programmer?

Compute a gas station's ideal number of donuts of a give type for the coming week -- a representative line:

`\$Idl{\$t}=[map{sprintf"%.0f",\$_?(\$_{\$t]?\$m->[\$t]:\$_):undef}map{ref(\$aS->[\$_])&&\$ajS->[\$_]/(1-(.08>1/(\$ajS->[\$_][\$_]&&\$ajS->[\$_]>1.5?\$ajS->[\$_]:\$D->[\$_])?.08:1/(\$ajS->[\$_]>2?\$ajS->[\$_]:\$D->[\$_])))}(0..6)];`

FTFY

•  It looks just like any other line of Perl on the planet.

• @Weng said:

It looks just like any other line of Perl on the planet.

+1

• @Weng said:

It looks just like any other line of Perl on the planet.

• @Weng said:

It looks just like any other line of Perl on the planet.

Perl:  For the 1337 programmer that likes writing entire programs in one line.

• @Weng said:

It looks just like any other line of Perl on the planet.
Perl is just applied emoticons.

• @belgariontheking said:

Perl is just applied emoticons.

Oddly, only emoticons for anger, frustration and despair.

• ``` \$Idl{\$t} = [   map{     sprintf "%.0f", \$_ ? (\${\$t] ? \$m->[\$t] : \$) : undef   }   map{     ref(\$aS->[\$])       && \$ajS->[\$] / (1 - (.08 > 1 / (         \$ajS->[\$][\$] && \$ajS->[\$] > 1.5 ? \$ajS->[\$] : \$D->[\$]       ) ? .08 : 1 / (\$ajS->[\$] > 2 ? \$ajS->[\$] : \$D->[\$])     ))   }(0..6) ]; ```

Substitute some meaningful variable names, and you're well on your way to being able to refactor it into a set of simple lines. Of course, if by "representative line" the OP means "there are hundreds of other lines just as bad", then yeah, you've got a real chore ahead.

At my last job, one of our major programs had one line of code that ran to nearly 2K characters. BASIC, mind you, so I could have refactored it easily enough except I don't remember whether the dialect had added support for multi-line if/then blocks yet.

• @emurphy said:

```\$Idl{\$t} = [   map{     sprintf "%.0f", \$_ ? (\${\$t] ? \$m->[\$t] : \$) : undef   }   map{     ref(\$aS->[\$])       && \$ajS->[\$] / (1 - (.08 > 1 / (         \$ajS->[\$][\$] && \$ajS->[\$] > 1.5 ? \$ajS->[\$] : \$D->[\$]       ) ? .08 : 1 / (\$ajS->[\$] > 2 ? \$ajS->[\$] : \$D->[\$])     ))   }(0..6) ]; ```

Substitute some meaningful variable names, and you're well on your way to being able to refactor it into a set of simple lines. Of course, if by "representative line" the OP means "there are hundreds of other lines just as bad", then yeah, you've got a real chore ahead.

Uhm, are you sure this looks much better? The line starting with sprintf doesn't even have matching brackets... or is that { just a typo?

• @Weng said:

It looks just like any other line of code written by a poor software engineer on the planet.

FTFY

parse an outlook express dbx database file and output the subject of each email:

use Mail::Box::Dbx;
my \$dbx = Mail::Box::Dbx->new(folder => 'c:/path/to/database.dbx')
foreach my \$mid ( \$dbx->messageIds ) {
my \$message = \$dbx->find(\$mid);
print \$message->subject, "\n";
}

• @derula said:

@emurphy said:
```\$Idl{\$t} = [   map{     sprintf "%.0f", \$_ ? (\${\$t] ? \$m->[\$t] : \$) : undef   }   map{     ref(\$aS->[\$])       && \$ajS->[\$] / (1 - (.08 > 1 / (         \$ajS->[\$][\$] && \$ajS->[\$] > 1.5 ? \$ajS->[\$] : \$D->[\$]       ) ? .08 : 1 / (\$ajS->[\$] > 2 ? \$ajS->[\$] : \$D->[\$])     ))   }(0..6) ]; ```

Substitute some meaningful variable names, and you're well on your way to being able to refactor it into a set of simple lines. Of course, if by "representative line" the OP means "there are hundreds of other lines just as bad", then yeah, you've got a real chore ahead.

Uhm, are you sure this looks much better? The line starting with sprintf doesn't even have matching brackets... or is that { just a typo?

Might have been a typo in the OP.  Anyway, \$ajS->[\$] shows up 6 times (7 if you assume that \$aS->[\$] is a typo), so just replacing all of those is a significant step.

• @emurphy said:

Might have been a typo in the OP.  Anyway, \$ajS->[\$] shows up 6 times (7 if you assume that \$aS->[\$] is a typo), so just replacing all of those is a significant step.

I don't get it. Can someone who knows Perl write that... a little nicer?

The part in the second map{} however reminds me of this gem of mine (language is Ruby, but no pun intended, srsly):

`y==0 ?(x==0 ? nil : 3*((x<=>0)-1)):(y<=>0)*(3-(x<=>0)*(x.abs<y.abs ? 1 : 2))`

Okay, it has less floating point math and devisions, but it was fun to trim some 21-line code down to this.

However, I roughly know what it does, and this is for a personal project, meaning that now other developer will ever have to deal with it.

• @derula said:

this is for a personal project, meaning that now other developer will ever have to deal with it.

This is an oft-heard excuse for crap code, and it's wrong.

There is another developer who will need to understand this code. It's your future self.

@derula said:

it was fun to trim some 21-line code down to this.

This is an oft-heard excuse for crap code, and it's completely valid in every way.

As for the code;

You were doing something with 3*3 grids and coordinates?

• @dhromed said:

As for the code;

You were doing something with 3*3 grids and coordinates?

I know I should have anonymized the variable names.

@dhromed said:

There is another developer who will need to understand this code. It's your future self.

My future self will read the comment above the line which will tell him that I don't know what the following line does but that it's mostly a cheaper replacement for Math.atan2. Accompanied by how exactly the call to Math.atan2 would look like if it hadn't been commented out.

If my future self will find a bug in there, he'll just delete that crap and reintroduce the atan2 call; after all, there's more processing power in the future.

And then he'll find out that it isn't equivalent and that it changes the AI significantly, and he'll probably curse me. But that's okay.

• @dhromed said:

This is an oft-heard excuse for crap code, and it's wrong.

There is another developer who will need to understand this code. It's your future self.

Future Morbs is a dick, anyway.

• @morbiuswilters said:

Future Morbs is a dick, anyway.

Preserving the status-quo, no?

• @dhromed said:

@morbiuswilters said:

Future Morbs is a dick, anyway.

Preserving the status-quo, no?

True, but he's still an asshole so there's no reason for me to care what he will think about my codez.

• @morbiuswilters said:

@dhromed said:

This is an oft-heard excuse for crap code, and it's wrong.

There is another developer who will need to understand this code. It's your future self.

Future Morbs is a dick, anyway.

Ugly, too.

• @derula said:

Uhm, are you sure this looks much better? The line starting with sprintf doesn't even have matching brackets... or is that { just a typo?

Behold the power of white space.  That mismatched {] "pair" is in the original.

That having been said, `\$ajS->[\$_]` is used both as a number and as an array reference.  I would like to be unaware of any perl hacks that make that legal in a useful manner.  In any event, when it is used as an array reference, it's given the same index twice.  That also looks like a bug.

```\$Idl{\$t} = [
map {
sprintf "%.0f", \$_ ? (\$_[\$t] ? \$m->[\$t] : \$_) : undef
}
map {
if (ref \$aS->[\$_]) {
my \$ajS_element = \$ajS->[\$_];
\$ajS_element / (1 - ((.08 > 1
/ (
(\$ajS_element && \$ajS_element > 1.5) ? \$ajS_element
: \$D->[\$_]
)) ? .08 : 1 / (\$ajS_element > 2 ? \$ajS_element : \$D->[\$_])
));
}
} (0..6)
];
```

Next, we have some confusion thanks to excessively multi-level inline ifs.  Breaking out the first, and converting the second to a standard if1 gives us:

```\$Idl{\$t} = [
map {
sprintf "%.0f", \$_ ? (\$_[\$t] ? \$m->[\$t] : \$_) : undef
}
map {
if (ref \$aS->[\$_]) {
my \$ajS_element = \$ajS->[\$_];
my \$ajS_element_or_D
= (\$ajS_element && \$ajS_element > 1.5) ? \$ajS_element
: \$D->[\$_];
if (\$ajS_element_or_D > 12.5) {
\$ajS_element / .92;
} elsif (\$ajS_element > 2) {
\$ajS_element / (1 - 1 / \$ajS_element);
} else {
\$ajS_element / (1 - 1 / \$D->[\$_]);
}
}
} (0..6)
];
```

That's still a mess, but there's only so much one can do with a single code line.  For example, we can't tell if there might be a \$SIG{__WARN__} in effect, which would do something special with that 'undef' if warnings are turned on.  Also, we don't know if \$ajS->[\$_] evaluates to zero often enough to warrant putting in code to optimize that case.&nbs; (If it is zero, the map element for that iteration is zero).

1 If I recall correctly, the return value of an if clause is not defined in C.  However, it is defined in Perl, to be the value of whichever block is evaluated; if there's no else block and the condition is false, the value is that of the condition (which is false, but since there are multiple false values, the specific condition result may still be useful.)

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