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 imagehelp@codebetter.com
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
{
    get
    {
        this.total = property1 + property2;
        return this.total;
    }
}

 

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

[Advertisement]

Comments

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.

[)amien

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

@Damien,

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

this.total = property1 + property2;

  return this.total;

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

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

@Partial,

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

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)