Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is a good practice to refer maxQ.length ?

 
Poobhathy Kannan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is a good practice to refer maxQ.length ? see the bellow code blog.
1.directly calling maxQ.length
2.set it to a reference then use it..like
int length = maxQ.length;


code blog:

int[] maxQ = getQuantities();
.............
.............

compositePrice.setQty(maxQ.length);
..............
..............
if (press == PrintConstants.MIXED && pressCurrentIteration > 1)
{
quantityThreshold = maxQ.length;
}
for (int i = 0; i < maxQ.length; i++)
{
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the length of maxQ is going to be used in more than one place in your method, then I would obtain its value once and use it whenever necessary:

I would always use the final modifier as it helps in understanding that the length variable will not change.

Incidentally, if the length of maxQ will only be used in a loop, I would do this:

I just think that it's a little clearer and may even be a bit more efficient.
 
Poobhathy Kannan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chung-Wee

I have a small question on the part that you explained for loop.
What will happen if that Loop iterates 1 million time?
Because each and every time, it needs to access maxQ to get its length.

In that case it would be more efficient if I refer from a variable
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic