samedi 30 mars 2019

reduction of parameter list causing duplicate code

I'm working on an ATM machine project and I frequently need to pass bill amounts as parameters to functions. There are many functions that look similar to

depositCash(int fives, int tens, int twenties, int fifties){...}

I know that it's bad practice to have a function with too many parameters such as 4. So I tried to bundle these parameters in a new class billBundle and pass that as a parameter instead. But then I ran into a new problem of repeatedly having to write duplicate code like the following:

Billbundle billBundle = new BillBundle();
billBundle.setFives(fives);
billBundle.setTens(tens);
billBundle.setTwenties(twenties);
billBundle.setFifties(fifties);
depositCash(billBundle);

And if i just passed all the bills to bill bundle then that would be doing exactly what I was trying to avoid in the first place. How should I handle this? Thanks.

Aucun commentaire:

Enregistrer un commentaire