3

This may be tedious, but I would love to know which is a more efficient way of programming. The result may only be slightly better or slightly worse, but I am still interested in which is 'better'.

Say I want to create an array of String objects based on the amount of columns I received from an SQL query and then go ahead and fill this String[] object with values through the use of a for loop. i.e.

if(rs.next()) {
    String[] topLine = new String[metadata.getColumnCount()];

    for (int i = 0; i < metadata.getColumnCount(); i++) {
        topLine[i] = getColumnValue(rs, metadata.getColumnType(i + 1), i + 1);
    }
    writeNext(topLine);
}

See the use of metadata.getColumnCount() to fetch the amount of columns, only twice. Would it be more appropriate (and faster) to bring this value into it's own variable such that:

int columnCount = metadata.getColumnCount();

and make use of it only twice? Or should I leave the method to call twice? What n times do I have to call metadata.getColumnCount() before it becomes more efficient to use a local variable. Is there even any change?

Edit: By the way, this isn't my code, I picked it up as an example.

7
  • Looks like a getter. My guess is getColumnCount returns the value of a private field in the meta data object rather than doing any strenuous.
    – Nathan
    Commented Jul 30, 2015 at 13:36
  • That's not exactly what I'm asking here. I'm asking whether it's faster to just use the getter twice (thrice.. N'ice) or should I declare in all circumstances above N = 1.
    – insidesin
    Commented Jul 30, 2015 at 13:42
  • Looks like much more than twice. Appears to be twice per record but I would expect the metadata isn't changing as you walk through the records, so it could be done once outside of the loop. So 2n instead of 1.
    – cdkMoose
    Commented Jul 30, 2015 at 13:47
  • Woops, yes. Bad example by me. What if it were physically only twice though? Edit: Woops, it's only twice here as this is only one line. The while loop is redundant or could be replaced with an if.
    – insidesin
    Commented Jul 30, 2015 at 13:51
  • Not only that but if the column count were ever to increase after the construction of the string [] the assignment inside the loop would get an array index out of bounds exception. Essentially, this argues that the count isn't expected to change here.
    – Erik Eidt
    Commented Jul 30, 2015 at 14:10

2 Answers 2

2

When should I instantiate a variable rather than grabbing it through a method?

When the value returned in the method has meaning or will be use past the name of the method, then get the value and assign it to variable with the appropriate name.

If metadata.getColumnCount() is constant during the loop, then you can get it and cache it in a local before the loop.

Either way I wouldn't be concerned about the performance of the get unless you have been able to measure the degradation and it would make a difference to the application. Focus on the clarity and the intent of the code, optimise based on performance measurements. Modern compilers and VM are very good at what they do.

0

It depends on the intent of your code and avoiding repeating yourself.

If the intent is to rely on the return value of a method to be the same in both instances, assign a single variable. This way, another developer does not have to determine whether or not both uses of the method will always return the same value.

In this example, every developer should know your intent is to apply the same value twice:

thisCount = metadata.getColumnCount()
DoSomething(thisCount)
DoSomethingElse(thisCount)

Now, if there is a new parameter to apply to that method, you only need to change in one place:

thisCount = metadata.getColumnCount(skipPrimaryKey = true)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.