Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force explicit return statements in Groovy, especially with RxJava based code?

I've already burned a number of times by the implicit return statements in Groovy, especially with RxJava. Is there a declaration or setting that will put Groovy in a mode where any values returned via an implicit return statement will be ignored (not returned)?

In Groovy when using Reactive Extensions forgetting to return an Observable leads to hard to debug "zombie" situations where sequences stop emitting values after seemingly minor changes.

Another difficult debugging situation is created by dropping a statement that would be innocuous in an explicit-return language, by accidentally putting at the end of an operator closure or some method that must return an observable, and now unwittingly returns some other type due to the new statement that occludes the needed Observable return value on a previous statement, thereby blowing up the Observable sequence.

This is not a nitpick. I've already lost a lot of time chasing down problems that would never have happened if only explicit return statements returned values. I think Groovy is a powerful language and I really don't have time to rewrite the Groovy code base I am working in Java to get what I want. So I am hoping there's a Groovy setting that will do what I want.

like image 452
Robert Oschler Avatar asked Feb 28 '26 14:02

Robert Oschler


1 Answers

Is there a declaration or setting that will put Groovy in a mode where any values returned via an implicit return statement will be ignored (not returned)?

No there isn't. You could write an AST transformation that imposed whatever rules you like and you could have a static analysis tool like codenarc complain for non-void methods that don't have an explicit return, but the answer to your question as asked is "no".

like image 179
Jeff Scott Brown Avatar answered Mar 02 '26 05:03

Jeff Scott Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!