June 27, 2013
On Thursday, 27 June 2013 at 08:09:43 UTC, monarch_dodra wrote:
> Well if you solved it *that* way, *I* wouldn't hire you :p

Did I say I would cheat?

I meant to say "I would deliver a working solution ahead of schedule and under budget" :)
June 27, 2013
On 06/27/2013 09:48 PM, John Colvin wrote:
> On Thursday, 27 June 2013 at 18:37:26 UTC, Timon Gehr wrote:
>> On 06/26/2013 10:51 PM, Gary Willoughby wrote:
>>> Just for a bit of fun, I saw this question posted on reddit the other
>>> day and wondered how *you* would solve this in D?
>>>
>>> http://stackoverflow.com/questions/731832/interview-question-ffn-n
>>
>> int f(int x){ return x?(x&1?x:-x)+(x>0?1:-1):0; }
>>
>> unittest{
>>     foreach(x;int.min..int.max)
>>         assert(f(f(x))==-x);
>> }
>
> That highly compound statement..... Why?

It is easy to see what it is doing this way.

> Surely you would never write
> anything like this either in production code (unless absolutely
> necessary)  or in an interview?
>

Why not? The function fulfills the specification in a straightforward way.

> It's fine as a one-off, but a whole code-base full of this style would
> be horrifying.

I don't know. This depends on the kind of code base and the exact meaning of 'this style'. There are many programming languages where it is convenient/required to write every function in a single expression and those are not horrifying at all.
June 27, 2013
On Thursday, 27 June 2013 at 22:31:21 UTC, Timon Gehr wrote:
> On 06/27/2013 09:48 PM, John Colvin wrote:
>> On Thursday, 27 June 2013 at 18:37:26 UTC, Timon Gehr wrote:
>>> On 06/26/2013 10:51 PM, Gary Willoughby wrote:
>>>> Just for a bit of fun, I saw this question posted on reddit the other
>>>> day and wondered how *you* would solve this in D?
>>>>
>>>> http://stackoverflow.com/questions/731832/interview-question-ffn-n
>>>
>>> int f(int x){ return x?(x&1?x:-x)+(x>0?1:-1):0; }
>>>
>>> unittest{
>>>    foreach(x;int.min..int.max)
>>>        assert(f(f(x))==-x);
>>> }
>>
>> That highly compound statement..... Why?
>
> It is easy to see what it is doing this way.

Unless you're a hardened c/c++ etc. programmer, those 26 characters in a row are far from immediately obvious.

Personally I learnt to code in C and therefore it only took me 15-20 seconds to be sure I knew what was happening, but even so at first glance it's just an incomprehensible string of symbols.

Perhaps it's just a matter of taste, but I think that there's a sweet-spot for compact v.s. verbose and it lies somewhere to the verbose side of nested  ternary operators with no spaces.

In this example, just separating the main calculation from the special 0 case and adding a few spaces would make it much more understandable at first glance.
e.g.
int f(int x) {
    if(x)
        return (x&1 ? x:-x) + (x>0 ? 1:-1);
    return 0;
}
1 2 3 4
Next ›   Last »