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."
Permalink for comment 536381
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