rspec/rules/S5139/java/rule.adoc

20 lines
692 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Some implementations of ``++java.sql.ResultSet#getMetaData()++`` suffer from performance issue and should not be called in a loop. Instead, multiple calls in a row should be replaced by a single cached call.
== Noncompliant Code Example
----
ResultSetMetaData rsmd = rs.getMetaData();
for (int i=1; i<rsmd.getColumnCount(); i++) {
System.out.println(rs.getMetaData().getAutoIncrement()); // Noncompliant; "rs.getMetaData()" will be called for each columns
}
----
== Compliant Solution
----
ResultSetMetaData rsmd = rs.getMetaData(); // Compliant; result of "rs.getMetaData()" is cached
for (int i=1; i<rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getAutoIncrement());
}
----