Okay, there's bad code and then there's evil code, and the difference is pretty much whether or not the code wears a stylish cape and has a secret lair. - Chalain
Unintentional BadCode is the bane of the computer field. However, there are times when a programmer chooses to write code that is intentionally bad, often in a particularly astonishing way. When this is done, it transcends merely BadCode and becomes EvilCode.
While a sensible person would expect that one would never write bad code on purpose - after all, there's enough suffering in this world already - EvilCode does exist, and may be committed for a number of reasons. Most often, it is written by educators or textbook authors, as a way of illustrating some particularly nasty CodeTrap?. However, it is sometimes written as a means of JobSecurity, as revenge against those who would later have to maintain it, as a form of BrutalSarcasm with which to chide errant colleagues, or as an intellectual exercise (see InternationalObfuscatedCeeCodeContest for several examples of the latter). - JayOsako
One example of EvilCode written both for revenge and as an AttitudeAdjustmentTool, and the source of the quote at the top of the page, was a case where a programmer known as Chalain (possibly a PseudoNym) came across a piece of BadCode a CowOrker of his had written which worked something like this:
typedef enum { FOO_NONE, FOO_A, FOO_B } FOO; typedef enum { BAR_NONE, BAR_A, BAR_B } BAR; typedef enum { BAZ_NONE, BAZ_A, BAZ_B } BAZ; typedef enum { QAZ_NONE, QAZ_A, QAZ_B } QAZ; typedef enum { QUX_NONE, QUX_A, QUX_B } QUX; ... FOO foo; BAR bar; BAZ baz; QAZ qaz; QUX qux; foo = bar = baz = qaz = qux = 0;(not the actual code, of course).
Annoyed by this careless abuse of automatic conversion, and looking to teach the other programmer a lesson, he re-wrote it like this:
foo = (FOO)bar = (BAR)baz = (BAZ)qaz = (QAZ)qux = QUX_NONE;Later, on the recommendation of of another programmer who was in on the joke, he refined this to the even more appalling:
foo=static_cast<FOO>(bar=static_cast<BAR>(baz=static_cast<BAZ>(qaz=static_cast<QAZ>(qux=QUX_NONE))));In explanation of this, Chalain wrote:
This code serves a purpose: I want it to annoy the maintenance programmer that comes behind me. There's a very good chance that said programmer will be the original author, and this code will spark an argument in the break room... which is exactly what I want. If this code made you wince and say, "Ewww, that's UGLY"... then it served it's purpose. Both the original code and the new code are ugly. My code is just more honest/explicit about it.
If you really want to mess with the maintenance guy's head, one of the best techniques I've seen is to 'add' functionality by copying blocks of code out of other blocks of code, then sublty alter each copy over the course of a few weeks, and then sprinkle a few comments around which may or may not match what's going on in the code. Refactor that, maintenance boy! Did I intend each copy to be subtly different, or did I simply overlook one of the copies when I went through and altered all but one in the same way? Bonus points for submitting every file you happen to have checked out in a single changelist every 3 or 4 days, with a comment such as 'fixed lots of minor bugs.'
see BadCode, ObfuscatedCee, JobSecurity, AttitudeAdjustmentTool