4
\$\begingroup\$

We assume we have unlimited number of coins and bills. My code works fine but the method optimalDMCA() has a Cyclomatic Complexity of 8.

import java.util.*;
import java.math.*;

class GoodEnough {
   long how2 = 0;
   long about5 = 0;
   long this10 = 0;
}

class Solved {

  static GoodEnough optimalDMCA(long s) {
    long change = s;
    GoodEnough c = new GoodEnough();
    if (change >=10) {
        if (change % 2 != 0 && change % 5 !=0) {
            return dealWithChangeLike31Euros(change);
        }
        else {
            c.this10 = (long) change / 10;
            change = change % c.this10;
        }
    }
    
    if (change <10 && change >=5) {
        if (change % 2 == 0) {
            c.about5 = 0;
            c.how2 = change/2;
        }
        else {
            change = change - 5;
            c.about5 = 1;
        }
    }
    
    else if (change %2 ==0) {
        c.about5 = 0;
        c.how2 = change/2;
    } else {
        return null;
    }
    return c;
  }

  static GoodEnough dealWithChangeLike31Euros(long s) {
    GoodEnough c = new GoodEnough();
    c.this10 = ((long) s / 10) - 1;
    long change = (long) s % (c.this10 * 10);
    if (change > 5) {
        change = change - 5;
        c.about5 = 1;
    }
    if (change % 2 == 0) {
        c.how2 = change / 2;
    }
    else {
        return null;
    }
    return c;
  }
}

Can the implementation of the dealWithChangeLike31Euros() method be merged into the other method?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Contents of this question were edited to remove claimed copyrighted content in a DMCA Takedown Request by CoderPad, Inc. \$\endgroup\$
    – Cesar M
    Commented Mar 2, 2022 at 19:54

1 Answer 1

4
\$\begingroup\$

The only way of making an odd total is with a five euro bill. There is no point ever having more than 1 five euro bill, as you could replace pairs with a ten euro bill. So, you should start off checking for the odd total.

if (change >= 5  &&  change % 2 == 1) {
    c.about5 = 1;
    change -= 5;
}

At this point, you can simply compute the number of ten euro bills, and remaining 2 euro coins.

c.this10 = change / 10;
change = change % 10;

c.how2 = change / 2;

The only thing you have to watch out for is a total change amount of 1 or 3 euros (or negative), which is not possible to make. Handle those as a special case.


Your optimalDMCA() creates a GoodEnough object, and if dealWithChangeLike31Euros() methods is called, it creates its own GoodEnough object, and the first object is abandoned. You shouldn't create the object if it isn't going to be used. Options include passing the change object to the dealWithChangeLike31Euros() method, or delaying the creation.

Multiple return points is sometimes a code smell. optimalDMCA() has 3:

  • return dealWithChangeLike31Euros(change);
  • return null;
  • return c;

If you initialized GoodEnough c = null;, and only created the Change object when needed, you could have just the one return c; at the end. Less returns may reduce the complexity.

\$\endgroup\$
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.