Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spartan Programming

I really enjoyed Jeff's post on Spartan Programming. I agree that code like that is a joy to read. Unfortunately, I'm not so sure it would necessarily be a joy to work with.

For years I have read about and adhered to the "one-expression-per-line" practice. I have fought the good fight and held my ground when many programming books countered this advice with example code like:

while (bytes = read(...))
{
   ...
}

while (GetMessage(...))
{
   ...
}

Recently, I've advocated one expression per line for more practical reasons - debugging and production support. Getting a log file from production that claims a NullPointer exception at "line 65" which reads:

ObjectA a = getTheUser(session.getState().getAccount().getAccountNumber());

is frustrating and entirely avoidable. Short of grabbing an expert with the code that can choose the "most likely" object that was null ... this is a real practical pain.

One expression per line also helps out quite a bit while stepping through code. I practice this with the assumption that most modern compilers can optimize away all the superfluous temp objects I've just created ...

I try to be neat - but cluttering my code with explicit objects sure feels laborious at times. It does not generally make the code easier to browse - but it really has come in handy when tracing things down in production or stepping through my or someone else's code.

What style do you advocate and can you rationalize it in a practical sense?

like image 758
Luther Baker Avatar asked Sep 09 '08 02:09

Luther Baker


People also ask

What is spartan programming?

Spartan is a project to get affordable robots into the hands of students and teach programming concepts from day one in middle school through the university level. At the heart of the Spartan is the Core Controller with 26 motor, servo, and sensor ports allowing programs to interact with the robot's environment.

What is the Holy Grail of programming?

A language that makes it easy to write code that does what you want and hard to accidentally write code that doesn't do what you intend. A language that makes it easy to manage complexity.

What programming language does military use?

The Ada programming language is a language based on Ada. It has been widely used in the military and the US Department of Defense for decades. It has many advantages, including being highly customizable and enabling multiple users. It has object-oriented features and supports multi-user programs.


2 Answers

In The Pragmatic Programmer Hunt and Thomas talk about a study they term the Law of Demeter and it focuses on the coupling of functions to modules other than there own. By allowing a function to never reach a 3rd level in it's coupling you significantly reduce the number of errors and increase the maintainability of the code.

So:

ObjectA a = getTheUser(session.getState().getAccount().getAccountNumber());

Is close to a felony because we are 4 objects down the rat hole. That means to change something in one of those objects I have to know that you called this whole stack right here in this very method. What a pain.

Better:

Account.getUser();

Note this runs counter to the expressive forms of programming that are now really popular with mocking software. The trade off there is that you have a tightly coupled interface anyway, and the expressive syntax just makes it easier to use.

like image 151
Dan Blair Avatar answered Sep 28 '22 06:09

Dan Blair


I think the ideal solution is to find a balance between the extremes. There is no way to write a rule that will fit in all situations; it comes with experience. Declaring each intermediate variable on its own line will make reading the code more difficult, which will also contribute to the difficulty in maintenance. By the same token, debugging is much more difficult if you inline the intermediate values.

The 'sweet spot' is somewhere in the middle.

like image 36
pkaeding Avatar answered Sep 28 '22 05:09

pkaeding