473,658 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1277
Sam Martin <sa*********@ya hoo.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Sam Martin <sa*********@ya hoo.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.co m>
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.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Sam Martin <sa*********@ya hoo.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.co m>
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
12953
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 do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea that the faulty code is written by blackguards bent on foulling the language. The term...
9
1644
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 of developers who need education on validation, validity and all the usual good stuff. In particular it's a problem of triage on the legacy code: what's correctable now, what's going to need serious work, what's going to be a total re-write. ...
9
2967
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"); if(!$fp) {
0
2085
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... (java.io.FileNotFoundException: /home/dedhia/.jftp/jftp.properties (No such file or directory)) Here is my java code in which I get the property file: String propertyFile =...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.