52

If a coder writes code that nobody other than he can understand, and code reviews always end with the reviewer scratching his head or holding their head in their hands, is this a clear sign that the coder is simply not cut-out for professional programming? Would this be enough to warrant a career change? How important is comprehensible code in this industry?

Consider these C examples, compare:

if (b)
  return 42;
else
  return 7;

versus:

return (b * 42) | (~(b - 1) * 7);

Is there ever an excuse to employ the latter example? if so, when and why?

EDIT: Leaving the original latter snippet for consideration, adding a correction:

return (b * 42) | ((b - 1) & 7);

I think the other interesting observation is that it requires that b is 1 for true and 0 for false, any other values would render strange results.

Coder Guy
  • 727
  • 91
    Programmers who don't understand that communicating with other people is the main purpose of source-code probably don't deserve their title. – msw Feb 28 '15 at 00:18
  • Programmers need to actually design their code. Most of the time clear nice code is the best alternative, but there are also situations where cryptic code is better -- for example if the logic is important and you don't want people to modify it unnecessarily; cryptic code might prevent unwanted modifications to the code. It's also better for job security.... – tp1 Feb 28 '15 at 00:19
  • 81
    Those two pieces of code don't look equivalent to me. Regardless, there's an easier way to write it: return b ? 42 : 7; Yes, the second example is unnecessarily obtuse if it is, in fact, equivalent to the first. – Robert Harvey Feb 28 '15 at 00:20
  • 60
    They aren't equivalent. Running the second one produces -5 or 0, not 42 or 7. The fact that I had to use ideone to convince myself of that shows how bad that code is. – Ixrec Feb 28 '15 at 00:59
  • 7
    Maybe the second example was meant to use logical or || instead of bitwise or |, assuming short-circuit evaluation. And assuming b is always either 0 or 1. Which just underscores the importance of testable, readable code. There is always "Job Security" for those who end up cleaning up this kind of mess. – MarkU Feb 28 '15 at 03:21
  • 15
    Why would someone do that? Deliberately and repeatedly making your coworkers' jobs harder just for the fun of it, or to show off, could very well result in an involuntary JOB change if not a career change. – Jeanne Pindar Feb 28 '15 at 03:45
  • 2
    I believe the final code sample is equivalent (assuming b can only be 0 or 1) if you change the subtraction to - 2 instead of - 1. Which doesn't make it any more readable, but at least we could get an actual discussion about the question instead of sidetracking on that. – Aaron Dufour Feb 28 '15 at 04:12
  • "assuming b can only be 0 or 1, if you change the subtraction to -2 instead of -1..." head... hurts... please... help...... My guess is that the programmer in question must have been trying to be clever about working in some dependencies/assumptions based on state existing somewhere outside the scope of that crazy return statement. We all know what "assume" does. – Craig Tullis Feb 28 '15 at 05:17
  • 2
    If writing clear code was a job requirement, I think about 8 of 10 C programmers would be out of work - and that's not even considering the level of obfuscation possible with e.g. C[^:alpha:]+. Just for instance, consider the "if (b)" in the supposedly clear example. Now isn't it much clearer to write this as "if (b == 0)", making it absolutely clear that you're doing a numeric test rather than a logical one? (And similarly when comparing pointers to NULL.) Note that I assume b is supposed to be numeric because of the multiplication in the second example. – jamesqf Feb 28 '15 at 05:19
  • 3
    Why do you ask this as if this were a hypothetical situation? In this form , some of your questions are very opinionated. For example: "How important is comprehensible code in this industry"? In some areas more than in others. "is this a clear sign that the coder is simply not cut-out for professional programming" - it may be a sign that the code is not capable of working in a team, or that he has to learn better coding, who knows. The interesting question for you should ask: does the programmer want to improve, is he interested in learning to write better code. – Doc Brown Feb 28 '15 at 09:15
  • ...nobody other than he... I'd think in terms more like "...if more than 10% of coworkers..." rather than "nobody". There might always be a few coworkers who have trouble reading anyone else's code, but code should be clear to a large majority of similar staff. They're the ones most likely needing to understand it in the future. And code reviews are pointless otherwise and should fail the code. – user2338816 Feb 28 '15 at 12:34
  • 1
    There is a time and a place for write-only code. This wasn't it. –  Feb 28 '15 at 14:17
  • Assuming the second example produces the same result I can see a reason for it: The conditional form generates a branch, the complex form does not--the cache dump from a failed branch prediction is a lot more expensive than the extra code. Thus this is an optimization--most likely an unreasonable one. It certainly should be documented what's going on. – Loren Pechtel Feb 28 '15 at 18:23
  • 1
    If that code isn't in a performance-critical path, the optimization argument is moot, right? Premature optimization isn't a virtue. If such an optimization is deemed necessary (after performance-profiling the software), the should be clear comments in the code to that effect. – Craig Tullis Feb 28 '15 at 23:58
  • @Craig That's why I said it was probably an unreasonable one. – Loren Pechtel Mar 01 '15 at 19:24
  • @LorenPechtel Yep, I was pretty sure I was echoing your sentiment. :) – Craig Tullis Mar 01 '15 at 21:01
  • For all the proposed alternatives plus commenting, I'd add that a compile time test would be very useful for these types of expressions. See for example http://stackoverflow.com/questions/3385515/static-assert-in-c – Keith Mar 02 '15 at 00:00
  • 1
    I have seen people do this so no-one could change it. Well, they change it anyway, but swear at the original author. If I don't want anyone to change my code, I comment "Do not change this, or x and y will not work". I might even refer to a technical note. – RedSonja Mar 02 '15 at 07:44
  • 2
    People forget that compiled programming languages are written for humans to understand, not computers. If humans can't understand that code then something has gone wrong. – Mashton Mar 02 '15 at 11:33
  • Those lines even make some non-portable assumptions. The former are way more secure. – Marco A. Mar 02 '15 at 13:14
  • Re 4b - in the rare cases, the cryptic code like that can yield dramatic performance improvements. That may or may not be the case in your particular situation: http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array – ya23 Mar 02 '15 at 14:00
  • 2
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan. That is all. – Sarima Mar 02 '15 at 14:32
  • 2
  • 2
    @RedSonja just add a comment "Autogenerated code, changes will be overwritten". Nobody will touch that block again. – Stephan Muller Mar 03 '15 at 11:07
  • 1
    It's critical. Code should always be as clear and expressive as possible. Failure to write comprehensible code is failure to do the job. – DeadMG Feb 28 '15 at 00:24

9 Answers9

107

The first rule of any professional software engineer is to write code that is comprehensible. The second example looks like an optimized example for an older, non-optimizing compiler or just someone who happens to want to express themselves with bitwise operators. It's pretty clear what's going on if we are familiar with bitwise operations but unless you're in a domain where that's the case, avoid the second example. I should also point out that the curly braces are missing in the first example.

The coder may insist that his code is efficient, that may be the case but if it can't be maintained, it might as well not be written at all, since it will generate horrendous technical debt down the road.

  • Comments are not for extended discussion; this conversation has been moved to chat. –  Mar 01 '15 at 23:21
  • The latter example is also difficult for the compiler to understand, which can lead to a poorly-optimized executable. It isn't a readability/performance trade-off, it's a worse-in-every-way-possible trade-off! – Gordon Gustafson Mar 02 '15 at 23:24
  • 4
    So much of this. Code is usually "write once, read a bazillion times". Optimize for reading. If the piece of code is really, really performance critical, optimize for performance, but give enough of an explanation in a comment - the reasons why you're doing it, the performance tests that show it is indeed faster, the step-by-step explanation of what's going on, the limits at which it stops working (was it tested multi-platform? Does it work for all possible values? Etc.). This way, when someone goes through the code in five years, they can easily see if the reasons still hold. – Luaan Mar 03 '15 at 08:18
  • actually the first code is more efficient. 2. if this is C then the second code is actually broken, or at least not portable.
  • – AK_ Mar 13 '15 at 16:07