I am writing a small simulation program where I have a void move(int n)
function and a void move()
function. As their names suggest, move(int n)
makes n
moves at a time whereas move()
only makes one move.
Now I have two ways of implementing them on mind. Approach 1 is:
public void move()
{
// make one move
}
public void move(int n)
{
for (int i=0;i<n;i++)
{
move();
}
}
And approach 2:
public void move(int n)
{
for (int i=0;i<n;i++)
{
// make one move
}
}
public void move()
{
move(1);
}
Apparently when the number of moves is large, approach 2 gives time efficiency advantage as we do not make a separate functional call every time we make a move. However, since now the implementation of each move is implemented in a for loop
, changing the implementation later on (e.g. fetching metadata or analytical data from each move) seems not to have good modularity. And in cases of making multiple single moves one at a time, approach 2 requires extra function call and extra loop setup.
Assume we do not know about particular future use cases, how should I decide which approach to take?