Coding is all about translating a language of the concept into a language of the program. But in practice, not code, but stenographic writing is what is so often produced as an output. A perfect programming solution is already a language itself. An average developer is first lured by promising advantages of certain language, then steadily and dogmatically converted by its "exciting" lovelinesses, finally starts desperately advocating for something which he does not really masters or senses. All this painful evolution is normally cycled, when our newly matured adept continues the bloody expansion by converting new casualties. But the trick with any formal language is that it is not more than a handy instrument to put a group of symbols in a grammatically arranged joint. All the work, left there untouched, is to be conducted by the author of the program. It is the author who declares variables and drives the semantical wheel of specifying the identificators. It is he who governs the distribution of complexity through vast functionality. It is he who makes things come alive by telling the story behind the algorithm.
So, the real question is how definitive a good program could be? The code which is self-explanatory is a masterpiece of an inter-human communication (omitting the presence of computer as a mere necessity), thus automatically must be graded as a program. Such code does not require any artificial documentation or redundant comments. Such code is a pure logical semantics - above any formalism or hyper-consistency.
The introduction of the post was indeed a dramatical overkill. A reasonable need for an example must bring the discussion to a firm ground.
Python is one of those languages that, although intended to grasp the impossible - propose a constitution for perfect formalization - and, IMHO, failed, nonetheless has done better than most of its competitors. It is always up to python developer to resolve the design dogma "there is one and only one way to do something".
The following equivalent statements are a good example based on os module usage for some strings (a, b, c):
assert(os.path.join('a', 'b', 'c') == os.sep.join(('a', 'b', 'c')))
Mastering the ability to conclude which construction maybe preferable to the code readability can turn out to be an important asset in producing consistent code.
Another important example is:
a = '' assert(len(a) <= 0 == not a)
Implicit casting of a string or a list variables of zero length to boolean False, may turn out to be priceless in producing shorter, but more readable forms.
There are even more examples. Most of them must be known from studying PEP8 (Programming Recommendations). Others are an important piece of valuable practical knowledge, collected with time.
Code readability heavily depends on the person, who reads the code. Developer should be smart enough to understand, for example, the scope resolution. For the person, who doesn't know it, your code will be not readable and heavy understandable.
ReplyDeleteAnother point about "good ... code does not require any artificial documentation or redundant comments."
It could be applied only for high-level code or for damn simple code.
Try to write memory and time-efficient implementation of B-Tree, for example. In clean, and documentation-needless way.
@point: A proper introduction or "a priori" knowledge of the context and means of the language are essential. The question is how to use it?
ReplyDeleteOr is there always a place for balance? For instance, scope resolution, when possible, can be replaced by "import" statement or a shortcut.
"It could be applied only for high-level code or for damn simple code." This is a good definition for the readable code ;-)
Low-/high-level gradation points to the accessibility of the code and must by no means advocate for its ambiguity. IMHO Only a proper mathematical introduction of the meaning of "unknown" can make difference.
> Or is there always a place for balance?
ReplyDeleteThere will always be a possibility to express elegant and interpreter-efficient code in terms of simple operations.
I think, you are talking about the balance, but having in mind the people, who are the target audience of your code (not UI user, but further developers).
Simple question and abstract example: If there is an opportunity to use generators-with-send in your code, to obtain efficient code, will you use at your everyday projects ?
It's efficient, it's elegant, after all it's cool. But... Will you remake it in more simple operations? How will you feel yourself if you will do?
If you refer to code-generation tools like spring roo, zend tool, RoR, etc. than these are all stub generators, i.e. such code is generated only once. Otherwise, you may refer to codegeneration techniques or libs like cglib in Java. In this case, you don't maintain the generated code, but the template source. In both cases, there is a place for balance and readable code. PS please see http://www.ibm.com/developerworks/aix/library/au-cleancode/ as related casestudy.
ReplyDelete