473,405 Members | 2,294 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,405 software developers and data experts.

lots of IF or elses

J-T
I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot
Nov 23 '05 #1
17 3244
In the case you describe, probably not. It may be ugly, but if it works, I
wouldn't worry about it! Provided you've used a good if - elseif etc. logic
pattern.

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"J-T" wrote:
I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot

Nov 23 '05 #2
The switch statement may be perfect for this task.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
"J-T" <J-*@nospam.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot

Nov 23 '05 #3
You can write a function to do all the validations.
Best way you can learn the language is to read a few books about it.

Cheers,
Mark

"J-T" wrote:
I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot of
IF and elses statement which has made my code so ugly.Is there a better way
to do instead of sequential IF and elses?

Thanks a lot

Nov 23 '05 #4
Hi,

J-T wrote:
I ended up writing a lot of IF and elses statement which has made my code
so ugly.Is there a better way to do instead of sequential IF and elses?


You have some good responses already and I just want to add a mention a
favorite C language family construct of mine, which often helps in such
situations -- the conditional expression.

Let's say we are doing something like this:

if (value != 0)
Foo = new Bar(value);
else
Foo = null;

Using conditional expression, you can write the same as:

Foo = (value != 0) ? new Bar(value) : null;
--
Chris Priede
Nov 24 '05 #5
J-T

Thanks a lot guys
Nov 24 '05 #6
GONG!

Best way to learn a language is to use it, break something and then go read
the book to figure out what went wrong :-)

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
"Mark" <Ma**@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
You can write a function to do all the validations.
Best way you can learn the language is to read a few books about it.

Cheers,
Mark

"J-T" wrote:
I have a class which it has 10 properties, and each property needs to be
validated against 5 different validation rules.I ended up writing a lot
of
IF and elses statement which has made my code so ugly.Is there a better
way
to do instead of sequential IF and elses?

Thanks a lot

Nov 24 '05 #7
The correct term is "tertiary expression" because the expression supports
three operands. For more information search: teriary expression c#

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/


"Chris Priede" <pr****@panix.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
Hi,

J-T wrote:
I ended up writing a lot of IF and elses statement which has made my code
so ugly.Is there a better way to do instead of sequential IF and elses?


You have some good responses already and I just want to add a mention a
favorite C language family construct of mine, which often helps in such
situations -- the conditional expression.

Let's say we are doing something like this:

if (value != 0)
Foo = new Bar(value);
else
Foo = null;

Using conditional expression, you can write the same as:

Foo = (value != 0) ? new Bar(value) : null;
--
Chris Priede

Nov 24 '05 #8
Hi,

clintonG wrote:
The correct term is "tertiary expression" because the expression
supports three operands. For more information search: teriary
expression c#


While I appreciate your helpful contribution very much, the fact remains
that "conditional expression" alone gets many more relevant Google hits than
"tertiary expression" -- although I didn't try to limit the search to C#.
Perhaps you meant "ternary"?

In any case, you are most certainly welcome to call it what you want, while
I shall continue to call it what Brian Kernighan called it in 1974.

--
Chris Priede

Nov 24 '05 #9
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote:
The correct term is "tertiary expression" because the expression supports
three operands. For more information search: teriary expression c#


No, the conditional operator is *an example* of a tertiary operator.
"Conditional operator" is the terminology used by the C# language
specification though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 24 '05 #10
Chris Priede <pr****@panix.com> wrote:

<snip>
In any case, you are most certainly welcome to call it what you want, while
I shall continue to call it what Brian Kernighan called it in 1974.


To be honest, what Brian Kernighan called it for C is almost
irrelevant, even though it happens to be the same here. Given that
we're discussing C#, the correct source for definitions is surely the
C# language specification - which also uses "conditional operator".

If you start using C or C++ specifications as the correct place to find
definitions, you end up with difficulties in various terms...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 24 '05 #11
Well this has been an interesting learning experience.

<%= Clinton Gallagher

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chris Priede <pr****@panix.com> wrote:

<snip>
In any case, you are most certainly welcome to call it what you want,
while
I shall continue to call it what Brian Kernighan called it in 1974.


To be honest, what Brian Kernighan called it for C is almost
irrelevant, even though it happens to be the same here. Given that
we're discussing C#, the correct source for definitions is surely the
C# language specification - which also uses "conditional operator".

If you start using C or C++ specifications as the correct place to find
definitions, you end up with difficulties in various terms...

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

Nov 24 '05 #12
switch is evil, imo.

clintonG wrote:
The switch statement may be perfect for this task.

Nov 25 '05 #13

Jon wrote:
clintonG <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote:
The correct term is "tertiary expression" because the expression supports
three operands. For more information search: teriary expression c#


No, the conditional operator is *an example* of a tertiary operator.
"Conditional operator" is the terminology used by the C# language
specification though.


"ternary" is the correct term, meaning "made up of three parts".
"tertiary" means "third". So, the conditional operator is an example of
a ternary operator, although so far as I know it's the *only* example
in C#, so a lot of people use the terms interchangeably.

Nov 25 '05 #14
jeremiah johnson <na*******@gmail.com> wrote:
switch is evil, imo.


For any reason? Like many things, switch can sometimes be abused -
particularly when polymorphism would achieve the same result in a more
OO way. However, it's certainly not *always* evil.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 25 '05 #15
Instead of empty proclamations do you think it would be useful if it was
explained why switch is alleged to be evil?

<%= Clinton Gallagher

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
jeremiah johnson <na*******@gmail.com> wrote:
switch is evil, imo.


For any reason? Like many things, switch can sometimes be abused -
particularly when polymorphism would achieve the same result in a more
OO way. However, it's certainly not *always* evil.

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

Nov 25 '05 #16
Clinton... I believe you are responding to the wrong poster. It was not
Jon that
said "a switch is evil". I am not even sure if switch is evil in this
situation. But it is
wise to avoid switching on a TYPE, if the same problem can be solved
with
polymorphism. Switching on type is "brittle, error-prone, unsafe..".Herb
Sutter
and Andre Alexandrescu.

Here is the actual Coding Standard:

#90 Avoid Type Switching; prefer polymorphism.

The practical explanation is that switching on type is not very
extensible and
difficult to maintain. Instead, you can use polymorphism to
automagically
provide new behaviours without breaking client code. So you program to a
virtual base type or Interface, rather than switching at compile time on
a set of
concrete classes.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '05 #17
That's a good enough lead for me. Thanks Jeff.

<%= Clinton Gallagher

"Jeff Louie" <je********@yahoo.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
Clinton... I believe you are responding to the wrong poster. It was not
Jon that
said "a switch is evil". I am not even sure if switch is evil in this
situation. But it is
wise to avoid switching on a TYPE, if the same problem can be solved
with
polymorphism. Switching on type is "brittle, error-prone, unsafe..".Herb
Sutter
and Andre Alexandrescu.

Here is the actual Coding Standard:

#90 Avoid Type Switching; prefer polymorphism.

The practical explanation is that switching on type is not very
extensible and
difficult to maintain. Instead, you can use polymorphism to
automagically
provide new behaviours without breaking client code. So you program to a
virtual base type or Interface, rather than switching at compile time on
a set of
concrete classes.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Nov 26 '05 #18

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

Similar topics

4
by: Shenron | last post by:
Hello, I'm trying to translate my website in some languages, therefore I include a file which contains all words. This is something like that: $hello_english="Hello"; in lang_en.php...
12
by: Paul Moore | last post by:
One of the things I really dislike about Unittest (compared, say, to a number of adhoc testing tricks I've used in the past, and to Perl's "standard" testing framework) is that the...
4
by: Thomas Rast | last post by:
Hello everyone My scenario is somewhat involved, so if you're in a hurry, here's an abstract: I have a daemon that needs about 80MB of RAM to build its internal data structures, but can pack...
10
by: AlexS | last post by:
Hi, I wonder if anybody can comment if what I see is normal in FW 1.1 and how to avoid this. I have .Net assembly, which creates literally thousands of temporary strings and other objects...
2
by: Darryl Kerkeslager | last post by:
As the subject above hopefully makes clear, I want to do several reports, "with lots of fields not otherwise in database". These reports also have variable-length text. I have defined the...
1
by: | last post by:
Im working in some hydraulics calcs.(using lots of loops) that produces lots of data, where can I send this data to be easy readable. I tried with rictextboxes but it only accepts one line. I was...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
6
by: Dave Stallard | last post by:
So, I'm looking at some code that does 770K malloc calls in a row, of varying size, paired with corresponding freads from a binary file to initialize. In total, about 58 MB of data is allocated...
1
by: hockeymtl | last post by:
Hy all, ( sql server 2000 ) I have trouble with my indexes in my database after I inserted lots of data in some user tables ( 10000 + insert ). I need to pinpoint where to look to solve my problem....
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.