100 comments so far. Am I the only person here who suspects that this Spartan programming guide is tounge-in-cheek?
Most of the things look cool except that naming convention thing. In other words one should not be burn down variable names to letters or don’t try to write obfuscated code while there are obfuscating tool already available.
this.issparta() ?
Yeah I definitely disagree about using shorter variable names. Though making them too long can also be a problem, like variableNameThatGoesForWayTooLong.
If you look out an individual line in their final coding sample, such as m.setContent(mp); you have to remember that m is the message and mp is the multi-part. Yeah I know its only a couple lines up but its still harder to read that then something like message.setContent(multiParts);
Also better named variables can help when it comes to compiling and debugging. For example, in the debugger when you watching variables its easier to know what message is then m.
to yossi gil:
return x 0 ? x : -x;
This leads to returning -0, if 0 is the input - not so nice.
I ever use parenthesis when using the ?:-pattern for the question - i think it’s easier to read this way.
If the code between the : is more complex, i use them even there ().
This post of yours, Jeff, had by far the most impact on me from everything that I have read for the last two months. Willing just to put link to the original post, I eventually ended up writing a href=http://eterniel.blogspot.com/2008/07/spartan-programming-real-man-way-to-do.htmla full 3 page essay/a about Spartan Programming and the ways to write programs like a real man (I’m sorry I borrowed your 300 spartans screenshot).
It was… very entertaining. Thank you
That C example is one UGLY piece of coding!
A person that uses if-else in that way should be taken out back and shot (or at least given one hell of a wedgie)
Yo YG, the rzn ur wrng is vars 2 shrt is 2 hrd 2 read unless ur used 2 it. I call blsht.
Maybe you need to examine whether you are objectively considering that short 12 variable names are easier in general, or whether it’s just easier for you personally. You must divorce your personal bias when making your recommendations because the recommendation needs be based on fact.
Code Complete provides reasoning backed up by studies (thks G). You need your own compelling, and scientifically valid data to contradict or at least call into question the results of the Gorla, Benander, and Benander research. Yes research is hard, but so what. Your assertion needs to be backed up by proof. Otherwise it’s just opinion, and we’ve all got 'em.
Most people here, however, seem to disagree with you. So in this court of public opinion, you’re wrong.
Not that you need to change, but just realize that what makes you happy isn’t necessarily what makes anyone else happy. You have a hypothesis at this point, and nothing more. You’ve assembled and shared a list of personal preferences. Great, but don’t pretend it’s anything more than that!
Maybe rename the wiki to ‘YG minimalist Coding Style for PHP which YG affectionately calls Spartan’
Also Note that I did notice in your final result you initially mention the Character Count and Token Counts as metrics, but you never tell us what the final result is, so what’s the point? Seriously? These are pretty poor metrics to go by.
Is summation, I declare that calling something a programming ‘style’ belies the fact that it’s just a collection of personal preferences. Code for yourself and code however you want. Otherwise code for someone else to read and maintain it.
While I take full responsibility for giving html a try even though no HTML was clearly stated, feel free to write about non-rfc compatible and buggy auto-hotlinking in modern blogging platforms
Cheers And thanks again for the original post.
Okay T.E.D. I’m sober enough to attempt a reply:
- The first half of this works great… C has no such advanced programming constructs.
Yeah fair enough.
- This works in C today, although it is only recently that compilers started allowing variables to be defined in loops.
Hmm… pretty sure this has been in since at least ISO/IEC 9899 (adopted by ISO in 1999) but I don’t have the standard handy to check.
- No such thing as a private variable in C…
You can still minimise the accessibility of variables e.g. by using static variables.
- No such thing as constants in C…
Hmm… const… page 39, KR (2nd edition)
- …I am sure it works just fine in C, whatever it is.
Yep.
- …it should work just fine in C.
Yep.
- No standard componet library for C…
Fair comment - though it does also suggest off-the-shelf libraries, which, as you pointed out, are available.
- No classes or inheritance support.
Yeah but the rule is Minimizing the use of conditionals by using specialized constructs - classes it just an example. It also suggests Ternarization which C has.
- Works fine in C.
Yep
- No classes, thus no action applicator classes.
Yep - can’t really do this in C.
- Works better in C than most other languages I’ve seen.
Yep.
- Designed for C, obviously.
Yep.
So from my point of view, almost all of those rules work for C in some respect
Not that I actually agree with all those rules by the way, but I did want to defend C’s honour… it’s too old to defend itself
Here is a meta-issue for everyone to consider. I tried reading most of the stuff written here, and noticed a couple of statements, which follow the following 3 parts syllogism:
(1) Programming practice X is good [bad], because it produces code of style Y.
(2) Style Y is good [ugly] code. For proof see part (3).
(3) I say style Y is good [bad].
The great fun in making such an argument is never shadowed by the futility of trying to convince anyone with it.
Is there an alternative? I am not sure. My attempt in making the case for Spartan programming was based on more or less agreed upon metrics, and then trying to optimize these.
The difficulty with my argument is that it is a universal truth that any one of these metrics, or any other metric for that matter, is that they can be used to ridicule the purpose they are set to serve.
There is a general agreement (I think) that in general, shorter code is better than longer code, all things being equal.
But, if you try to get the shortest possible program for any given task, you would quickly become a competitor of the obfuscated C code contest.
How does one quantify code quality? Is there a more or less objective method to asses quality?
Yossi,
The trouble lies in proving that any particular style is good or bad.
One approach is to back your assertion with some data from a study. This is something that Steve McConnell does repeatedly in Code Complete and personally I find it quite convincing.
For example, on the tricky subject of a good variable name length (Chapter 11.1 in Code Complete 2) he first makes his assertion:
…Names that are too short don’t convey enough meaning. The problem with names like x1 and x2 is that even if you can discover what x is, you won’t know anything about the relationship between x1 and x2. Names that are too long are hard to type and can obscure the visual structure of the program…
…and then backs it by referencing an appropriate study…
Gorla, Benander, and Benander found that the effort required to debug a program was minimised when variables had names that averaged 10 to 16 characters (1990). Programs with names averaging 8 to 20 characters were almost as easy to debug.
re C++ code and making the enumerated switched
This is a classic Make a desert and call it tranquility* neophyte simplification, the kind of code that looks really neat when on display as a snippet, but is a nightmare to maintain in the middle of a piece of real code
Why on earth is it better to break the ontologically significant order in the enumeration to save a bit of scanning the text? Sure you get the unexecuted code up the top, but by having an enumeration in place you get the same movement through every time, you have a geography to the code that is the same every time you encounter the enumeration. It also helps followers-up on the code-base to know what to expect, and to spot egregious errors
moreover - doesn’t that mean that for ALL of the code moved forward the ENTIRE switch gets traversed EVERY time? Why is that better than a lot of breaks avoiding the traversing?
*ob ref Tacitus atque ubi solitudinem faciunt, pacem appellant
I tend to agree with most of the ideas and for the most part have practised something close for years without giving it a special name. When it comes to variable names though, I disagree, I’ve gone the opposite way because I think it improves clarity, avoids errors and allows me to use my IDE as it was designed.
I tend to agree with most of the ideas and for the most part have practised something close for years without giving it a special name. When it comes to variable names though, I disagree, I’ve gone the opposite way because I think it improves clarity, avoids errors and allows me to use my IDE as it was designed.
Hi Jeff,
I read your blog almost everyday, and usually anything you post, has a bunch of links that give more information that what you state in your article.
This one was pretty disappointing, coz more than 50% of the content of this post, is from the source url. I am sorry for calling out on you, but as your reader, I have come to depend on your blog for daily insights into the work I do, and have hence come to expect more quality from you.
(I know my blog sucks, which is why I hard write and no one read it )
Graham: I am of course familiar with statistical methods. I am not sure they can be used here: unlike many other fields of science, experiments are very difficult to conduct, and the interpretation is very subjective. As a reference point, consider application of statistical methods to determine whether a certain protocol is better than another in treating a particular disease. The design of such an experiment is not trivial, and there are companies which make fortune just by doing that. But, the problems in doing a software based experiment are much much worse - and any change to the fundamental variables will invalidate the results: change of programming language, programming environment, debugging task, debugger tool, programmer training, underlying code, etc. It would be nice if we could run experiments of this sort. I am not so sure this is possible - but I will check out the references you provide.
What I am looking for is a method, which obviously exists in other disciplines, of reaching some agreement, without the usual bickering.
Such methods must exist in fields such as law, humanities, and other, non exact, non empirical fields of science.
titrat: I am not sure I fully understand the implications of returning -0. Do you say it may be less efficient? Or do you say it may lead to wrong results on some machines?
- This works in C today, although it is only recently that compilers started allowing variables to be defined in loops.
Hmm… pretty sure this has been in since at least ISO/IEC 9899 (adopted by ISO in 1999) but I don’t have the standard handy to check.
I suppose recently is a relative term. My first contact with C was in 1983. The reference book I still use for the language is copyright 1984. 1999 (and the subsequent adoption in compilers which would have taken a few years) seems quite recent to me.
- No such thing as a private variable in C…
You can still minimise the accessibility of variables e.g. by using static variables.
Heh. Well, yeah. In most other languages this kind of thing (no program-global visibilty for objects not explicitly defined in an interface) comes out of the box. It didn’t even occur to me that this would be considered a feature. But I suppose you could claim this for C if you really want.
It seemed to me this technique was talking about creating what we used to call opaque types (private members for you whippersnappers). C++ can do that with private and protected. With C, no dice. The best you can do is declare the thing a void * and redefine it properly in the .c file (Security through obsurity). That’s generally a lot more work than its worth.
The final refactored sendEmail is a much cleaner example of coding, but are Spartan techniques really the cause of this? Breaking the routine into smaller functions isn’t some new method to achieve readable code. My problem with the code: !OO. Make the email message an object with appropriate properties and a Send method! Oh wait, what language am I thinking in?