Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a function, that's invoked 12 times with different arguments, is it okay to have in code 12 function calls in a row?

I have a function, that’s invoked 12 times with different arguments, is it okay to have in code 12 function calls in a row? That function draws cards from deck to players hands in my card game.

    drawCard(userHand);
    drawCard(userHand);
    drawCard(bot1Hand);
    drawCard(bot1Hand);
    drawCard(bot2Hand);
    drawCard(bot2Hand);
    drawCard(bot3Hand);
    drawCard(bot3Hand);
    drawCard(bot4Hand);
    drawCard(bot4Hand);
    drawCard(bot5Hand);
    drawCard(bot5Hand);
    drawCard(dealerHand);
    drawCard(dealerHand);
    drawCard(dealerHand);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yes, there is nothing inherently wrong with this.

Option 1:

If you would like to make your code a little more readable, you could add all the hands into a list and loop through the list evoking the function on each element.

let hands = [userHand, bot1Hand, bot2Hand, ...];

for (const hand of hands) {
    drawCard(hand)
}

Option 2:

I see that you are calling the function twice for each. So, you could run the code in the drawCard() function twice using a loop in order to half the amount of function calls.

Option 3:

You could add another parameter to drawCard() which has the number of cards you would like to draw then return a list of the drawn cards.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading