Intro to Methods
Not Started

If you made it here, you can make it anywhere! ✨

You know how to define classes, instantiate them into objects using constructors, and declare member variables by this point. This taught us how to express state. Now, we will learn how to express behavior with methods.

A method is a set of code defined within a class. It divides code into small, reusable subroutines. Developers can create custom methods that accept inputs as parameters, manipulate data, and return results.

Consider we’re asked to calculate an employee's tenure. To determine this, we’d need to subtract the start year from the current year, currentYear - startYear.

Assuming Employee is defined like this:

public class Employee { String firstName; public String startYear {get; private set;} Employee(String firstName, Integer yearHired) { this.firstName = firstName; this.startYear = yearHired; } }

we’d instantiate an employee and express the calculation like so:

public class EmployeeController { Employee jerryEmployee = new Employee('Jerry', 2015); Integer currentYear = 2023; Integer jerryTenure = currentYear - jerryEmployee.startYear; }

This is fine, but it ain't great. If we’re asked to do the same thing for Elaine, we start to notice a pattern:

public class EmployeeController { Integer currentYear = 2023; Employee jerryEmployee = new Employee('Jerry', 2015); Integer jerryTenure = currentYear - jerryEmployee.startYear; Employee elaineEmployee = new Employee('Elaine', 2018); Integer elaineTenure = currentYear - elaineEmployee.startYear; }

The formula isn't complex but needs to be rewritten for each employee object. It’s not reusable. If the formula changes in the future, we’d need to update each formula individually. We’d have to copy and paste the formula if we introduce a new employee. That’s not my idea of a good time.

As developers, we aim to maintain code that adheres to the DRY principle, which stands for "Don't Repeat Yourself." DRY code minimizes redundancy and provides a single point of reference for knowledge. Methods play a key role in achieving DRY code by allowing us to normalize, i.e., organize and group logic together to reduce duplication.

The opposite of DRY code is WET (Write Everything Twice or Write Every Time) code. We want to write DRY code and avoid WET code. This can sometimes turn into an art form rather than a science.

In the next lesson, we’ll write DRY code by creating the calculateTenure() method to encapsulate the formula, ensuring it can be easily invoked without the need for repetitive rewriting.

Challenge

But first, we need to feel the pain of WET code. Given the provided EmployeeController class create two new employee objects.

The first object should be named georgeEmployee. George started in 2014. Calculate his tenure using the formula and store the result in georgeTenure.

The second object should be named kramerEmployee. Kramer started in 2022. Calculate his tenure using the formula and store the result in kramerTenure.