1

Is this possible, to suppress checkstyle "Magic number" errors when I try to get specific column from a result set?

To be clear, I have code like this

resultSet.getBigDecimal(1)

or

resultSet.getString(1)

where digit - is the column index in the result set. Can I avoid suppression for that case (not with annotations)?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kostya Kiselev
  • 145
  • 1
  • 6

1 Answers1

1

The ResultSet API offers getters that work with column names, such as getString(String). Then your code would look like this:

resultSet.getBigDecimal("EmployeeID")
resultSet.getString("EmployeeName");

This resolves the magic number problem, and gives a good clue about what it is that you are reading from the table.

From a Checkstyle point of view, suppression would not be conveniently possible, as you would have to annotate every single case.

barfuin
  • 16,865
  • 10
  • 85
  • 132
  • So, as I understand, we can't add some settings in xml config file, only use supression annotations or refactor code? – Kostya Kiselev Feb 08 '18 at 12:23
  • Yes, unfortunately so. (Not counting esoteric solutions such as defining XPath expressions for these cases or something.) But I believe that using the column names is actually quite a nice solution. :-) – barfuin Feb 08 '18 at 12:29