Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Simple Principle, Big Dividends

In his book Domain Driven Design Eric Evans shows the value in keeping functions which are 'Side-Effect-Free' from commands which alter some state in the program. Prior to understanding this, it was common for me to have some method like so:

public double Total
    { = property1 + property2;


As lame as this was, it worked :(

The problem with this was I was producing state changes to my objects when all I wanted was some calculated value based on their state. Changing my code to provide functions which simply performed the calculation without changing state helped me today with a new requirement that was slipped in.

The Subject Matter Expert (SME) decided to wait until a great deal of coding was done to bring up that different rounding rules should be applied to the calculations of certain objects to determine Pass/Fail status on their results. However, there are default rounding requirements that should still be used to present to the user...even though the specs are potentially considering a different value altogether.

Since the functions on my objects do this (waaaay simplified):

public double CalcTotal()
   return property1 + property2;

I could safely consume my objects however the new specs required without worrying about any unintended side-effects on other members of the host object. This also avoided having forces which have nothing to do with my aggregate root from getting implicitly introduced; ie, introducing some kind of Calculation that rounds with different precisions. Keeping functions side-effect-free places the responsibility of the calculation's relevance on the client, not the target.

As basic as this is, strict adherence to this principle across many objects which perform complex calculations allowed me to slip in the solution to my  SME's oversight in a matter of hours and with confidence that my objects are always in a valid state.

Posted 02-19-2008 2:58 PM by Michael Nichols



Damien Guard wrote re: Simple Principle, Big Dividends
on 02-19-2008 8:18 PM

Sometimes you need to cache/store the value such as lazy-loading properties or collections.


Mike wrote re: Simple Principle, Big Dividends
on 02-19-2008 11:39 PM


There are times when changing state while performing an operation is necessary (ie, reducing roundtrips) but even so I can usually pull out operations into side-effect-free functions and call them from within the command method that requires the state change.

PartialClass wrote re: Simple Principle, Big Dividends
on 02-21-2008 2:51 AM

i never undertood it

why didnt u write

return property1 + property2;

instread of this = property1 + property2;


or u needed a topic to write the post ? :)

Mike wrote re: Simple Principle, Big Dividends
on 02-22-2008 4:32 PM


If you had read the post, you would have seen it is about mixing functions with commands and how avoiding that can provide greater reuse. Not about how to write a function.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)