We have a method called attachDevice(Device device) which has only one argument. We had a situation to overload this method with one more parameter as like attachDevice(Device device, String deviceName).
with single argument
public void attachDevice(Device device)
with double arguments
public void attachDevice(Device device, String deviceName)
Actually, my Team Lead asked me to make these two methods into a single generic call. The overloaded method only have two additional lines than the single method (which are shown as above). I can pass empty string instead of passing the deviceName, because the invocation of overloaded method will be very lesser than the invocation single argument call. But how bad it is that if passing null value since i don't set the name if the argument will be null. Which would be the best practice for this scenario?
Any suggestions are highly appreciated.
Note: The actual problem I stated here is, that
i) I wanted to merge overloaded methods into one method which should be generic to avoid code duplication
ii) Need a recommended value to pass to the method (either empty string or null value) to stick with best coding practices.
From the given answers, I got the best solution for my problem. The linked question which has been referred this question duplicate also provides info about handling null value, but not covered the first point I mentioned in the note. And I didn't write bad code by passing null as parameter, since I am aware that NPE will thrown sometimes.