I’m wondering what’s wrong with an empty function block. I have a class with a
stop()
method that currently doesn’t do anything. I want the API to say that the
stop()
method should be called when it is no longer needed. It’s not the caller’s business whether any instructions are there or not. There may be in the future. Or is this a case of YAGNI?
Ellen Spertus
10/29/2019, 8:33 PM
It used to do something, which is why I’m only now getting the detekt warning.
m
Mike
10/29/2019, 9:58 PM
Tough to answer without more context. But if the function serves a purpose, you can always override Detekt with an @Suppress. It doesn't know everything.
e
Ellen Spertus
10/29/2019, 10:47 PM
Thanks for the reply, @Mike. I know I can suppress it but didn’t want to do that casually in case I misunderstood the reason for the rule.
m
Mike
10/29/2019, 11:47 PM
Under normal circumstances, an empty function means you forgot to delete something, as it serves no obvious purpose. If you know it will serve a purpose, or you have to implement it because you're extending an abstract class that doesn't define a reasonable default, then you could end up with a legitimate empty function, and then Suppress would make sense.
Don't know if that helps explain my thought process better.
b
Brais Gabin
10/30/2019, 1:44 PM
I don’t know if I understand you properly. You are using an API that forces you implement a function and you want to do nothing there? You should active
ignoreOverriddenFunctions
property. IMO this should be active by default.
Brais Gabin
10/30/2019, 1:45 PM
If you are creating that api and your “default implementation” is do nothing you should add a comment inside the function to note. Something like:
// no-op
.
Brais Gabin
10/30/2019, 1:48 PM
So I think that the rule is ok. If you really want to do nothing a comment tells others that this is not a bug. And if you are just fighting agains a bad API you can enable