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?

Advertisements

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 :

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.

Leave a ReplyCancel reply