Skip to main content
35 votes

How do interactions between objects work?

In your design, coins need to know about vending machines. This unnecessary coupling seems to be a wrong start: The vending machine’s responsibility is to provide a product if the price is paid. The ...
Christophe's user avatar
  • 80.6k
34 votes
Accepted

Unable to solve mystery of functions in Javascript

I am trying to understand behind the curtain scenes of Javascript and kind of stuck in understanding the creation of built in objects, specially Object and Function and the relation between them. It ...
Eric Lippert's user avatar
  • 46.4k
26 votes

How far can you push Object Oriented Programming?

I think the principle that all getters somehow violate oop principles at some fundamental level is wrong. If I have a string class and I want to know how long the string is by calling getLength does ...
chubbsondubs's user avatar
19 votes

How far can you push Object Oriented Programming?

This is not a getter Just because a method has get in its name doesn't mean it's a getter; nor does renaming a getter to eg say fetchName stops it from being a getter. In fact, the getItem method is ...
falsedot's user avatar
  • 315
16 votes
Accepted

How do interactions between objects work?

I'm not going to criticize your design because I see that it's not the point. Indeed coins shouldn't know about vending machines, and the related arguments, are valid. Let's assume that these classes ...
Kaz's user avatar
  • 3,652
13 votes

How do interactions between objects work?

The code is being too literal in attempting to model the real world. Object oriented programming does not need to model the real world. Instead, it should model the business process of purchasing ...
Greg Burghardt's user avatar
10 votes

How far can you push Object Oriented Programming?

I'll take a stab at it but I'll just touch on the hash table question. Firstly, the person that replied to you, is an advocate of Object oriented domain driven development (coined by him) and ...
Ced's user avatar
  • 599
10 votes
Accepted

How far can you push Object Oriented Programming?

However, some boundaries, like library boundaries, make it impossible to move the method and so you move data. I agree. Sometimes you do not (or can not) know the behavior that will be attached to ...
Robert Bräutigam's user avatar
8 votes

How to avoid != null statements?

Deciding whether or not null is a allowed as an object value is a decision that you must make consciously for your project. You don't have to accept a language construct just because it exists; in ...
Kilian Foth's user avatar
7 votes

Why are classes named "class"?

Just did some googling and it seems the terms were invented by Ole-Johan Dahl and Kristen Nygaard for their Simula programming language We chose the terms “class” and “objects” of classes for our ...
Ewan's user avatar
  • 79.8k
6 votes

Design pattern for objects in invalid states

No no no no. First, stop using floating point numbers to represent base 10 money. Ints work fine if you count pennies and remember to add the decimal point when presenting them as dollars. ...
candied_orange's user avatar
6 votes

Unable to solve mystery of functions in Javascript

You have many excellent answers already, but I just want to give a short and clear answer to your answer about how all of this works, and that answer is: MAGIC!!! Really, that's it. The people who ...
Jörg W Mittag's user avatar
6 votes

How to avoid != null statements?

In Java 8, a new feature was added: Optional<>. This is for fields that may or may not be there and is an alternative to using null checks. If you use Optional, then you can check whether or ...
Jonathan Coustick's user avatar
6 votes

If methods are actions that an object can perform, how do we represent abstract objects?

First, let's look at what "object-oriented" actually means. The very first object-oriented programming language was Simula (1960). As the name implies, it was designed for simulations. If ...
Jörg W Mittag's user avatar
5 votes
Accepted

How to avoid != null statements?

The dilemma If a variable with null value gets used in your program causing a NullPointerException, this is clearly a situation in your program which you did not expect. You must ask yourself the ...
Neil's user avatar
  • 22.8k
5 votes
Accepted

Object vs Primitive: Not use long primitive because... the default value is zero?

If you need a representation for "not set", then Long is a good choice, with null standing for "not set". If that isn't needed, don't waste time and space with the unnecessary boxing and unboxing ...
Ralf Kleberhoff's user avatar
5 votes

Where to put the behaviour of DTOs ? Object vs Data structure clean code

DTOs are not part of the domain model, and do not have any behaviour. DTOs represent the data structures that are transferred across your system boundary, e.g. data that is serialized to be sent ...
amon's user avatar
  • 135k
5 votes

How far can you push Object Oriented Programming?

You can't infer OOP from a line of code This question is a bit of a semantic puzzle in what it's asking. But that not every line of a program can be purely object oriented. Yeah, because object ...
Flater's user avatar
  • 56.3k
4 votes

Should we consider immutability for local scoped variables

The reviewer is right: Modifying the variable in a way you do makes the code difficult to reason about. Actually, it makes the code quite cryptic. Imagine I'm barely using the method from your ...
Arseni Mourzenko's user avatar
4 votes

Why are classes named "class"?

Because 'class' is a great choice for the concept? "A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality." https://en....
passer-by's user avatar
4 votes
Accepted

Should only static functions print to screen?

I think there is no widely known principle stating that only static methods should print to screen. For example, a CLI app can have injected loggers inside its' objects. Such loggers would print to a ...
Tulains Córdova's user avatar
4 votes

How do interactions between objects work?

Coin should have only getters about their value and no insert method since it doesn't have any responsability other that being a legit coin. VendingMachine should have an Accept(Coin* coin) method. ...
Tulains Córdova's user avatar
4 votes
Accepted

Opaque objects on the stack / in structs

The typical way to create allocatable opaque types is to give up on the opaqueness in the strictest sense, and to declare access to internals as UB. For example, the POSIX pid_t type happens to be a ...
amon's user avatar
  • 135k
4 votes

Services: Which structure is a better idea?

If you can't decide, it's because you're not making your decision based on things that really "matter" to you. If your objects would express the actual behavior of the application you're ...
Robert Bräutigam's user avatar
3 votes

Should I be creating more objects in Java?

Should I be creating more objects in Java? If you can create more useful classes you should. As you do other classes should be getting smaller. ...messing around with a program that essentially ...
candied_orange's user avatar
3 votes

Unable to solve mystery of functions in Javascript

From ECMA spec 1 ECMAScript does not contain proper classes such as those in C++, Smalltalk, or Java, but rather, supports constructors which create objects by executing code that allocates ...
Ewan's user avatar
  • 79.8k
3 votes
Accepted

Design pattern for objects in invalid states

How are responsibilities between classes? There is no single answer to that question. It's first a question of responsibilities: Shall using classes be responsible for verifying if they can do the ...
Christophe's user avatar
  • 80.6k
3 votes

Why do we need factories in the first place?

The real question is what you want to achieve, and either achieve it in C++, or in another language if you think it would suit the needs better. Why can't virtual constructors exist in C++? Imagine ...
Christophe's user avatar
  • 80.6k
3 votes

How far can you push Object Oriented Programming?

Does this count as a "pure" HashTable? Note that any data container, like an array or hash table, can expose a ForEach method that takes a function pointer as argument and call that function ...
Ccm's user avatar
  • 2,144
3 votes

How far can you push Object Oriented Programming?

I almost responded with an answer similar to this on the other question and decided not to. I've been tempted twice, I'll bite. What are we even arguing about here? Why do we do Object Oriented ...
Jared Smith's user avatar
  • 1,935

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