New answers tagged

0

I would do the following: Remove the second function with the String as second parameter. Call device.setName(deviceName) before calling attachDevice(device). It is not the responsibility of the attachDevice method to set the name of the device. Change the attachDevice method to add the device to the genericDeviceMap if it has a name, as shown in the code ...


9

I would discourage you to ever use null since it can lead to a further NPE, which are hard to debug (and cost a lot if they occur in production code). Solution 1 (overload method) If no deviceName is provided, you can provide a default one instead. The biggest disadvantage from this approach is the danger in genericDeviceMap.put(deviceName, device) because ...


4

I'd argue that your team leader is asking you to create bad code. Having to pass some arbitrary value in as a name parameter when you do not want to specify a name is messy and confusing for any devs looking to use the method. Instead, keep your two methods (though I'd rename your latter one to something like attachNamedDevice to make it clear it does ...



Top 50 recent answers are included