Неплохая инструкция о том, как нужно писать гарантирующий трудоустройство программный код.
http://freeworld.thc.org/root/phun/unmaintain.htmlПара примеров:
Cargill's QuandryTake advantage of Cargill's quandary (I think this was his) "any design problem can be solved by adding an additional level of indirection, except for too many levels of indirection." Decompose OO programs until it becomes nearly impossible to find a method which actually updates program state. Better yet, arrange all such occurrences to be activated as callbacks from by traversing pointer forests which are known to contain every function pointer used within the entire system. Arrange for the forest traversals to be activated as side-effects from releasing reference counted objects previously created via deep copies which aren't really all that deep.
Lisp LISP is a dream language for the writer of unmaintainable code. Consider these baffling fragments:
(lambda (*<8-]= *<8-[= ) (or *<8-]= *<8-[= ))
(defun :-] (<) (= < 2))
(defun !(!)(if(and(funcall(lambda(!)(if(and '(< 0)(< ! 2))1 nil))(1+ !))
(not(null '(lambda(!)(if(< 1 !)t nil)))))1(* !(!(1- !)))))
A Real Life ExampleHere's a real life example written by a master. Let's look at all the different techniques he packed into this single C function.
Код:
void* Realocate(void*buf, int os, int ns)
{
void*temp;
temp = malloc(os);
memcpy((void*)temp, (void*)buf, os);
free(buf);
buf = malloc(ns);
memset(buf, 0, ns);
memcpy((void*)buf, (void*)temp, ns);
return buf;
}
- Reinvent simple functions which are part of the standard libraries.
- The word Realocate is not spelled correctly. Never underestimate the power of creative spelling.
- Make a temporary copy of input buffer for no real reason.
- Cast things for no reason. memcpy() takes (void*), so cast our pointers even though they're already (void*). Bonus for the fact that you could pass anything anyway.
- Never bothered to free temp. This will cause a slow memory leak, that may not show up until the program has been running for days.
- Copy more than necessary from the buffer just in case. This will only cause a core dump on Unix, not Windows.
- It should be obvious that os and ns stand for "old size" and "new size".
- After allocating buf, memset it to 0. Don't use calloc() because somebody might rewrite the ANSI spec so that calloc() fills the buffer with something other than 0. (Never mind the fact that we're about to copy exactly the same amount of data into buf.)
Некоторые из примеров, к сожалению, попадаются мне в жизни.
Цитата:
If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.