Most efficient way of applying multiple conditional logic

B

booksnore

..eh I was stuck thinking up a subject title for this post for a while..

So I am processing a really big file (scary big). Each record is fixed
length, I need to test conditions on certain fields in the record. At
the moment the most efficient way I've found to process the data is a
series of nested if/else statements so like below. My question is does
anyone know of a better way to process this kind of logic. I can't use a
switch statement because each condition applies to a different field
(substring) of the input record.

if (condition1)
{
reject(line);
}
else
{
if (condition2)
{
reject(line);
}
else
{
process_record(line);
}
}
}

Any thoughts appreciated.

Joe
 
J

Jon Skeet [C# MVP]

booksnore said:
.eh I was stuck thinking up a subject title for this post for a while..

So I am processing a really big file (scary big). Each record is fixed
length, I need to test conditions on certain fields in the record. At
the moment the most efficient way I've found to process the data is a
series of nested if/else statements so like below. My question is does
anyone know of a better way to process this kind of logic. I can't use a
switch statement because each condition applies to a different field
(substring) of the input record.

if (condition1 ||
condition2 ||
...)
{
reject(line);
}
else
{
process_record(line);
}
 
B

Brian Pelton

This might be overkill for what you are doing, but I just happened to
use some similar logic for a program I'm writing and it might help.

This would be best suiting if you have lots of validations....

Define an interface IValidation that exposes a method bool
IsValid(string Line).

Then for each of your validations that you want to run, create a class
that implements IValidation and the IsValid method.

In your file processing piece, create an array or collection of
IValidation objects and do a foreach (IValidation val in
ValidationCollection).

This of course assumes that each IValidation is independent, meaning
that one doesn't depend on another, etc.


Anyways, just though I would share...

--Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top