jon torresdal

  • About
  • Contact

    Refactoring TryParse Into a Value Object

    22. June 2009

    Recently I worked with a colleague on some code using decimal.TryParse() and tried to find a better way of using it. Specifically I had a string value representing a currency amount entered by a user in a web form. The amount needed more than just normal validation, so I needed to do some stuff with it.

    Here’s the code I started with:

    public void SomeMethod(string userEnteredAmount)
    
    {
    
        decimal amount;
    
        if(decimal.TryParse(userEnteredAmount, out amount) {
    
            //Do some stuff with amount
    
        }
    
        else {
    
            //Show message to user about invalid amount
    
        }
    
    }

    I’ve recently found many cases where I find the use of Value Objects to be very applicable. I found this case to be a particularly interesting example. Anyway, I created a Value Object called Amount to abstract away the TryParse stuff. Below is the class I created.

    public class Amount
    
    {
    
        private readonly decimal _value;
    
        private readonly bool _isValid;
    
    
    
        public Amount(string amount)
    
        {
    
            _isValid = decimal.TryParse(amount, out _value);
    
        }
    
    
    
        public decimal Value
    
        {
    
            get { return _value; }
    
        }
    
    
    
        public bool IsValid
    
        {
    
            get { return _isValid; }
    
        }
    
    }
    
    
    

    See how clean it got? At least I think so. The important part though is that it’s usage is so much easier to understand and read:

    var amount = new Amount(userEnteredAmount);
    
    if(amount.IsValid) {
    
        //Do some stuff with amount.Value
    }
    
    else {
    
        //Show message to user about invalid amount
    
    }

    It’s more code, but cleaner. Or did I say that already? :-) And of course it’s reusable other places where amount has a meaning.

    Scott Hanselman and Jeremy D. Miller to NNUG Bergen!

    4. June 2009

    As I’ve previously twitted through @nnugbergen and also through the invites I sent out yesterday, Scott Hanselman and Jeremy D. Miller is coming to NNUG Bergen Monday the 15th of June (Jeremy) and Saturday 20th of June (Scott).

    Jeremy will do two talks. The first being Software Design and Testabillity and the second will be about StoryTeller, his take on integration testing. Scott will do his Deep Tour of .NET 4 talk.

    To sign up or see the full agenda, use the links below:

    Jeremy: http://nnug.no/Avdelinger/Bergen/Moter/NNUG-Bergen—Juni-2009—Jeremy-D-Miller/
    Scott: http://nnug.no/Avdelinger/Bergen/Moter/NNUG-Bergen—Juni-2009—Scott-Hanselman/

    In case you don’t know, all events going through NNUG is free, including these two! I’m already now worried how we’re going to top this next year. Maybe we should just close down NNUG Bergen after 2009 ;-)

  • Recent Posts

    • How ConDep came to life
    • Introducing ConDep
    • Lightning Talk: Why you shouldn’t track bugs
    • How Do We Track Bugs? Check In a Failing Test!
    • Stepping Down from NNUG Bergen, Still Chairman of NNUG National
  • Archives

    • March 2013
    • February 2013
    • November 2012
    • January 2012
    • June 2011
    • May 2011
    • September 2010
    • August 2010
    • June 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
  • Categories

    • .Net
    • ADFS
    • Agile
    • Ajax
    • Architecture
    • Articles
    • ASP.NET
    • ASP.NET-MVC
    • Blogging
    • Books
    • BPEL
    • CleanCode
    • CloudComputing
    • Community
    • ContinuousDelivery
    • ContinuousDeployment
    • CSharp
    • DasBlog
    • Database
    • DDD
    • Deployment
    • DevOps
    • DSL
    • Events
    • ExtremeProgramming
    • Fun
    • Gadgets
    • IIS
    • InfoQ
    • Java
    • Kanban
    • Lean
    • Linq
    • MemoryLeaks
    • Microsoft
    • MVC
    • NDC
    • NNUG
    • Other
    • Patterns
    • Performance
    • Scrum
    • Security
    • Silverlight
    • Software
    • TeamManagement
    • TechEd
    • Testing
    • Tools
    • TvGuide
    • Uncategorized
    • Vista
    • VisualStudio
    • WCF
    • Web
    • WebDeploy
    • WIF
    • Windows
    • WiX
    • Work
    • Workflow
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

Tumblog WordPress Themes by Theme created by Obox