質問

My project at current job use private access modifier for methods of MVC controllers:

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    private ModelAndView helloWorld() {

I have integrated PMD and his report has a lot of:

/src/main/java/com/web/controller/SignalController.java:91: Avoid unused private
                                            methods such as 'handleNewRequest()'.

So instead of disabling useful PMD rule I think to change controller's methods visibility to public.

Are there any reasons to keep controller's methods private?

役に立ちましたか?

解決

You're shooting yourself in the foot by making it private:

  1. it's seen as unused by PMD and the IDEs (and, transitively, all the other private methods it calls, too). So you or a collegue might mistakenly remove private methods that are actually used.
  2. It makes it much harder to unit-test them.
  3. It's unconventional, making your code look weird to experienced Spring developers.
  4. They're logically public, since they're called by code outside of the class and package.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top