Wrap around Boolean objects

I have a code that I want to reorganize. I have many methods that take multiple arguments of the same type, for example:

public void foo(String name, String street, boolean b1, boolean b2) { ... } 

etc. Since different objects can only be selected by name, I would like to wrap them in Object (Enums) so that I can use the language system for the language (Java in this case).

 public class Name { private String value; public String getValue() { return value; } // ... } 

Similarly, I can make the call code pass in Objects of a certain type. This would assure that this would not lead to an inadvertent confusion of the order of the method parameters, and therefore would not lead to unexpected runtime behavior :

 foo(new Name("John"), new Street("Broadway"), new B1(true), new B2(false); 

This makes refactoring safer , you can transfer an object through the system as long as you want, the data inside it, the string is always safe. Only when you need it will you get it by calling getValue ().

Now, for objects that wrap strings, it's pretty simple, since there are many state instances.

But what about boolean wrappers? It is either TRUE or FALSE. The implementation just looks, well, a little ridiculous:

 public enum Quanto { YES() { protected boolean isQuanto() { return true; } }, NO() { protected boolean isQuanto() { return false; } }; protected abstract boolean isQuanto(); } 

Even a stranger, I find what the call code looks like:

 public void doStuff(Quanto quanto) { if(quanto.isQuanto()) { // ... } } 

Technically, it doesn't matter, but it's just not. Have you found the β€œbest” ways to deal with this?

EDIT: What also does not satisfy me is the fact that in the above example there are more values ​​than YES and NO , say POSSIBLE ...?!

Thanks!

+4
source share
4 answers

I would wrap booleans in semantic enumerations. In other words:

 public void doStuff(boolean isActive, boolean wrapResult); 

becomes

 public enum State {ACTIVE, INACTIVE}; public enum ResultMode {WRAPPED, UNWRAPPED}; public void doStuff(State state, ResultMode resultsMode); 
+9
source

Basically, what you do is called parameters, so I would suggest one class for each function with elements for each parameter.

The call site will look something like this:

 foo(new foo_arguments().Name("John").Street("Broadway").B1(true).B2(false)); 

And then you will use arguments.Name () and such functions inside.

This has the added benefit of allowing you to give default arguments without a massive number of overloads and allowing you to specify arguments in any order, so this works:

 foo(new foo_arguments().Street("Sesame").Name("Monster")); 

Required Class:

 public class foo_arguments { private string _name = "John Doe"; public foo_arguments Name(string name) { _name = name; return this; } public string Name() { return _name; } private string _street = "Pennsylvania Ave NW"; public foo_arguments Street(string street) { _street = street; return this; } public string Street() { return _street; } private string _b1 = false; public foo_arguments B1(string b1) { _b1 = b1; return this; } public boolean B1() { return _b1; } private string _b2 = true; public foo_arguments B2(string b2) { _b2 = b2; return this; } public boolean B2() { return _b2; } } 

As an aside, in C # you can do it very elegantly with auto-properties and object initializers.

+4
source

Do methods always use the same set of arguments? If they are, perhaps you want to create a data object that stores all the data, and you only provide this object with these functions. Perhaps you can even move functions to an object at a later point. Sometimes methods with many input arguments are a clue that an object exists for all of these arguments.

I would not encapsulate one Boolean in one object. This does not seem right to me.

+2
source

Instead of listing for boolean elements, why not have encapsulation objects for the different values ​​you pass to the method? Thus, the values ​​are on getters and setters and have the necessary information:

 public class Encapsulator { private boolean isSelected; private boolean isAwake; public boolean isAwake() { return isAwake; } public void setIsAwake(boolean isAwake) { this.isAwake = isAwake; } public boolean isSelected() { return isSelected; } public void setIsSelected(boolean isSelected) { this.isSelected = isSelected; } } 

Thus, when you access the data, it is very clear that a certain element in the object does this or that. It also reduces the parameter set for methods, which is the smell of code according to Martin Fowler.

+1
source

All Articles