
Daniel is a system programmer & web developer with 10 years of experience...
system programmer & web developer
coder . cl
a journey in software development...
spartan programming: my first impression
by Daniel Molina Wegener on 18-12-2008
After reading some articles on Spartan Programming, I’ve been feeling the need of some techniques to be teached, some of the principles that approach the programmer to better and more legible code, skipping some principles of SP and joining a mixture with a more verbose coding style. I agree with the code minimization, but some times verbose programming it’s absolutely necessary.
bool BufferWriter::writeContent(BufferObject *buf) {
return selfWriter->write(buf->dataBuffer()->memoryBlock(),
buf->dataBuffer()->blockSize());
}
A clear example are unchecked pointers. A common mistake made by Java Programmers, that always are trusting on the JVM, Garbage Collector and the language itself because seems to never fail and it’s hard to break. Dynamic language programmers would fall into the same mistake where they are in front of languages like C and C++. The same method, expressed in Java would have a work-alike bug on the unchecked reference on buf.
bool writeContent(BufferObject buf) {
selfWriter.write(buf.dataBuffer().memoryBlock(),
buf.dataBuffer().blockSize());
}
A light and misunderstanding knowledge on SP and the code would make the on liner code looks OK. Really in SP, by the strictness of the meaning would look like this:
bool writeContent(BufferObject *buf) {
if (buf != null
&& buf->dataBuffer() != null
&& buf->dataBuffer()->blockSize() > 0)
return selfWriter->write(buf->dataBuffer()->memoryBlock(),
buf->dataBuffer()->blockSize());
return false;
}
After choosing the way of SP, you must be considering a good editor for your code, like Emacs or VIM. Editors such as eclipse have the ugliest line breaking and indentation. In a file edited in eclipse, under an hex editor, you can find DOS line breaks “\r\n“, Unix line breaks “\n” and Mac — before OS X — line breaks “\r” mixed together. Also eclipse can shake a mix of tabular, space and tabular-space indentation behavior. Other editors like textmate have a work-alike behavior. Just try to imagine your self maintaining an old application with these editor those editors, an old compiled would crash thanks to your favourite editor. In example, with wrong editor, you could look the code as follows:
bool writeContent(BufferObject *buf) {
if (buf != null
&& buf->dataBuffer() != null
&& buf->dataBuffer()->blockSize() > 0)
return selfWriter->write(buf->dataBuffer()->memoryBlock(),
buf->dataBuffer()->blockSize());
return false;
}
A piece of bul*t — I love the strictness of python on what indentation refers. SP requires a good indentation and the right strictness on the code. Some web designers really don’t fit the SP model on html programming.
on the code minimization
On code minimization we must be ruled under the strictness that requires the language we are programming on. It’s important to remember that code minimization doesn’t means code corruption and one-liner jihad. The spartan minimization regards considerations compile-speed and algorithmic minimization, all referred on the code metrics: horizontal complexity, vertical complexity, token count, character count, parameter, variable, loops and conditionals. There are a lot of examples on how the code can be minimized.
horizontal complexity
Blow off the unnecessary else statements and nested conditionals. A classic example:
if ((x != 0 || y != 0) && z != 0) {
return ((x + y) * z);
} else {
return ((x + z) * y);
}
This code should be minimized to:
if ((x | y) != 0 && z != 0)
return ((x + y) * z);
return ((x + z) * y);
And comparing the behavior with a profiler with the algorithm written in C:
% cumulative self self total
time seconds seconds calls ms/call ms/call name
17.6 0.01 0.00 65000 0.00 0.00 long_func [2]
5.9 0.01 0.00 65000 0.00 0.00 minimized_func [3]
We get great results on the algorithm optimization minimization…
Also, SP requires that you must understand the complete algorithm, and isn’t a just for fun minimization, the second algorithm is in the fact, more faster than the first, but for some people more hard to understand — really this can occur. There are a lot of techniques on code minimization, should be a must on your SP learning adventure, also it’s clear that verbose programming in this case don’t apply.
vertical complexity
Just reduce the lines of code, less lines, are less tokens to parse, similar to the token count task. Horizontal complexity and code minimization can help a lot, also, minimal block comments — not less comments — like GNU Coding Standards proposal on comments, but can be applied to some documentation systems like Doxygen.
number of tokens
On token count, I prefers things like KNF indentation, just because tabular indentation means less tokens to parse and faster compile time and less CPU usage. Also, the number of curly braces and some other tokens, such as unnecessary parenthesis, mainly in math expressions would help a lot on code minimization. The right editor also helps a lot on common parsing tasks.
character count
Nothing to discuss, many times the client request you a naming convention or you must follow certain naming conventions if you work on a third party FOSS project, and then you are lost, but also you can get a reading on Generic Names and Micro Abstraction.
parameters and variables
Variable minimization it’s a must read article, mainly the section Inlining vs. Optimization and Clarity, everyone agrees with the clarity of the code, but also needs of good programming skills to apply this kind of minimization.
be careful with premature optimizations
This is an interesting article on what must be learned and what be the right behavior for anyone that begins on computer science and also IMO for anyone that do professional programming. I completely agree with this, you must have your algorithms working before you go with any kind of optimizations, like the SP given optimization by code minimization.
strict modularization
Modularization it’s an important issue today. The OO methodology bring us some well designed overview of some problems, and also we can share this perspective with some imperative programming languages and functional programming languages too. But also, depending on the software that we are developing, we can need other perspectives, such as Subject Oriented Programming and Aspect Oriented Programming. IMO, we must think dynamically about the problem we are in front of…


You don’t have done an algorithm optimization, but an optimization of the implementation
Yes, thats true in this case.
Also is there a strange research from Micro$oft on that issue: “WHEN ARE TWO ALGORITHMS THE SAME?”
http://research.microsoft.com/pubs/70544/tr-2008-20.pdf
I think that I must read it, I don’t find any reason on why Micro$oft is interested on differentiating their algorithms, maybe some patent troll issue…