Hot answers tagged

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


43

So is there a reason why isn't this a OOP convention? My best guess: because it violates CQS You've got a command (changing the state of the object) and a query (returning a copy of state -- in this case, the object itself) mixed into the same method. That's not necessarily a problem, but it does violate some of the basic guidelines. For instance, ...


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


29

Saving a few keystrokes isn't compelling. It might be nice, but OOP conventions care more about concepts and structures, not keystrokes. The return value is meaningless. Even more than being meaningless, the return value is misleading, since users may expect the return value to have meaning. They may expect that it is an "immutable setter" public ...


12

I don't think this is an OOP convention, it's more related to the language design and its conventions. It seems you like to use Java. Java has a JavaBeans specification which specifies the return type of the setter to be void, i.e. it is in conflict with chaining of setters. This spec is widely accepted and implemented in a variety of tools. Of course you ...


10

First off, var list = new List<string>(); list.Add("hello"); list.Add("world"); Is just as, if not more readable than var list = new List<string>().Add("hello").Add("world"); Lines of code is not, in any way a proxy for code's cleanliness or simplicity. Thus my question for this very particular pattern (i.e "return this" for method ...


8

In the MVC design pattern, the Controller part is responsible for translating user actions into modifications of the various Model classes that are involved in a piece of functionality. There is no one-to-one relation between Controllers, Views and Models. In particular, if a user action requires changes to multiple Model classes, then it is the ...


6

The one-liner definition of OCP is very nice, although it tends to mislead. Quoting the original Bob Martin's article on OCP: It should be clear that no significant program can be 100% closed. ... In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. (I really recommend you reading ...


6

First, reduce the problem to a test of availabilities "per day". All your examples are using ranges for only one day, but if you also want to support a range like "Monday 10:00 to Wednesday 14:00", break this down into three tests for "Monday 10:00 to 23:59", "Thursday 00:00 to 23:59" and "Wednesday 00:00 to 10:00". For each day, store an interval ...


5

I think much of the reason it's not a convention to chain one setter after another is because for those cases it's more typical to see an options object or parameters in a constructor. C# has an initializer syntax as well. Instead of: DTO dto = new DTO().setFoo("foo").setBar("bar"); One might write: (in JS) var dto = new DTO({foo: "foo", bar: "bar"}); ...


5

That technique is actually used in the Builder pattern. x = ObjectBuilder() .foo(5) .bar(6); However, in general it is avoided because it is ambiguous. It is not obvious whether the return value is the object (so you can call other setters), or if the return object is the value that was just assigned (also a common pattern). Accordingly, ...


5

As other people have said, this is often called a fluent interface. Normally setters are call passing in variables in response to the logic code in an application; your DTO class is a example of this. Conventional code when setters don’t return anything is normal best for this. Other answers have explained way. However there are a few cases where ...


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

Starting from the bottom; if you want to know if two values are the same, you just check that they are equal. Think about 7. No matter what happens between now and the heat death of the universe, 7 is never going to equal 2. On the other hand, entities do change. Today, Bob is 2. In the due course of time, Bob will be 7. Even though he isn't 2 any ...


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


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

I think you are confusing a few different ideas here. The repository is usually used an abstraction between the consuming code and the persistence component. Typical methods on a UserRepository would be interface UserRepository { void save(User user); User getById(UserId id); } Operations to change the User would normally be implemented on the ...


3

Whatever kind of "clerical control" your application needs. If you don't know what that means, chances are high your application won't need such control ;-) For example, this could mean to keep account on the whole set of objects of a certain type which were created during the lifetime of your application, for the purpose of reiterating over them at a later ...


3

At least in C#, your desired construct is entirely unnecessary. You can write something like var mylist = new List<string>(new[] {"Hello", "World"}); ... mylist.AddRange(new[] {"Add", "Some", "More", "Items"}); which is about as concise as you can get, while still being perfectly readable. If you really want method chaining in a class, even one ...


3

Returning an object of built-in type from a function usually carries little to no overhead, since the object typically fits in a CPU register. The point of this comment from wikipedia isn't that returning the value has no cost. Overhead is additional cost, and the here the cost is in addition to that of the return value mechanism. The article is ...


3

can it be optimized with no overhead? How? As Telastyn wrote, one approach is to have a compiler providing two function versions. If I were in the role of a compiler designer, I would handle it this way: I would build a compiler with inlining support. Such a tool can obviously optimize the return this statement out when it is not used. and if this ...


3

I think, what you are referring to here is not a specific design pattern, but a more general software development principle called "separation of concerns". If you constrain these concerns into more specific areas like UI logic, service usage and data processing, you are almost in the MVC/MVP world, which could be indeed seen as a pattern. On the other ...


3

I'd argue no-ish. I'm not sure it's really the "module pattern" if you return anything other than a singleton. But, you can certainly use the same "pattern" to accomplish other things. The module protects the global scope from the internal variables used to build the return-value; but the return value can itself be a function -- including a constructor. To ...


2

ViewModel If its responsibility is to hold model data in a format convenient for the view, then I would call it a ViewModel.


2

Meh. Use whatever term you and your team are comfortable with, as long as you're consistent. For what it's worth, when I was reading the question as soon as I saw OTHER "MODEL" OBJECT I thought "Data transfer object".


2

It seems to me you are missing a User object. Obviously there will be millions of reminders, but you're only going to be interested in the ones for a particular User. Hence User.AddReminder(Reminder reminder) User.DeleteReminder(string reminderId) User.Reminders Also, as you point out the reminder will want to be able to provide the user with a link to ...


2

Basically if you look at your requirements posted above: create a Reminder delete a Reminder get the list of all Reminders check if an Event has a Reminder These are classic plain CRUD. Even the keywords match almost exactly Create, Read (/find/get/check), Update, Delete. As such, you have no complicated domain or business logic where a ...


2

What you're describing is business logic. Purchasing an Item is a business transaction. This belongs in the Service layer. Remember that the Repository layer acts as a facade for persistence, and exists to abstract the underlying details of your persistence mechanism from the rest of your application. The Service layer, by contrast, exposes business ...



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