3

Bit of a random question, I know, but it's been bugging me for years, and every time I Google around looking for examples of how other code handles it, I come up short.

Here's what I imagine an instantiated list or set should look like, from a stylistic perspective:

Set<string> setEg = new Set<string> {'a',
                                     'b',
                                     'c'};

Would you experienced coders consider this a 'correct' or 'good' style? There's a few stylistic choices represented there, but the one I'm most interested is how the instantiated values are indented.

I use MavensMate with Sublime Text, with syntax set to Java. Generally when going to a newline and hitting tab, the cursor will jump to a context appropriate indentation level. But for list instantiations, it just seems to provide a normal indent space, resulting in:

Set<string> setEg = new set<string>{'a',
 'b',
 'c'};

Which doesn't seem as readable.

3

If the content is to be broken out over multiple lines, then completely separating the content from the container is another style:

List<String> listEg = new List<String>{
        'a',
        'b',
        'c'
        };

It is then easier to see the content or re-order it or cut and paste it. And if the variable is renamed the content doesn't have to be re-indented. This is also using a double indent to indicate that the multiple lines are a continuation of the first line.

But the important thing in a code base is that a consistent style be used so go for whatever the editor you use does easily.

  • 2
    +1 to this. I do not know how many times I have updated lists but forgot the first element because it was on the same line as the container.... – Eric Jul 17 '15 at 21:27
  • 1
    I like the reasoning behind this style - and it appears that Sublime's formatting handles this indentation method consistently. Thank you! – smohyee Jul 20 '15 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.