7

What a public method documents as the exceptions that can be propagated out of it (throws declaration) and where the exception actually gets generated are two completely different and independent things. As soon as a method or function detects that it cannot fulfil its contract, that is the point where an exception should be thrown. This is completely ...


6

These are using these terms in a slightly different way, but they are trying to convey similar underlying ideas. Here's a quote from the book you referenced (emphasis mine): Going back to the ShoppingBasket example, you would expect this class to have responsibilities such as add item to basket; remove item from basket; show items ...


5

This sounds like a clear violation of YAGNI (You aren't gonna need it). Do not develop features, or methods that you do not directly need. Basically only create what you know you are going to need, not what you might need in the future. Who knows you might choose to take an entirely different approach before you have the need. Besides that, who says that the ...


5

Variables should be declared with the shortest possible lifetime. Instance variables used as parameters or as return values don't necessarily match that guidance.  That leaves room for error by the consuming client; in this case, the return variables are accessible before calling the function that sets them.


5

Whenever you have an operation between two objects which doesn't seem to properly fit into either one of those objects, there are two possibilities: The two objects shouldn't be two objects, they should be one object. The two objects shouldn't be two objects, they should be three objects. In this case, I believe, it should be the latter. The object you are ...


4

If you want to really practice object-orientation here is a completely easy rule to follow: Don't have getters! This will force your design to assign real responsibilities to things, you will almost certainly automatically follow the Law of Demeter, Tell don't ask, and other important principles of object-orientation. Don't let people tell you that things ...


4

You will not find many design defects in a class diagram itself: The class diagram is a language to communicate about the design. If you’d use English language to communicate about your design, you wouldn’t probably look at the choice of words or the structure of the sentences to find flaws in the reasoning, would you? Moreover the class diagram only shows ...


4

We may need to customize views in future if needed. Creating it earlier and using it makes less refactoring effort. If this is an actual reason, you're using the wrong tools. Imagine that at some point, you need to add a behavior to all of the buttons in your application, and for some reason, creating a base class is the most straightforward way to do it. ...


4

I'd recommend the former, because it better fits the semantics of being a functor. If you want named fields, then make a specific class which it returns rather than a Tuple. That's not to say there's never a purpose for objects that calculate results and store them. Usually this would make sense for something that gets fed data over time and doesn't have ...


3

Yes. More precisely, this is called object composition: you compose a more complex object which access its components using their known interfaces (i.e. the operations defined for primitive data types). In a Java context, there's a difference between primitive data types, which have value semantic, and class types, which have reference semantic, but in both ...


3

Functors are objects that represent functions. In view of this purpose, functors should behave exactly as a function. In many cases, it is even desirable to make them interchangeable with functions, or other functional objects (e.g. a lambda, or an std::function etc...). Your second approach seems very practical at first sight. But you'd better avoid it ...


3

Is there any recommended way by DDD how can I split this AggregateRoot class into few classes? There are at least two different approaches that may help. One possibility is to review your model, and think about whether your aggregate is really multiple domain entities acting in a coordinated fashion. Your "root" entity code may be reduced by ...


3

You’re attentive reading and analysis has helped you to spot a common misunderstanding. Congratulations! The right case is 1: the authors use the same word with very different definitions: For most authors in the OO domain, the responsibility refers to what a class has to do. The term probably finds its roots in Rebecca Wirfs-Brock pioneering work about ...


3

Besides the general principles of Why use getters and setters/accessors? I try to answer that with an example, and focus on the usage class-internally as this is your question. One thing for clarification: In Java there are two classes to represent a point in time regarding Coordinated Universal Time (UTC) where both have a method to get their value, which ...


2

In programming, we build abstractions representing domain concepts.  Ideally, we make things easier for the consuming client programmer (often ourselves).  Coherent abstractions help with that.  One way to look coherence of an abstraction is to look at the lifetime of its members. Our abstractions are often compositions, objects with multiple ...


2

It's unusual for a single object to have 50 variables that wouldn't be better grouped into sub objects. For example a UK address has 14 fields. You could imagine that an address class that covered addresses in many countries might have 50 fields to cover the different formats. But you could obviously split it into multiple country specific address classes ...


2

You cannot generally say that having fifty variables is bad. Let’s say you are a pilot who has a checklist of 200 items to go through and check. And the test results need to be stored. 200 variables. Of course you can split this into 20 classes of 10 instance variables each, but that doesn’t improve things. In your case, I dont see any reason for the fifty ...


2

It's hard to say for sure because we don't know what var1 through var50 are exactly but in general, yes, this is a code smell. It might mean that the class is trying to do too many things. Another possibility is that this is a 'property bag' a.k.a Data Transfer Object (DTO), a.k.a 'value object'. The former is pretty widely considered to be a bad idea. ...


2

what are the exact steps that take place and how is memory allocated to the member functions and the attributes of the class and in what order? This is language (and in some cases, implementation) specific. In a fair number of languages (e.g. Python, JavaScript), the allocation of memory is not a concept which is exposed by the language so your question ...


1

You seem to have a basic misunderstanding of how instantiation works and how a program goes from human-readable text to a format that can be executed by a processor. First, when a source file with a class in it is read by a compiler or interpreter, the entire class is read and converted to a data structure that can be used by either the runtime environment ...


1

Ask not why you want to throw an Exception but why would you want to catch one! The most important part of Structured Exception Handling is that last bit - handling the Exception. In my book, that means catching it and doing something "useful" with it. What's "useful"? Well (as ever) it depends. If your own code can catch an Exception, &...


1

Yes, diagrams can be used to find overall / high level defects in the modeling and design and are sometimes a good base for discussions. Be aware however that design is a highly subjective topic. First, forget the SOLID principles! There are much more usable and practical ways to determine whether a design looks good or not. Here are some mechanical (easy ...


1

Trying to find defects in a design by just using a class diagram, without implementing the classes in code is like trying to find bugs in code without tests. - So don't get me wrong, I am not saying it is completely impossible to find some defects (or "design smells"), but in total it is not very efficient. So yes, one can look at the diagram and review it,...


1

Is there multiple definitions for responsibility of class? In short, yes. That's true of most words, and while logical sciences often tend to be much more rigorous about its nomenclature, there are still some ambiguous namings. You have stumbled on one of them. Responsibility can be defined at different granularities. You could say that waiter and cook are ...


1

(My opinion on this.) I don't think that there is a "categorical" answer ... and I think that your question as posed either wishes or assumes that there is one. "Method parameters" don't store data: they are simply inputs, needed by the method to carry out a particular operation. If some client of the object wants to make a series of independent calls ...


1

Check the semantics first. Someone might subclass your class and (mis-)override the getter to not to return the instance variable. In this sense, utilizing the getter instead of the instance variable is wrong, even in the base class itself. On the other hand, you might not allow subclassing.


1

tl;dr– If you need to share a mutable state between different methods in the same class, then having that mutable state stored in a field/property seems entirely reasonable. However, you may want to centralize how that state is read/modified rather than having each method duplicating the same access logic. To do this, you may want to write up a ...


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