Click here to monitor SSC

Alex Davies

Software Engineer - Red Gate Software @alexdcode

So if you're not going to use design patterns?

Published Friday, March 20, 2009 12:16 PM

I realised my last post was a bit negative, so I thought I'd do a quick one with my 2c about what you should do instead of following patterns. There obviously needs to be a way by which you can judge how good code is, a "design principle" if you like.

In my world, there is only one design principle, from which all others can be derived:

Encapsulation

My definition of encapsulation is minimising the amount of code that has to know about a particular concept. "Concept", here, is a very broad term, which can mean anything from
  • a single rule about the behaviour of a program (some might call a piece of business logic)
  • the existence of another component of the program, or a library
  • the use of particular language features (threading is a good example here, I'll blog about how one day)
  • a use case of the program
I'd say that code "knows about" a concept if it could ever interact with its behaviour. So if two methods in a class obviously use a concept, the rest of the class "knows about" it, because one day they might interact with it. Badly designed classes can leak interaction possibility, obviously we need to try to avoid that.

Optimising a single measure, that sounds easy, right? Absolutely not! Like a good game of whack-a-mole, reducing the scope of one concept tends to increase the scope of another, and only finger-in-the-air style judgement can get you anywhere near the best overall.

Like I say, all other design principles stem from encapsulation:
  • Concise code reduces the amount of code the concepts in it are spread over (over-architecting can often reduce encapsultation in this way)
  • Loosely coupled code splits concepts so less of them have to be in both places
  • Repeated code obviously increases the number of places a concept is known about
I'm sure there are more. Can you think of any principles you follow, but don't come under encapsulation?

Comments

No Comments
You need to sign in to comment on this blog
Latest articles
Backups, What Are They Good For?
 We've heard the confessional story from Pixar that Toy Story 2 was almost lost due to a bad backup, but... Read more...

C# Async: What is it, and how does it work?
 The biggest new feature in C#5 is Async, and its associated Await (contextual) keyword. Anybody who is... Read more...

Handling Deadlocks in SQL Server
 In this excerpt from his book Troubleshooting SQL Server: A Guide for the Accidental DBA, Jonathan... Read more...

SQL VIEW Basics
 SQL Views are essential for the database developer. However, it is common to see them misued, or... Read more...

The PoSh DBA: Grown-Up PowerShell Functions
 Laerte goes step-by-step through the process of tidying up and making more reusable an untidy... Read more...