Design issues

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello ,
I have a function which gets 6 parameters and needs to check every
parameter (existence of it and it value).
What is the best design solution for it?
 
Hello csharpula,

cc> I have a function which gets 6 parameters and needs to check every
cc> parameter (existence of it and it value).
cc> What is the best design solution for it?

Check the public methods.
What to use it up to you. You can make this check manually, or use some kind
of IDE addins like CodeRush (afaik it inserts checks for null for a new created
method)

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello csharpula,

There are different ways this could be done:

1) You could create a Guard utitlity class where you perform the actual validation
of the arguments but in the code you still need to call the correct methods
2) You could use validation attributes and then create a helper method that
can validate the parameters based, this depends heavility on reflection
3) You could use the Spring.NET framework which has the concept of @Before
advices which could be used to validate the parameters

Basically I would just create a Guard.cs with static methods like RequiredArgument(),
RequireNotNull() etc and in the method I would call these static method to
verify the parameters. It also depends on what you are going to do with these
parameters, if they are used to initialize the state of an object than the
responsibility of the validation should be for the object itself.

Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net
 
I have the following situation :
Got a console application (executable) with 6 options such as present
files by date,name,path..
In main i send those arguments to some class and then i need to check
the combinations of them-i can send 1 param,can send any 2,any 3
params....
I need to check which ones exist and to present files which answer to
those criterias. how can i do it without the if(a &b) if(a&b&c)...?
Thanks
 
Hi,

You could use Enum flags and a utility method to simplify the processing
code, but you're not going to be able to avoid "if (a & b)" somewhere:
(This code makes me want your "class enums", Jon :)

[FlagsAttribute]
enum Display { None = 0, Date = 1, Name = 2, Path = 4 }

class Program
{
static void Main(string[] args)
{
Display display = ParseDisplayFlags(args);

if (display == Display.None)
display = Display.Path; // choose a default

Process(display);
}

static Display ParseDisplayFlags(string[] args)
{
Display display = Display.None;

foreach (string arg in args)
{
switch (arg.ToLower())
{
case @"\p":
case "/p":
case "-p":
display |= Display.Path;
break;
case @"\d":
case "/d":
case "-d":
display |= Display.Date;
break;
case @"\n":
case "/n":
case "-n":
display |= Display.Name;
break;
}
}

return display;
}

static bool IsSubset(Display value, Display set)
{
return (set & value) == value;
}

static void Process(Display display)
{
// make sure you list these top-down from largest subset to smallest
subset:

if (IsSubset(Display.Name | Display.Date | Display.Path, display))
{
Console.WriteLine("Display Name, Date and Path");
}
else if (IsSubset(Display.Date | Display.Path, display))
{
Console.WriteLine("Display Date and Path");
}
else if (IsSubset(Display.Path, display))
{
Console.WriteLine("Display only Path");
}
// etc.
}
}
 
csharpula said:
I have the following situation :
Got a console application (executable) with 6 options such as present
files by date,name,path..
In main i send those arguments to some class and then i need to check
the combinations of them-i can send 1 param,can send any 2,any 3
params....
I need to check which ones exist and to present files which answer to
those criterias. how can i do it without the if(a &b) if(a&b&c)...?

In my opinion, the first thing you need is a command-line argument
parser. I built one myself, but I happen to know that there are at
least three good ones offered for public consumption at gotdotnet

http://www.gotdotnet.com/

A command-line argument parser gives you a framework for offering
flexible command-line arguments to your caller. For example, my parser
allows for positional arguments and named arguments, including mixed
variants, so I can call my program like this:

myprog.exe input.txt /process=summary output.txt /unitofmeasure=lf

and the parser will sort out that I passed two positional arguments
(input.txt and output.txt, 1 and 2, respectively) and supplied the
arguments "process" and "unitofmeasure". It allows me to set default
values for missing arguments, etc, and allows the user to re-order
named arguments without my program having to care.

So, once you're past that point, you now have, in your main program,
some arguments that were supplied and some that were not. I claim that
your main program should then sort out what's what and call one of
several overloads offered by the method or methods that it calls. So,
if the user can pass one, two, or three arguments, and they mean
different things, then you could write three method overloads:

MyMethod(string something, int anotherthing, double yetanotherthing) {
.... }

MyMethod(string something, double yetanotherthing) { ... }

MyMethod(string something) { ... }

and your main program figures out what to pass based on what the user
specified on the command line.

This keeps all of your argument processing in one place: in your main
program, and once you're past that point everything is much tighter.
 

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

Back
Top