ABSTRACT

So far, we have written all the code in the main method. Although this is fine for small programs, it is impractical for larger ones. Consider a program that consists of ten thousand lines of code. Scrolling through all the lines to find an error is not a viable option. To avoid this scenario, Java allows us to modularize our code into methods. Good software practices call for every method to perform a single task. For example, a method that reads an integer from a file and multiplies it be a constant will not be very reusable. The chance that one will require these two operations in the same order again is very low. However, a method that reads an integer from a file has a good chance of being reused. Similarly, a method that multiplies an integer by a constant also has a high probability of being reused. As a rule of thumb, a method should not be very complicated and it should rarely take more than 50 lines of code. If a method is longer than that, we should consider breaking it down into smaller methods.