3

I'm reading uncle Bob's Clean Code and it completely revolutionizes my programming style. In this book author claims that best methods are small methods. What about controller's action methods in modern frameworks? Do you try to follow the "20 lines per method" rule, or you delegate some tasks to private methods? I don't ask about things that you can move to services and external classes, sometimes there are situations that you cannot avoid bigger code in action. Another question and another look at this problem - do you tract controller class as (some sort of) special class with only action methods, or just another regular class with private methods and own fields along with action methods?

2
  • 6
    Rules are meant to be followed until you understand them well enough to break them, when it makes sense to do so. Commented Aug 14, 2014 at 21:44
  • 1
    I'd disagree with some of that. If an action has more than 20 lines it is almost certainly doing too much, and it is most likely implementing business logic. Consider moving the code mapping your business entities into your view models into separate methods, or even classes - that's the only thing that should be taking up any significant amount of line count. Commented Aug 15, 2014 at 13:07

1 Answer 1

1

or just another regular class with private methods and own fields along with action methods?

Yes. It's just a class, i.e. keep methods small and refactor as needed into private sub-methods (or delegate to other classes, if appropriate).

Personally, 20 lines is a good upper limit, but I try for less. Occasionally, e.g. for complex algorithms with lots of state, you might go way over 20.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.