lots of IF or elses

  • Thread starter Thread starter J-T
  • Start date Start date
J

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
 
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
 
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
 
Hi,

J-T said:
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;
 
Hi,
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.
 
clintonG said:
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.
 
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 said:
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.
 
jeremiah johnson said:
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.
 
Instead of empty proclamations do you think it would be useful if it was
explained why switch is alleged to be evil?

<%= Clinton Gallagher
 
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
 
Back
Top