Thursday, November 03, 2011

Weird obfuscation crap in their code, please don't be clever.

Weird obfuscation crap I found in a driver. 
This is an example of what NOT TO DO. 

[jsokol@jsokol-lx test]$ cat define.c
#include <stdio.h>

#define XYZ_MASK(Register,Field) \
XYZ_##Register##_##Field##_MASK

#define XYZ_SOMEREG_SOMEFIELD_MASK 8

main()
{
int x;
x = XYZ_MASK( SOMEREG, SOMEFIELD );
printf("x = %d\n", x);
}


Why in the hell would someone dynamically rewrite #define constants?!?!?
There is a reason why we don't do that.
None of the tools could tell me what the constants were defined too.
Because I see x = XYZ_MASK( SOMEREG, SOMEFIELD );
and "SOMEFIELD" is never defined.
  Instead I need to look up XYZ_MASK, but oh wait, grep doesn't show me the whole line because the \'ed it.
only to find I need to look for XYZ_SOMEREG_SOMEFIELD_MASK.
did coding it as
XYZ_MASK( SOMEREG, SOMEFIELD );
make it easier then
XYZ_SOMEREG_SOMEFIELD_MASK
Nooo, it didn't. But some one thought he was being clever.

In professional code we don't want clever because clever make the next guys job hell.


1 comment:

Anonymous said...

You know, I've seen an entire library built on that style where I work, only with some additional indirection for certain things. Much of that code is autogenerated, too.

Always drives me nuts when I'm forced to wander in there. I avoid that library like the plague most of the time.