So I am a relatively new programmer and am still learning, and have run into a bit of an issue (so apologies if my tags are a little off). Early on I had a chance to read an article discussing the advantages of breaking up methods that contained lots of code into sets of calls to many smaller, clearly-named methods. In general I felt like this made cleaner looking code, and definitely made unit testing a lot easier. However, I have some concerns about whether to make all these new methods public or private. Making them private seems like the right thing to do, since the rest of the code generally doesn't need access to these methods. However, unit testing private methods can be messy. Is there a best practice for this?
What I'm doing right now:
public class WashingMachine {
public Load wash(Load load) {
// Removes one sock from load
for (ClothingItem item : load.getItems()) {
if (item.getType().equalsIgnoreCase("sock") {
load.removeItem(item);
.. // logic for sending sock to proper dimension
break;
}
}
// rest of logic for cleaning clothes
}
}
Turns into:
public class WashingMachine {
// Wash a load of laundry
public Load wash(Load load) {
// Removes one sock from load
removeSock(load.getItems());
// rest of logic for cleaning clothes
..
}
// Oh no, I can't unit test this easily!
private void removeSock(List<ClothingItem> items) {
...
}
}