Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
General Development "I've been programming professionally for about 3 years at this point, and I've noticed some interesting patterns in other programmers I've worked with. One of the key differentiators among programmers is motivation. I'm not referring to an individual's passion to simply be successful in their career, but rather the type of work they want to pursue. The thing they want to do with computers every day, the types of problems they are interested in solving."
Thread beginning with comment 536381
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE: Sad Truth
by Loreia on Tue 25th Sep 2012 08:28 UTC in reply to "Sad Truth"
Loreia
Member since:
2012-01-17


In case anyone is curious, this one liner function gives you the absolute value of some 32-bit integer 'num':

"int myAbs(const int num) { return num < 0 ? (~num) + 1 : num; }

There is another way: multiply the number by itself and return the square root of the multiplication (which will make it work with real numbers if you replace all instances of 'int' with 'double'). But the above code should suffice, given the requirement explicitly states 'some 32-bit signed integer'.

"

I actually have few issues with your code.
1. Why do you use trigraph here? It only makes code it harder to read. Not by much, but still, I need to stop for few seconds to decipher that line. If ... else block would be much nicer.

2. I try to use trigraphs only in combination with boolean values:
return isPositive ? num : (-1)*num;
This form makes it much easier to read the code. Boolean expressions fit much nicely in if ... else block.

These two points might be a personal preference, but I believe that code readability is the most important thing in coding (I did work in design maintenance for years, so my views are a bit distorted though)

3. Why not use abs function? Why do you need an implementation of your own? My response during the interview would be: There is already a function that does just that, in C/C++ I would use abs. One downside is the ugliness of using abs labs, cabs, llabs, fabs for different number types, but your example was about ints, so abs would be a natural choice.

4. And finally, the implementation itself.
Why would you use two's complement for such trivial thing? Two's complement is something they teach you at the university, and 99% of people forget about it before they graduate. That means more likely that not, other programmers will not be able to figure out your code at first glance.
What would be the reason for this kind of complexity? Speed of execution? Why don't we let compiler worry about that, and just write num*(-1)?

I work in a company where programmers regularly write functions that wrap assembler code. You see, our in house compiler is not very smart with optimizations, but for regular C/C++ I'd recommend letting compiler optimize such trivial things like num*(-1).

5. What to say about your other solution?
That one is almost in the league of what student wrote. Multiply, than square root? Compared to num*(-1) logic is complex and there's just too much work.


Other than this example, I support your comment 100%.

BR
Loreia

Reply Parent Score: 3

RE[2]: Sad Truth
by phreck on Tue 25th Sep 2012 08:53 in reply to "RE: Sad Truth"
phreck Member since:
2009-08-13

Nitpick: "Trigraphs" -> http://de.wikipedia.org/wiki/Trigraph

You really meant "Ternary Operator", often called "The Ternary [Operator]", because there is only one in both C and C++.

Then: Instead of multiply by -1, just use the unary negation operator.


I disagree about the bad readability of the ternary operator. It is just as readable as an if-statement or class-declarations. The one thing you have to do, like with all other things in C++, you just learn it. That's it.

As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).


Apart from that, I totally agree to your last three points.

Edited 2012-09-25 08:59 UTC

Reply Parent Score: 2

RE[3]: Sad Truth
by Loreia on Tue 25th Sep 2012 09:20 in reply to "RE[2]: Sad Truth"
Loreia Member since:
2012-01-17

Nitpick: "Trigraphs" -> http://de.wikipedia.org/wiki/Trigraph

You really meant "Ternary Operator", often called "The Ternary [Operator]", because there is only one in both C and C++.

Then: Instead of multiply by -1, just use the unary negation operator.


I disagree about the bad readability of the ternary operator. It is just as readable as an if-statement or class-declarations. The one thing you have to do, like with all other things in C++, you just learn it. That's it.

As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).


Apart from that, I totally agree to your last three points.


Yes, ternary operator, thanks for pointing it out, I didn't even notice my mistake.

I find ternary operator less readable than if-else block, for anything longer than:

bool ? 0 : 1

In this specific case, I see no reason in condensing function to one line by using ternary operator.
I prefer:

int funct (int num)
{
if (num < 0)
retrun num*(-1)

return num
}


It just takes less time and effort to read that than this:

int funct(int num){return (num<0)?num*(-1):num;}


Again, it is a personal preference, and ternary operator certainly has its place (and I do use it) in C/C++.

As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).

Absolutely, that was exactly my point.

Then: Instead of multiply by -1, just use the unary negation operator.

Even better, I actually first read comment by Neolander, and followed what he said (trigraph and *(-1)). I guess lack of sleep kicked in and I didn't notice there was even simpler solution.

BR
Loreia

Reply Parent Score: 2

RE[2]: Sad Truth
by JeeperMate on Tue 25th Sep 2012 11:33 in reply to "RE: Sad Truth"
JeeperMate Member since:
2010-06-12

Question no. 1:
Really? we encounter ternary operator on a day-to-day basis. It's very common here. That snippet in my original post is just an example, for the sake of brevity.

Question No. 3:
This is already answered in one of my earlier replies. To put it another way, my question to the applicant was essentially "could you write an example of abs() implementation for me?".

As for No. 4:
Very few people forget such basic operation. Most fresh (or semi-fresh) grads still remember. And it's not a matter of optimization whatsoever. Again, that's just an example. It doesn't mean I would frown at solutions different from mine. I just don't like bad solutions.

Even the solution I wrote in my original post has a bug, which I would've asked about, if he would've come up with something similar. But this follow-up question is only meant to test his mastery, which is not compulsory for the position he was being interviewed for.

Question No. 5:
Given such solution, I would ask the applicant about overflows, but again, this is a test of mastery and is not compulsory for the position in question, but a test that must be performed nonetheless.

Reply Parent Score: 1

RE[3]: Sad Truth
by phreck on Tue 25th Sep 2012 12:04 in reply to "RE[2]: Sad Truth"
phreck Member since:
2009-08-13

I somewhat don't believe you that you knew about the bug when you wrote your first post.

Reply Parent Score: 1

RE[3]: Sad Truth
by Loreia on Tue 25th Sep 2012 13:17 in reply to "RE[2]: Sad Truth"
Loreia Member since:
2012-01-17

Question no. 1:
Really? we encounter ternary operator on a day-to-day basis. It's very common here. That snippet in my original post is just an example, for the sake of brevity.

Question No. 3:
This is already answered in one of my earlier replies. To put it another way, my question to the applicant was essentially "could you write an example of abs() implementation for me?".

As for No. 4:
Very few people forget such basic operation. Most fresh (or semi-fresh) grads still remember. And it's not a matter of optimization whatsoever. Again, that's just an example. It doesn't mean I would frown at solutions different from mine. I just don't like bad solutions.

Even the solution I wrote in my original post has a bug, which I would've asked about, if he would've come up with something similar. But this follow-up question is only meant to test his mastery, which is not compulsory for the position he was being interviewed for.

Question No. 5:
Given such solution, I would ask the applicant about overflows, but again, this is a test of mastery and is not compulsory for the position in question, but a test that must be performed nonetheless.


1. Yes really, I find that ternary operator out of place in your example. But as I said in another discussion, it is a matter of personal preference, and as such hardly worth a long discussion.

3. When you put it like that, it is a whole another story. In normal C++ code, one would use abs(num) and be done with it. But the way I read your posts here, it sounds like you think (or have thought) that abs is some sort of non standard, third library, hack. Another strange thing I noticed in some of your posts, was criticizing use of Google. What is the problem with that? Google is a great tool to find quick and simple solutions for small problems. In fact, Google will more likely that not (in my experience), point to the standard (most typical) way.

4. Still it is relatively complex and unintuitive binary hack and if I was mentoring you, I'd point that function as something that needs to be simplified. You say that you don't like bad solution, and converting to string for testing of presence of minus sign is certainly a bad solution, but I would argue that your implementation is also a bad solution just to a lesser degree. And the second solution you offered is bad in both performance and correctness as it covers only numbers up to 2**16 (for 32 bit integers).

And what bug are you talking about? Passing in unsigned int?

5. Fair enough.

The thing is, your original post made a lot of sense until you wrote your own code. You have obviously interviewed a newbie, and his mistakes were naive, even funny, but then you offered something from the same neighborhood. That is what I was aiming at.

BR
Loreia

Reply Parent Score: 1