Case Statement Auto-Formatting In Editor

I don't like the auto-format in the editor.

In every language I have ever used there should be an indent on those statements that are highlighted. In our groovy editor, those statements are flush with the case expression. What are the chances we could get that indent there? Online in groovy tutorials there seems to always be an indent. I feel like this is a deficiency in the auto-formatter or a setting that could be changed.

I've noticed this too... Not a huge deal to go and fix it by hand.. Who knows, maybe the Hubitat staff has a thing against using switch statements!

Since indentation isn't required for the code to work correctly, this is all a matter of preference. You could have everything flush left and it would still work. The IDE editor for ST doesn't auto-indent at all.

That said, why would the IF be indented in this case? The line below the IF, yes, but the if is on the same level as the line above it. It isn't a layer lower. Why would that be indented?

I agree, @srwhite. It's not a huge thing to fix it by hand but one of my frustrations with the general code base for Hubitat and SmartThings is the inconsistency in formatting. If you hold shift and hit tab on a highlighted block of code it auto-formats. I can't use it in half of my apps/drivers because it breaks the manual formatting of switch statements. Because of this I typically format in IntelliJ before committing. It would just be a quality of life improvement.

If you have ever worked with a large code base with many programmers I think you would have a different opinion. Formatting is not a preference at that point. It is often enforced by the repository through various ways. Differences in white space and code formatting can cause conflicts making merges and branching difficult. White space and formatting should always be consistent. Period. Not preferential. If we want to be a bunch of amateurs here that's fine but I'd rather not.

Also, I never said just the IF should be intended by itself. If you noticed my highlighted code does not just contain the IF. The entire block of code under the case statement is not indented. 634 to 636 should have been indented by the editor but instead it justified it to the same indentation as the case expression.

Haha
I’m in the process of removing all indents in my code before release.

Andy

3 Likes

Does groovy not use indents there?

Indents are not required for the code to work.
It just makes it easier to read and follow

1 Like

I was taught old-school formatting techniques many, many years ago. It's kinda stuck...

I think I'll just minify everything going forward. :exploding_head:

When I’m writing the code I always indent correctly.
I just remove the indents (and most white space) from the release copy

Obviously indents aren't required but almost every code has either an official or formally adopted formatting scheme. I assumed Groovy would follow Java's and therefore use indents there. I think I misunderstood the reason you were removing them.

Does that work ok with groovy?
Not tried it.

If someone wants to copy and reuse my code, I want them to work for it :slight_smile:
Why should I make it easy and understandable?

1 Like

Minifying and removing indents?! Arg... Don't contribute to my pain!

Maybe they should switch all of the drivers over to Python... then all of you guys would be screwed.... :stuck_out_tongue:

1 Like

There is literally no work involved in auto-formatting though. Minifying there would be but there would be no benefit to that unless you were trying to prevent downstream manipulation.

Also, I doubt there is a minifier for groovy specifically (because it serves no real purpose since it's not a web language) but there might be a obfuscator. There certainly are for Java.

I don't know.. Never tried it.

I refuse to learn to Python because of that. Ick!!

Python is glorious to use... But the formatting is annoying (even if it is mostly taken care of by any good ide).

Python is consuming the world whether we like it or not. That's one of the things (enforced white space formatting) I didn't like about it at first but after working with programmers that just wouldn't conform to standards I loved it.

The other thing I love are subversion and git hooks. You can enforce formatting convention on any language.

This has driven me mad for a while.

I usually type up a bunch of stuff and then highlight and press shift tab to format it. The case is the only one that doesn’t behave.

2 Likes

The ST IDE editor doesn't auto-indent at all.

I might...but this is not enforced in Hubitat at all. So, why would this be the hill you'd choose to die on?

I also don't understand why that if should be indented.

634 is blank, so how do you know if it's indented. And 635 is the close brace from the else. So, those shouldn't be indented either.