i writing small simulation program have void move(int n)
function , void move()
function. names suggest, move(int n)
makes n
moves @ time whereas move()
makes 1 move.
now have 2 ways of implementing them on mind. approach 1 is:
public void move() { // make 1 move } public void move(int n) { (int i=0;i<n;i++) { move(); } }
and approach 2:
public void move(int n) { (int i=0;i<n;i++) { // make 1 move } } public void move() { move(1); }
apparently when number of moves large, approach 2 gives time efficiency advantage not make separate functional call every time make move. however, since implementation of each move implemented in for loop
, changing implementation later on (e.g. fetching metadata or analytical data each move) seems not have modularity. , in cases of making multiple single moves 1 @ time, approach 2 requires function call , loop setup.
assume not know particular future use cases, how should decide approach take?
realistically, jvm should make them pretty equivalent (with inlining mostly, see this answer); should instead focus on readable code, makes first 1 preferable. if matters, suggest trying profile code random use cases (e.g. single moves, large number of moves, mixed); you'll have make qualitative decisions view likely, , keep in mind implementation dependent if you're writing code ported.