Hot answers tagged

87

You need to consider the cost of change. What if you wanted to change how connections are made? How easy would it be? If you have a lot of duplicated code, then finding all the places that need changing could be quite time consuming and error prone. You also need to consider clarity. Most likely, having to look at 30 lines of code isn't going to be as ...


77

Why not to use Set? If using List: It is easy to add the same role twice Naive compare of lists won't work here properly: [User, Admin] is not the same as [Admin, User] Complex operations such as intersect and merge are not straightforward to implement If you are concerned about performance, then, for example in Java, there is EnumSet which is ...


53

No. In fact, even your "simple" code should be split into smaller parts. At least two. One to make the connection and handle the normal 200 response. For example, what if you change from a POST to a PUT in some cases? What if you are making zillions of these connections and need some multi-threading or connection-pooling? Having the code in one single ...


48

First off, please note that doing something like entity.underlyingEntity.underlyingEntity.method() is considered a code smell according to the Law of Demeter. This way, you're exposing a lot of implementation details to the consumer. And each need of extension or modification of such a system will hurt a lot. So given that, I'd recommend you to have a ...


32

TL;DR: It is usually a bad idea to use a collection of enums as it often leads to a bad design. A collection of enums usually calls for distinct system entities with specific logic. It is necessary to distinguish between a few use cases of enum. This list is only of top of my head so there might be more cases... The examples are all in C#, I guess your ...


17

is it acceptable to copy and paste ... No. For me, the deciding argument is this one: ... it is commonly used ... If you use a piece of code in more than one place then, when it changes, you have to change it in more than one place or you start to get inconsistencies - "odd things" start to happen (i.e. you introduce Bugs). it is ...


9

It seems daft to have a function to check whether the code that is stored is the admin code. What you really want to know is whether that person is an admin. So if you don't want to expose the constants, then you also shouldn't expose that there is a code, and call the methods isAdmin () and isUser (). That said, "if User.getRole().getCode() == ...


7

The programming uses of the verb "map" and the noun "mapper" are largely unrelated to their common uses in English, so this is a very understandable question. The programming use is also very broad, so let's start with the most concrete and well-defined meaning of "map". Namely, the higher-order map function present in most functional programming languages. ...


7

This doesn't really have anything to do with copy and paste. If you take code from elsewhere, at the second you take the code it's your code and your responsibility, so whether it's copied or written completely by yourself doesn't make a difference. In your alerts you make some design decisions. Most likely similar design decisions should be made for all ...


7

The big hint is in your question: "interface inheritance". Basically, an interface is nothing but a set of method signatures. In a traditional OOP language, the only thing a class needs to do to satisfy an interface is to have implementations of those method signatures, and declare that it implements the interface. Since it's not inheriting implementations, ...


6

You can't use multiple inheritance with abstract classes.


5

In addition to what others have posted already, you should keep in mind that using the constant directly has another drawback: if anything changes how you handle user rights, all those places needs to be changed, too. And it makes it horrible to enhance. Maybe you'd like to have a super user type at one point, that obviously also has admin rights. With ...


5

Duplication is OK in some circumstances. But not in this one. That method is too complex. There is a lower limit, when duplication is easier than "factoring out" a method. For example: def add(a, b) return a + b end is stupid, just do a + b. But when you get just a little, tiny bit more complex, then you're usually way over the line. foo.a + foo.b ...


5

Objects are models. They don't have to correspond to real-world objects. Sometimes actions need to be modelled. Take, for example, the typical Bank Account Scenario, that is used in many introductory OO courses. The design that is taught looks a bit like this: class BankAccount { Money balance; void deposit(Money amount) { balance += amount; } ...


4

Write your enum so you can combine them. By using base 2 exponential you can combine them all in one enum, have 1 property and can check for them. Your enum should be lile this enum MyEnum { FIRST_CHOICE = 2, SECOND_CHOICE = 4, THIRD_CHOICE = 8 }


4

Is it a good practice to use List of Enum values on User? Short answer: Yes Better short answer: Yes, the enum defines something in the domain. Design-time answer: Make and use classes, structures, etc. that model the domain in terms of the domain itself. Coding time answer: Here's how to code-sling enums ... The inferred questions: ...


4

if(input.getId() < 36 && input.getId()%3 = 1){ //if the imput's ID is less than 36, it's a metal, and if ID % 3 = 1, it's a ruined metal, meaning it is smeltable. Giving magic meaning to the id numbers is asking for trouble. What if you want add a new type of metal? What about removing one? What about adding a new state? Multiply this across all ...


4

The type system can do many things for you, but even the best type system cannot do everything. There is no problem with having several methods with the same type signature but different contracts. Look at it this way: if everything could be expressed machine-verifiable via the signature, we wouldn't need programmers - just spec writers and compilers. The ...


4

There are some reasons which can make this design the better alternative: GenericHelperClass ghc = new GenericHelperClass(); // ... // using `ghc` here for other purposes //... ProjectSpecificClass psc = new ProjectSpecificClass(ghc); if the construction of ghc is slow or needs lots of resources, it might be better to construct the object only once if ...


4

In simple cases, it is possible to read information about entities from your database query result sets and do whatever needs to be done without creating objects. For more complicated cases, it is more convenient and maybe more efficient to create objects to represent the entities. But you don't have to do that. Even in the most complicated cases you ...


3

When it comes to reference types (classes), you're already ok since those have Equals and GetHashCode implemented properly even without overriding. When it comes to value types (structs, enums, ...), these should always override Equals and GetHashCode in meaningful manner as well as be immutable as per MSDN design guidelines - struct should implement ...


3

It's a mathematical concept. www.thefreedictionary.com 2. Mathematics The correspondence of elements in one set to elements in the same set or another set. It's to establish a correspondence between two distinct data models. For example, you can say thay the class User is a representation of a row of the database table USER, that would be ...


3

That isn't something I've seen but I see no reason why it wouldn't work. You might want to use sorted list so it defends against dupes however. A more common approach is a single user level e.g. system, admin, power user, user etc. System can do everything, admin most things and so on down. If you were to set the values of roles as powers of two, you could ...


3

What you have here is a typical use-case of runtime multiple-dispatch, and depending on the actual object-oriented language you intend to use, this is either built-in or requires some work. I am sorry this answer cannot be more precise, but this depends largely on the actual programming language. Assuming a Java-esque language, you could quickly devise a ...


3

I think you're getting a bit too caught up in comparing unrelated types of graphs, and perhaps in thinking about data-flows rather than contract dependence. It sounds to me like you've had a discussion with someone who's talking about the call graph of a piece of software, and/or the associated control-flow dependencies between functions. In a call graph, ...


3

When developing games it is often a good idea to separate the game engine from the game content by moving such information to external files. It allows to tinker with values without having to recompile the game, it allows non-programmers to edit these files and it makes the game more modding-friendly. However, keep in mind that file access is very slow ...


3

I would not store calculated values in a database, so that is not related to this question Or is it? The reason for not storing calculated values in your class is the same as the reason for not storing them in your database. It creates additional complexity where none is required. Essentially, it means that the same information is now being stored in ...


3

Just to clarify, IComparable<T> defines that an object has a CompareTo(T) method that can be called. It does not require that the object (on which the method was called) and the argument (that was passed into the method) are of the same most derived type. In general, if you call IComparable<T> between a Cat and a Dog, it should return false. ...



Only top voted, non community-wiki answers of a minimum length are eligible