New answers tagged

1

This is a very broad question, as it depends on the language used, and the features that language offers: Can "plain old data structures" be made immutable? Does the language enforce encapsulation of private functions and data, or is it by convention? Is the language statically or dynamically typed? Does it allow functions outside of static classes? Does ...


0

Classes vs "free data" + functions The freedom of data is always at the expense of the freedom of the programmer: With a POD you're free to do whatever you want. And what you want, you will implement in a nicely overloaded function. But other programmers (or your future yourself) are also free to do whatever they want and express their creativity. They ...


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 ...


7

I agree that it is totally okay if a class has only one method, but recently I have read many blogs saying this is bad OOP and rather procedural coding. If a class has only one method, it's usually Execute(), or something equivalent. The question you have to ask yourself is, what are you encapsulating by using a class, if you only have one method? That's ...


7

according to Clean Code, is it okay to add a single constructor to a plain data structure? Yes. Constructors are not functions in this context since they're not something that operates on data. They don't hide the data. And if the data changes, they'll need to change, but that's no different from any other initialization strategy.



Top 50 recent answers are included