Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Should validation code exist in set methods or in a validate method

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Should validation code exist in set methods or in a validate method

    I was wondering whether I need to validate data passed to a set method, or whether I should keep all the validation checks in a single method. In other words is it dangerous to set class variables without checking them, providing you do check them before you use them?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Should validation code exist in set methods or in a validate method

    validate data passed to a set method
    That'd be my choice. Then you could throw an invalid data exception at the source of bad data.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Should validation code exist in set methods or in a validate method

    I guess it make sense, I've been told any public method should check it's data so I assume it makes sense. I'll have to play with it a bit. Thanks for the reply!

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Should validation code exist in set methods or in a validate method

    The purist approach to this is to start every method with a precondition to check everything is OK to continue, and finish each method with a postcondition to check everything is OK to return. Some languages (e.g. Eiffel) have this approach built-in. In Java you can do this manually or use AOP (Aspect Oriented Programming), and the checks can range from just ensuring a parameter isn't null, to doing a full inspection of the class state before continuing.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Should validation code exist in set methods or in a validate method

    Typically you can check the value in the setter method and if it is invalid throw an IllegalArgumentException or IllegalStateException, depending upon the condition. These thrown exceptions should be well documented for the function so those calling know why these are thrown when they are thrown.
    If the code is written for a project which is not expected to be used or visible by other groups, then you could alternatively use assertions during debugging tests.
    Last edited by copeg; May 27th, 2010 at 08:39 AM.

Similar Threads

  1. java validate help??
    By zyspt in forum Java Theory & Questions
    Replies: 1
    Last Post: April 16th, 2010, 02:48 AM
  2. org.apache.derby does not exist?
    By disclaimer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 30th, 2010, 04:58 PM
  3. How to handle quadratic roots that don't exist?
    By kairojya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2009, 12:21 PM
  4. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  5. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM