Lesson 12Adding Methods
Do you like to repeat yourself unnecessarily? Do you like to leverage technology and/or techniques to reduce the amount of work you need to do? Have you ever been called lazy—as a compliment?
In this lesson, we'll look at methods. Methods allow us to better organize our code so that it can be used in multiple places. This, of course, means less work. Good coders are lazy coders—well, smart and lazy.
WHY METHODS?
One of the important concepts in writing good code is the DRY principle: Don't Repeat Yourself. When given a task, we want to do it well, and we only want to do it once. Methods allow us to write a bit of code, give it a name, and then use that code over and over again anywhere in our program. Methods give us two new tools for designing and writing solid code:
- We can reuse code that we've written.
- We can use methods to break down large, complex tasks into simpler steps.
Methods don't add any features to the language itself. They are an organizational tool. A large component of writing good software consists of good code organization.
DEFINING METHODS
A method is defined by a method declaration ...
Get Job Ready Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.