473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Checking validity of a property on set()

hi all,

I'm looking for your opinions on whether to validate and subsequenly throw
exceptions based on the data set() to an object's property or whether you
should do it on say a Save() method.

For example, I have a series of Business Components that have fairly
straight forward properties, and methods to Save().

Should I...

a) Check the validity of each property when it's set and throw an exception
if invalid
or
b) Wait until the Save() method is called for the object, then validate and
throw exceptions where application

My concern is having to put a try block round every bit of code that tries
to set the propery of one of my business objects...

you thoughts....

TIA
Sam Martin
Nov 16 '05 #1
3 1266
Sam Martin <sa*********@yahoo.co.uk> wrote:
I'm looking for your opinions on whether to validate and subsequenly throw
exceptions based on the data set() to an object's property or whether you
should do it on say a Save() method.
It depends on the situation, really. Sometimes you may want to go
through many intermediate steps which may have the object in an invalid
state, but leave it in a valid state eventually. In that kind of
situation, validation on save is a good idea.

In other situations, you really want to make sure that an object is
*always* in a valid state. Without knowing more about your situation,
it's hard to say for sure which line you should take, I'm afraid.
My concern is having to put a try block round every bit of code that tries
to set the propery of one of my business objects...


Don't have a try block round each of them - have a try block nice and
high up. You shouldn't need to use try/catch particularly often.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
thanks for opinion jon

got a good idea of what im doing now.

cheers
sam

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sam Martin <sa*********@yahoo.co.uk> wrote:
I'm looking for your opinions on whether to validate and subsequenly
throw
exceptions based on the data set() to an object's property or whether you
should do it on say a Save() method.


It depends on the situation, really. Sometimes you may want to go
through many intermediate steps which may have the object in an invalid
state, but leave it in a valid state eventually. In that kind of
situation, validation on save is a good idea.

In other situations, you really want to make sure that an object is
*always* in a valid state. Without knowing more about your situation,
it's hard to say for sure which line you should take, I'm afraid.
My concern is having to put a try block round every bit of code that
tries
to set the propery of one of my business objects...


Don't have a try block round each of them - have a try block nice and
high up. You shouldn't need to use try/catch particularly often.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
To add to what Jon said, I also think that this is a situation where
exceptions might be a bad idea. If these are objects that are business (I
use that term loosely) entities, then you might want to have an IsValid
method which will return what is valid and what is not. I don't think that
raising exceptions while setting values in this case is really a good idea,
since you really are returning information that relates to business logic,
not exceptional situations.

However, if this was an operation more like setting the apartment state
of a thread, where there is a direct and immediate effect, then throwing an
exception for something out of range or invalid is justified, IMO.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sam Martin <sa*********@yahoo.co.uk> wrote:
I'm looking for your opinions on whether to validate and subsequenly
throw
exceptions based on the data set() to an object's property or whether you
should do it on say a Save() method.


It depends on the situation, really. Sometimes you may want to go
through many intermediate steps which may have the object in an invalid
state, but leave it in a valid state eventually. In that kind of
situation, validation on save is a good idea.

In other situations, you really want to make sure that an object is
*always* in a valid state. Without knowing more about your situation,
it's hard to say for sure which line you should take, I'm afraid.
My concern is having to put a try block round every bit of code that
tries
to set the propery of one of my business objects...


Don't have a try block round each of them - have a try block nice and
high up. You shouldn't need to use try/catch particularly often.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
9
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r");...
0
by: ndedhia1 | last post by:
I am having trouble loading a property file and keep getting this error when running my shell script that loads and runs my jar files, etc: no property file loaded, using defaults......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.