Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar: Array should contain trailing comma

Sonar shows me a minor working saying:

- Array Trailing Comma
Array should contain trailing comma.

And my code contains:

RECOVER_PASSWORD(new String[] {
        RequiredPermissions.USERS_PASSWORD_RECOVER,
        RequiredPermissions.USER_RETRIEVE,
        RequiredPermissions.API_USER_RETRIEVE,
        RequiredPermissions.ONETIMELINK_CREATE,
        RequiredPermissions.API_ONETIMELINK_CREATE,
        RequiredPermissions.PASSWORD_RECOVER,
        RequiredPermissions.API_PASSWORD_RECOVER }),

Why should I insert a trailing comma?

like image 315
Jordi P.S. Avatar asked Aug 23 '13 09:08

Jordi P.S.


2 Answers

Why should I insert a trailing comma?

You don't need to. I suspect Sonar should just be showing a warning there.

It depends on programmer's preference what he prefer. Adding a trailing comma at the end of the array, makes it easy to remove or add any entry later on from the array.

So, if you have an array like:

String[] arr = new String[] {
                  "abc",
                  "def",
                  "ghi",
               }

Adding an entry merely requires you to add that entry with trailing comma at the end of your array. Without comma, you would first need to add a comma, then the element.

And removing an element just requires you to remove that line containing that element. Or just commenting out that line.

String[] arr = new String[] {
                  "abc",
                  "def",  
              //  "ghi",    // This won't cause any error
               }

If trailing comma wasn't allowed, then if you remove the last element, you would have to go and remove the comma before it too. Well, I know this reason is quite absurd. But that's just allowed.


This is highly beneficial for code generators while generating the code for array initialization. They can simply go on adding elements with trailing comma in the array, without worrying about whether it's the last element.

Let's take an example:

StringBuilder array = new StringBuilder();

array.append("int[] arr = new int[] {").append("\n");
for (int i = 0; i < 10; ++i) {
    array.append(i + ",").append("\n");
}
array.append("}").append("\n");

Consider if a trailing comma wasn't allowed, then how that code would look like. You would have to handle the last element separately. So, it's just doing good to the code generators.

like image 127
Rohit Jain Avatar answered Oct 18 '22 10:10

Rohit Jain


JLS (§10.6) states:

A trailing comma may appear after the last expression in an array initializer and is ignored.

It is in no way necessary.

However, it's somewhat of a style choice, and it can make things easier for reordering, manipulating, and/or copy-pasting of the last element. Moving RequiredPermissions.API_PASSWORD_RECOVER to another place or pasting in new entries below it can be easier as you don't need to add an extra comma and risk a syntax error from mistypes or misclicks.

like image 6
nanofarad Avatar answered Oct 18 '22 10:10

nanofarad