Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Lack of Clarity

Despite what competitions like the Obfuscated C Code Contest might let one believe, the writing of complex undecipherable code is not a sign of programmer prowess. Consider as an example the following code fragment, found in a widely used program.

     if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
      nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);

Amazingly, this mind-twisting sequence could be simply rewritten as

     if (SWITCH_TAKES_ARG (c) && (p[1] == 0))
      nskip++;

which, incidentally, is also slightly more efficient than the initial code.

We can’t express all algorithms in a clear manner. However, some would regard deliberately obtuse code as an insult toward the developers who will maintain it in the future.

When you see code that appears overly complex, try to simplify it, starting with expression or statement-level refactoring operations. If you succeed, you’ll know the code’s quality isn’t all that great, and you’ll also leave the code in a state slightly better than the state you found it in.

  • + Share This
  • 🔖 Save To Your Account