33

This is one of the rules from Googles static analyser CodePro AnalytiX:

Summary

Arrays should not be statically initialized by an array initializer.

Description

This audit rule checks for array variables that are initialized (either in the initializer or in an assignment statement) using an array initializer.

Example

The following array declaration would be flagged because of the use of an array initializer:

int[] values = {0, 1, 2};

Now, I can disable it if I don't like it, that's not a problem. But I'm wondering why would this be a problem, and what would be the solution to keep that code from being flagged by the audit rule?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Davor
  • 1,387
  • 16
  • 33
  • 6
    It just seems that there is a bunch of predefined rules that are just a matter of taste so that you can configure it easily. By looking at [this list](https://developers.google.com/java-dev-tools/codepro/doc/features/audit/audit_rules_com.instantiations.assist.eclipse.auditGroup.codingStyle) I find lots of rules that I look either unfounded or even questionable to me. – Holger Nov 19 '14 at 13:29
  • Does the audit checker complain if the array is const? Perhaps they don't want values which can be modified later going into anything other than the dynamic heap storage, due to the potential of buffer overruns, leading to security holes. – Marty Nov 19 '14 at 18:22

2 Answers2

16

It's an interesting question, and this decision is groundless IMHO. (I hope somebody else will answer this thread if there is a legit reason behind this design decision).

Moreover, Google shows how to format those static initializers in their good practice formatting guide https://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s4.8.3.1-array-initializers without saying anything about how bad it is to use those constructs ...

I guess that the person behind that rule just had a tooth against that style of programming :)

Loïc Gammaitoni
  • 4,173
  • 16
  • 39
  • 3
    I don't deny, I posted it as an answer as it is more readable than a comment. Moreover, I guess than one can only speculate about this, as I didn't find any source explaining the why of that decision. – Loïc Gammaitoni Nov 19 '14 at 12:03
  • 1
    I think it actually somewhat answers the question "But I'm wondering why would this be a problem, and what would be the recommended best practice?". It is allowed according to the google styleguide. – Magnilex Nov 19 '14 at 12:10
  • 1
    The relevant part is that "Google shows how to format those static initializer in their good practice formatting guide" even though their analyser rules those out. The link is just a reference that is not necessary to grasp my point. – Loïc Gammaitoni Nov 19 '14 at 13:18
  • 2
    And my point is since Google actually specifies that initializing arrays should be done so and so, according to their guidelines, OP:s question "would this be a problem..." is answered. Google thinks it is OK, but has an optional rule implemented in CodePro AnalytiX. So I gave this answer my +1. – Magnilex Nov 19 '14 at 14:10
  • Thank you @Magnilex for the support, seems like those people (whose comments are now removed) would bash anybody with the pretext of poor formatting or poor relevance to get some reputation ... Their comments were the only irrelevance towards that topic ... sad... – Loïc Gammaitoni Nov 19 '14 at 15:27
11

I'd think that is because it is a special syntax that only works when initializing values.

int[] values = {1,2,3} //legal

int[] values2;
values2 = {1,2,3} //not legal


int [] values3; 
values3 = new int[]{1,2,3} //legal

The last form values3 is legal wether it is when creating the array or later on. So instead of mixing forms of initializing arrays you'd be better of using the same form always. IMHO that makes for clearer code, following the principle of least surprise.

Strangely though the google code style does not prohibit this form of initialization which is very clear in this example.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • 1
    "Array Initializer" is indeed used by the [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6) specifically for the `{ values }` part, however `new int[]{1,2,3}` is still using an array initializer ["as part of an array creation expression"](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.10). Question is, does the analyzer mean the same thing? – zapl Nov 19 '14 at 12:30
  • A related question: http://stackoverflow.com/questions/5387643/array-initialization-syntax-when-not-in-a-declaration – Vitalii Fedorenko Nov 27 '14 at 02:43