Extract Interface - method modifiers

When you do extract interface, it doesn't put the public access qualifier on the methods:


Is it possible to get IDEA to explicitly put in the public modifier when doing Extract Interface?

The same would apply for constants - public static final.

If it's not possible, then can it be made possible?

Thanks,
Calum

12 comments
Comment actions Permalink

When you do extract interface, it doesn't put the public access qualifier on the methods:


And this is good so, because "public" is redundant.

0
Comment actions Permalink

No, if an access qualifier is not specified, the default is "package",
which is not the same as public.

Lars Köhler wrote:

>> When you do extract interface, it doesn't put the public access
>> qualifier on the methods:


And this is good so, because "public" is redundant.


0
Comment actions Permalink

When you do extract interface, it doesn't put the

public access qualifier on the methods:

And this is good so, because "public" is redundant.


I am aware that "public" is redundant. However, "good" is rather subjective.

Different coding conventions may say different things - ours say to include "public" explicitly.

IDEA should allow for different coding conventions, rather than enforce one particular "better" one. Settings | Code Style is obviously there to allow for user-specified preferences.

Calum

0
Comment actions Permalink

No, if an access qualifier is not specified, the default is "package",
which is not the same as public.


We are talking about interfaces, not classes. In interfaces "public" is
the default.



--
Lars Köhler

0
Comment actions Permalink

Java Language Specification, §9.4:

... "It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods."

I think that "strongly discouraged" part is reason enough for IDEA to not add 'public' modifiers and to not offer such a choice.

0
Comment actions Permalink

Well, writing "public" implies, that there can be another option. But
there is no alternative to "public", written or not written. Hence it is
better to avoid it.
--
Lars Köhler

0
Comment actions Permalink

Calum MacLean wrote:

When you do extract interface, it doesn't put the public access qualifier
on the methods:

 {
>     void doIt();
> }]]>


Is it possible to get IDEA to explicitly put in the public modifier when
doing Extract Interface?


Not at the moment.
I personally agree with most people here that 'public' is quite unneccessary
here.
However, you may file an SCR for that - let's see how many votes it will
get.

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Well, writing "public" implies, that there can be
another option. But
there is no alternative to "public", written or not
written. Hence it is
better to avoid it.
--
Lars Köhler


So, I hadn't realised that the JLS "strongly discourages" using "public".
However, it is still allowed by the JLS.
I'd note that common Java interfaces (e.g. Runnable, ActionListener) use the explicit "public".

Also, "better" is again subjective - you could have all sorts of arguments either way.

IDEA should allow the developer (or the coding standards) to choose.

Calum

0
Comment actions Permalink

Not in interfaces.

N.

Donald F. McLean wrote:

No, if an access qualifier is not specified, the default is "package",
which is not the same as public.

Lars Köhler wrote:

>>> When you do extract interface, it doesn't put the public access
>>> qualifier on the methods:
>>
>>
>> And this is good so, because "public" is redundant.


0
Comment actions Permalink


While I would certainly not wish to see such an option in IDEA, I will say that it would be pretty easy to create a code inspection which would notify you of all such "missing" public/static/final modifiers and allow them to be automatically added. If you are tempted to write such a plugin, the InspectionGadgets inspection which automatically removes all such unnecessary interface modifers (clevery titled UnnecessaryInterfaceModifierInspection.java) would be a good place to start.

--Dave Griffith

0
Comment actions Permalink

Added - http://www.intellij.net/tracker/idea/viewSCR?publicId=25391.

I'd note that, for those people like me who do put in the "public" modifier, it's really annoying to have to put it in manually after each Extract Interface.

For those who don't put in the "public" modifier, it doesn't do any harm to them to have an option (which can default to not including the modifiers).

0
Comment actions Permalink


There are a large handful of options which are "allowed" in Java, but discouraged. Did you know you could put the keyword "abstract" before "interface", for instance. It has no effect whatsoever. For that matter, "abstract" is also allowed but discouraged (and pointless) on interface methods. Would you want an option for that as well?

--Dave

0

Please sign in to leave a comment.