In most cases, the answer depends on the vertical size of the screen. Since ancient times, there is a hidden rule for saving the entire function on one screen , where the function name, opening and closing parentheses on separate lines. Thus, this rule leaves about 20 lines for each function with the popular 80x25 screens. This rule still applies if you mostly program on the console and your IDE is called vi. But I think this rule is still valid for other environments and higher resolutions.
There are other measures to understand the complexity of a function, the length of one, as well as the number of local variables is also another strong indicator . If you have too much, you should consider refactoring.
But your question is rather asked for advice on a certain type of function. There are times when these rules are bent. For example, functions with many case statements, such as automatas, can be stored for a long time.
In your case, setting up (initializing) the GUI is a valid case for a long function . However, if you insist on having a small and easy-to-maintain function, you can move anonymous inner classes outside of this method and give your own names. . Then each of the buttons will eventually become just a 2-line code. Or you can use the Extract method for each of the buttons and create them in a separate function.
source share