I am wondering what type of alternatives are best to use in this situation.
if (method1(parameters)
method2(parameters);
if (method1(differentParameters)
method2(differentParameters;
//and so on.
The code is for an othello game implementation, the first method checks if there are any opponent pieces that can be fliped, the inner method flips the pieces. We check all 8 directions to and flip pieces accordingly.
The code works, but I am searching for alternatives to refactor it.
CodePudding user response:
If seems to me your method1 is a Check
and your method2 might be called Flip
.
It seems to me you issue can be easily encapsulated like this:
CheckAndFlip(params) {
if (Check(params))
Flip(Params)
}
And then call
CheckAndFlip(params);
CheckAndFlip(otherParams);
...
which looks a lot better for me.
CodePudding user response:
If your method1(param) checks the variable param is you can do something like:
Object o = method1(param);
switch (o)
case o == 1:
method2(o)
case o == 2:
method2(o)
or something like that