rspec/rules/S5139/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

39 lines
975 B
Plaintext

== Why is this an issue?
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
[source,java]
----
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
[source,java]
----
ResultSetMetaData rsmd = rs.getMetaData(); // Compliant; result of "rs.getMetaData()" is cached
for (int i=1; i<rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getAutoIncrement());
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Cache the result of "java.sql.ResultSet#getMetaData()" out of this loop.
endif::env-github,rspecator-view[]