- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I accept date2 - date1 may be a little obscure, but there is also date2.Subtract(date1), which really isn't that hard to find.
Admin
It might be clever if it were:
if (((dat/100) & 1) == ((dat/100) >> ((1 << 1) | 1)))
...but the code used is clearly a WTF!
Admin
Interesting; I was taught that N is problem size, and must be defined for the problem in question. The problem with saying "input length" is, of course, that the input in many problems (like this one) is fixed length.
You could call the input "variable length" by taking an arbitrary number of date pairs; but in that case, the complexity would be O(N) with N as the number of pairs accepted.
I don't see what you're donig to arrive at 2^N.
Admin
I love it
Admin
Why, what's wrong with it? Sure, if the outermost <font color="#0000ff" face="Courier New">if</font> block contains nothing but the two inner <font color="#0000ff" face="Courier New">if</font> blocks then the outer <font color="#0000ff" face="Courier New">if</font> is unnecessary. But if you want to execute code if either condition is true in addition to code for each specific condition, how else would you do it?
<font face="Courier New">{</font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> </font>
<font face="Courier New"> <font color="#0000ff">if</font> (condA)</font>
<font face="Courier New"> {</font>
<font face="Courier New"> <font color="#008000">// Do condA-specific processing...</font></font>
<font face="Courier New"> }</font>
<font face="Courier New"> </font>
<font face="Courier New"> <font color="#0000ff">if</font> (condB)</font>
<font face="Courier New"> </font><font face="Courier New"> {</font>
<font face="Courier New"> <font color="#008000">// Do condB-specific processing...</font></font>
<font face="Courier New"> }</font>
<font face="Courier New">}</font>
<font face="Arial">Is there any better way? The only one that seems to come close is...
</font></font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> </font>
<font face="Courier New"><font color="#0000ff">if</font> (condA)</font>
<font face="Courier New">{</font>
<font face="Courier New"> <font color="#008000">// Do condA-specific processing...</font></font>
<font face="Courier New">}</font>
<font face="Courier New"> </font>
<font face="Courier New"><font color="#0000ff">if</font> (condB)</font>
<font face="Courier New">{</font>
<font face="Courier New"> <font color="#008000">// Do condB-specific processing...</font></font>
<font face="Courier New">}</font>
</font><font face="Arial">...which is almost the same thing, except that when the condition in the first </font><font face="Courier New"><font color="#0000ff">if</font></font><font face="Arial"> statement evaluates to false you know that the two </font><font face="Courier New"><font color="#0000ff">if</font></font><font face="Arial"> statements that follow will also evaluate to false, so there's no point in even testing them.
This seems to work at first glance...
</font><font face="Courier New"><font color="#0000ff"></font></font>
<font face="Courier New">{</font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> <font color="#008000">// Do condA-specific processing...</font></font>
<font face="Courier New">}</font>
<font face="Courier New"> </font>
<font face="Courier New"><font color="#0000ff">if</font> (condB)</font>
<font face="Courier New">{</font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> <font color="#008000">// Do condB-specific processing...</font></font>
<font face="Courier New">}</font>
<font face="Arial">...however, if both <font face="Courier New">condA</font> and <font face="Courier New">condB</font> are true then<font face="Arial"> </font></font><font face="Arial"><font face="Courier New">DoSomethingUseful() </font></font><font face="Arial">will get called twice.
I actually found myself writing code like this recently, and though it was kind of ugly and I didn't like testing the same variables in quick sucession, I couldn't think of a better way to write it. So, am I missing something or was I merely seeing sarcasm where there wasn't any?
</font>
Admin
<font face="Courier New"><font color="#0000ff">if</font> (condA)</font>
<font face="Courier New">{</font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> <font color="#008000">// Do condA-specific processing...</font></font>
<font face="Courier New">}</font>
<font face="Courier New"> </font>
<font face="Courier New"><font color="#0000ff">else if</font> (condB)</font>
<font face="Courier New">{</font>
<font face="Courier New"> DoSomethingUseful();</font>
<font face="Courier New"> <font color="#008000">// Do condB-specific processing...</font></font>
<font face="Courier New">}</font>
Admin
Wow, talk about missing the obvious. That will definitely work, and it's painfully simple, too. I probably didn't think of that because I instinctively try to refactor duplicate code inside of an if/else block to be executed outside of the block. It's kind of a pet peeve of mine when I see that in other people's code, but I guess this is a rare instance where it serves a useful purpose.
File this under "Duh".
Admin
HEAR HEAR
Admin
What about this case makes it seem ok to have the duplicate code? It's marginally more efficient (except that with modern compilers and processors, it's probably not; at best it's premature optimization), and in exchange you have to maintain the common code in two places. The original construct is preferable, even if at a glance it looks odd.
Admin
I probably should have put // Do something useful... instead of DoSomethingUseful(), because that's closer to what I was working with. Instead of a single function call I had four lines before and two lines after the condition-specific processing, and neither of these small chunks of code really warranted its own function. In that case, yes, my solution is the probably best solution (unless there's yet another way I haven't thought of).
Crimper's suggestion wouldn't work for my case, anyways, since instead of two consecutive ifs inside an outer if block I had an if/else inside an outer if block. (It was a function that generated a string for scheduled events to be displayed in a simple calendar. Each event could have an optional start time and an optional duration, so certain formatting needed to be done if either was specified, followed by one type of formatting if a start time was specified (regardless of whether a duration was specified) and another type of formatting if only a duration was specified.)
I guess it's just one of those cases where no matter what code you come up with it probably won't qualify as "elegant".
<edit>
Why does my color formatting only show up in the quoted text in the final displayed post, even though in the editor the colors show up in both the quoted text and my reply? And why does the text I type in the "Edit Notes" box not show up anywhere, either? Weird.
</edit>
Admin
This doesn't match BACON's original example. With this "optimization", if (condA && condB), then condB-specific processing is not performed.
//capcha == "craptastic"
Admin
Uh? That is exactly the coding that the WTF sample uses, except for "+" instead of "<<" (which a decent - ok, a more than decent compiler - could have picked between in the old days when LSR was much faster than ADD...)
So, does that mean we now know the author of that fragment of beauty? ;-)
/David (I am)
Admin
It was to this message my response - which should be the previous post - referred. Sorry.
/David (I am)