The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Minimalist Coding Style

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Mark Levison

Posts: 877
Nickname: mlevison
Registered: Jan, 2003

Mark Levison an agile software developer who writes Notes from a tool user.
Minimalist Coding Style Posted: Jul 21, 2008 8:59 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Mark Levison.
Original Post: Minimalist Coding Style
Feed Title: Notes from a Tool User
Feed URL: http://feeds.feedburner.com/NotesFromAToolUser
Feed Description: Thoughts about photography, software development, reading, food, wine and the world around us.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Mark Levison
Latest Posts From Notes from a Tool User

Advertisement

image

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away” - Antoine de Saint  Exupéry. If you’ve ever invited me do some pair programming with you, you probably have a good idea what this quote is all about. I often wind up asking questions like:

  • Why do you need this boolean named retVal? Could it be eliminated the use of early return statements?
  • Is the else clause in this if statement necessary? Could it be avoided with a return statement? Or break/continue in a loop?
  • I noticed that this method has parts that are nested five levels of braces deep. Is there anything we can do to reduce that?

Two recent posts Spartan Programming (Jeff Atwood Coding Horror) taken from the Spartan Programming page on the Technion Wiki and Life After If, For and Switch (Scott Hansleman Computerzen), have reminded me of this subject.

Most of the Spartan ideas make sense to:

  • Minimize depth of nesting of control structures
  • Minimize the number of lines occupied – the idea being its easier to make sense of a method if its all on one screen. I would prefer that most methods be < 10 lines long. Some people read this suggestion to mean all blank lines should eliminated. I would disagree preferring to use blank lines like paragraph breaks, they indicate we’re changing gears.
  • Minimize Token and Character Count suggest removing braces etc. – i.e. remove every non-essential character. I think these measures can be harmful and wouldn’t use them.
  • Minimize the number of Parameters to a method and avoid the use of out params.
  • Reduce the number of Variables – a number of excellent techniques are shown. I disagree with only two: the use of ternary operator and encouraging the use of very terse variable names.

My additional rules:

  • Booleans make bad parameters. If you have method that has doStuff(realParam, true, false) – how does someone reading the calling know what true and false mean. At the very minimum use enums with meaningful names.
  • One letter variable names like i,j,k and e come from the early days of Fortran where variables were 1-6 characters long. With so few characters its not surprise that common convention dictated that i,j,k were loop/index variables. We’ve come a long since then – use names that while concise get the point across.
  • Rather than for(int index = 0; index < size(); index++) use Java (or .NET’s) foreach. They save an unnecessary indexing variable.
  • Check arguments at the start of any public or package level method and after that assume they’re right.
  • Minimize the use of instanceof – as your class structure grows these will be hard to maintain. More on this in up coming post.
  • The if/switch statement you don’t write is one you don’t have to test.

So next you invite me to pair program at least you have an idea about some of the questions I will ask.

If you enjoyed this post, subscribe now to get free updates.

7. Case Studies

Credit: this is based on material from "10 ways to screw up with Scrum and XP" by Henrik Kniberg. Personal Experience: Mark Levison.

If you enjoyed this post, subscribe now to get free updates.

Read: Minimalist Coding Style

Topic: Smalltalk Daily 7/18/08: Version History in Store Previous Topic   Next Topic Topic: Caching for Java Applications

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use