SRP: "It says that your class, or method should do only one thing"
When do I know my method is doing more than one thing?
Example:
I have a class Bus
with a List<Passenger>
and a enum BusState
in it. The state of the Bus
depends on the size of List<Passenger>
.
public void addPassenger(Passenger p){
this.passengerList.add(p);
if (passengerList.size < 10)
this.state = BusState.EMPTY;
else if (passengerList.size < 30)
this.state = BusState.HALF_FULL;
else if (passengerList.size >= 30)
this.state = BusState.FULL;
}
And even if I refactor this:
public void addPassenger(Passenger p){
this.passengerList.add(p);
changeBusState();
}
private void changeBusState(){
if (passengerList.size < 10)
this.state = BusState.EMPTY;
else if (passengerList.size < 30)
this.state = BusState.HALF_FULL;
else if (passengerList.size >= 30)
this.state = BusState.FULL;
}
In my opinion the method addPassenger()
is doing more than one thing:
- adding a new passenger to the List
- checking the current number of passengers
- changing the state of the bus if necessary
How can I understand the SRP? Is this method doing more than one thing?