Creating a Reusable Private Method
The code snippet from the previous chapter is such a common case that we'll use it in this section:
public getUsers( includeInactive: boolean = false, filterText: string = null, orderByName: boolean = false, forHireDate: Date = null) : User[] { }
Ask yourself: Are the parameters just data that I need to give it? Or do some of the parameters indicate that the method is going to alter its behavior?
If you answered yes to the second question, then you probably want to refactor this method.
The easy wins when finding these kinds of methods is to:
- Make them private
- Create wrapper methods exposing the intended behaviors by their individual names
Step 1 would entail changing getUsers
to a private method.
Next, let's look at all the different behaviors this method is exposing:
- Get active users
- Get inactive users
- Get users that have a certain name
- Get users and order them by...