1
\$\begingroup\$

I'm looking at a class diagram and it shows a method like this:

SavePrintOptions(bool,bool,bool,bool,bool,bool,bool,bool,bool);

where each of the parameters match up with save options (checkboxes) in the UI. How could I refactor this signature to reduce the number of parameters?

asked Jun 14, 2013 at 19:14
\$\endgroup\$
2
  • 5
    \$\begingroup\$ As the answer to this question suggests, collect all of the parameters into a class, and change the method to accept an object of that class. \$\endgroup\$ Commented Jun 14, 2013 at 19:22
  • \$\begingroup\$ It's also possible that a large number of parameters to a method indicates the code is smelling. Extract and refactor to more methods, possibly even more classes, to embody SRP (Single Responsibility Principle). \$\endgroup\$ Commented Jun 14, 2013 at 21:05

3 Answers 3

6
\$\begingroup\$

That is a real code smell and should be refactored. How should any mortal soul unterstand, what this code does?

Why not using a simple object?

public class{
 public bool Whateveryouwant { get; set; }
 public bool Whateveryouwantelse { get; set; }
}

The advantage:

1) your parameter shrunk to just one

2) if you name the properties (better than above) anybody else knows, what options were chosen.

answered Jun 14, 2013 at 22:04
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You may also wish to use enumerations rather than boolean values. An enum with 2 possibilities is a lot clearer and easier to understand than simply "true/false". \$\endgroup\$ Commented Jun 15, 2013 at 9:53
  • 1
    \$\begingroup\$ Or blog.barvinograd.com/2012/06/working-with-bitfields-in-c \$\endgroup\$ Commented Jun 15, 2013 at 10:01
1
\$\begingroup\$

I would create additional PrintOptions class with immutable fields having meaningful names, it could be easily serialized, passed as method parameter etc.

You also can have several different option sets so it easy to manipulate with several options at once not just one by one if it's required, for example you can have DefaultOptions instance.

public class PrintOptions {
 public readonly bool option1;
 public readonly bool option2;
 public readonly bool optionN;
 public PrintOptions(bool option1, bool option2, bool optionN) {
 this.option1 = option1;
 this.option2 = option2;
 this.optionN = optionN;
 }
}
public class SomeClass {
 public SomeClass(PrintOptions printOptions) {
 PrintOptions = printOptions;
 }
 public PrintOptions PrintOptions {get; set;}
 public void Print() {
 Print(this.PrintOptions);
 }
 public void Print(PrintOptions printOptions) {
 Print(printOptions);
 }
}

So print options is decoupled from class that do some work using this options. PrintOptions could be considered as dependency or passed as an argument to Print method, depending on your requirements

answered Jun 16, 2013 at 20:07
\$\endgroup\$
1
\$\begingroup\$

Looks like there are a lot of hidden work is going inside class that is not responsible for saving print options.

It might be even possible that this method doesn't use all of the parameters. I also saw code, where private instance fields were passed into method instead of using them inside calling method:

public Work()
{
 ...
 SaveOptions(_instanceField, whatever)
}
private void SaveOptions(bool option, string parameter)

I'd rather move this parameters as fields into separate class. Then give meaningfull names and provide default values for fields. After all you will have class with single responsibility and method without ANY parameters

var printOptions = new PrintOptions(bool, bool);
printOptions.Orientation = Orientation.Vertical;
printOptions.BlackAndWhite = true;
printOptions.Save();
answered Jun 27, 2013 at 10:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.