CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Patrick Smacchia [MVP C#]


Rules for Spartan Programmers

A month ago, Jeff Atwood (Coding Horror) wrote a blog post about Spartan Programming. The idea is to tend toward minimalism coding style: Minimalism isn't always the right choice, but it's rarely the wrong choice.

 

I figured out that many of Spartan Programming tenets can be expressed as CQL rules:

 

Horizontal complexity. The depth of nesting of control structures.

WARN IF Count > 0 IN SELECT METHODS WHERE ILNestingDepth > 3

 

Vertical complexity. The number of lines or length of code (all routines, and and preferably larger software units such as classes fit within a single screen).

WARN IF Count > 0 IN SELECT METHODS WHERE NbLinesOfCode > 15

 

Parameters. The number of parameters to a routine or a generic structure.

WARN IF Count > 0 IN SELECT METHODS WHERE NbParameters > 4

 

Variables

WARN IF Count > 0 IN SELECT METHODS WHERE NbVariables > 4

 

Conditionals. The number of if and multiple branch switch statements.

WARN IF Count > 0 IN SELECT METHODS WHERE CyclomaticComplexity > 7

 

 

Minimize visibility of identifiers (what I call Optimal Encapsulation).

WARN IF Count > 0 IN SELECT METHODS WHERE CouldBePrivate

This applies also to types and fields and to any non-public visibility level, for example.

WARN IF Count > 0 IN SELECT TYPES WHERE CouldBeInternal 

 

 

Minimize variability of variables. Strive to make fields read-only, methods pure and types immutable (see also Immutable types: understand their benefits and use them).


Immutables fields that could be declared with the readonly keyword. 

WARN IF Count > 0 IN SELECT FIELDS WHERE 
IsImmutable AND !IsInitOnly 

 

Immutable types that could be tagged with a ImmutableAttribute.

WARN IF Count > 0 IN SELECT TYPES WHERE IsImmutable AND
!HasAttribute "OPTIONAL:NDepend.CQL.ImmutableAttribute"

 

Pure methods that could be tagged with a PureAttribute. 

WARN IF Count > 0 IN SELECT METHODS WHERE   
  
!HasAttribute "OPTIONAL:NDepend.CQL.PureAttribute" AND 
  
!ChangesObjectState AND !ChangesTypeState AND
  
NbLinesOfCode > 0

 

 

Use annotations or restrictions whenever possible: Types that could be declared as sealed (see also Rambling on the sealed keyword)

WARN IF Count > 0 IN SELECT TYPES WHERE 
IsClass AND NbChildren == 0 AND !IsSealed


Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 5, 2008 12:23 PM

Damon Wilder Carr said:

Fantastic content... Thanks!

Damon

# August 5, 2008 12:25 PM

Dew Drop - August 6, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 6, 2008 | Alvin Ashcraft's Morning Dew

# August 6, 2008 8:20 AM

Jon Rowett said:

this is a bit like the list of "dos and don'ts for wives" in Marge Simpson's housekeeping magzine - they're all "don'ts".

# August 6, 2008 8:26 AM

Weekly Links #13 | GrantPalin.com said:

Pingback from  Weekly Links #13 | GrantPalin.com

# August 11, 2008 12:04 AM

Kartones Blog said:

For this application review, I've chosen a proof of concept I built last year, a small dungeon generator

# September 27, 2008 5:36 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Patrick Smacchia

Patrick Smacchia is a Visual C# MVP involved in software development for over 15 years. After graduating in mathematics and computer science, he has worked on software in a variety of fields including stock exchange, airline ticket reservation system as well as a satellite base station at Alcatel. He's currently a software consultant and trainer on .NET technologies as well as the lead developer of the tool NDepend which provides numerous metrics and caveats on any compiled .NET application. He is the author of Practical .NET2 and C#2, a .NET book conceived from real world experience with 647 compilable code listings. Check out Devlicio.us!

Our Sponsors