Saturday 10 December 2016

Spartan Programming

You all have seen the movie 300 which makes you enough of an expert to understand this post. It would help if you knew some programming as well but it's just an after thought so don't beat yourself for it.
Remember how Spartans are almost anti-fancy in their attire and yet a menacing force of nature? Well it so happens that there is a culture in programming inspired almost from this principle i.e. be as effective as possible without using too many tools. This culture is aptly named Spartan programming.
A programming work can be deemed as being Spartan based on the following criteria:

  1. Horizontal complexity: A program shouldn't be too nested. More nesting there is, the more chances you have of clearing your logic and making it more streamlined.
  2. Vertical complexity: The length of the program should be as short as possible. No point in writing more code when the problem can be solved with something shorter and hence sweeter.
  3. Token count: A token is any entity in a program that have individual meaning be it your variables, literals, keywords or anything that is allowed by the language and is part of your program. It is obvious that reducing token count without loosing meaning can ensure simplicity to your algorithm and hence can be more effective and scale-able.
  4. Character count: Don't be too verbose with your comments and variable names. Be brief and to the point.
  5. Parameters: It is always nice to refactor a solution into subroutines. A good subroutine should have a clear purpose and should be spartan in itself. One of the essential elements of ensuring this would be reducing the passing of implicit data to subroutines as arguments. You don't want to pass things that are available otherwise and can be derived from other parameters.
  6. Variables: Don't overuse a variable in too many places. It is also considered a good practice to declare variables immediately before they are to be used and not all together at the top of your program.
In conclusion, spartan programming encourages frugal use of resources as a programmer. Another important feature is to make your solution robust just like a spartan. A spartan isn't invincible but it certainly should be tough enough.

No comments:

Post a Comment