Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top