Skip to main content
203 votes
Accepted

Should we design our code from the beginning to enable unit testing?

Reluctance to modify code for the sake of testing shows that a developer hasn't understood the role of tests, and by implication, their own role in the organization. The software business revolves ...
Kilian Foth's user avatar
165 votes
Accepted

How do I ensure that interface implementations are implemented in the manner I expected?

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and its fix. // should be class, not struct as struct can be created ...
Euphoric's user avatar
  • 38.2k
82 votes

Should I still follow "programming to an interface not implementation" even if I think using concrete class members is the simpler solution?

Programming to an interface means that you should focus on what the code does, not how it is actually implemented. See Telastyn's answer to Understanding “programming to an interface”. Interface ...
pschill's user avatar
  • 2,040
75 votes

Should we design our code from the beginning to enable unit testing?

It's not as simple as you might think. Let's break it down. Writing unit tests is definitely a good thing. BUT! Any change to your code can introduce a bug. So changing the code without a good ...
Ewan's user avatar
  • 84.4k
55 votes
Accepted

In "I don’t want my users knowing that I’m handing them an interface.", why is the severity "I don’t want" instead of "Users don't need to know"?

why would the post use the tone "I don’t want" in "I don’t want my users knowing that I’m handing them an interface." This is a cite from the "Clean Code" book by Robert ...
Doc Brown's user avatar
  • 220k
51 votes

Is it ok to inherit a class without adding anything to the child, to respect the Open Closed principle?

No. Emphatic no. Unless I misunderstood you, the question is to subclass for a different behavior, but actually not have the behavior itself. Instead an outside actor checks the exact type and does ...
Robert Bräutigam's user avatar
49 votes

Should I still follow "programming to an interface not implementation" even if I think using concrete class members is the simpler solution?

"Programming to an interface" does not require the language keyword interface. It means you care about what promises the type provides about it's behaviour. You don't care how java.lang....
Caleth's user avatar
  • 12.4k
45 votes

Is it Good Practice to Only Expose Interfaces

As is the nature of using interfaces, this decouples my users from being concerned with how I variously decide to refactor the implementations. Let's take a step back and make sure we understand what ...
JimmyJames's user avatar
  • 30.9k
40 votes
Accepted

Implementation of pure abstract classes and interfaces

In C# and Java implementations, the objects typically have a single pointer to its class. This is possible because they are single-inheritance languages. The class structure then contains the vtable ...
amon's user avatar
  • 136k
39 votes
Accepted

Pass object twice to same method or consolidate with combined interface?

No, this is perfectly fine. It merely means that the API is over-engineered with regards to your current application. But that doesn't prove that there will never a use case in which the data source ...
Kilian Foth's user avatar
38 votes
Accepted

What does the author mean by casting the interface reference to any implementation?

Abstracting your class into an interface is something you should consider if and only if you intend on writing other implementations of said interface or the strong possibility of doing so in the ...
Neil's user avatar
  • 22.9k
38 votes

In C#, is there a way to enforce behavior coupling in interface methods or is the fact that I am trying to do that a design smell?

What you are looking for is a well-known approach called Design by Contract. It was supported directly in the framework in version 4.0. DISCLOSURE: Be careful when adding code contracts to a new ...
Doc Brown's user avatar
  • 220k
37 votes

In a fluent interface with "with", is cloning expected?

Looking at this code I’d have no idea. Semantically you did say it’s another pizza. But since this is of type Pizza and not a PizzaBuilder that gives you a pizza object only after you call the build ...
candied_orange's user avatar
35 votes
Accepted

In C#, is there a way to enforce behavior coupling in interface methods or is the fact that I am trying to do that a design smell?

Well, first of all, let's tweak your interface a bit. public interface IComponent { void Enable(); void Disable(); bool IsEnabled { get; } } Now then. What could potentially go wrong ...
Robert Harvey's user avatar
33 votes

Why just "interface segregation principle" but not "method segregation principle"?

The principle of Low Coupling/High Cohesion states that functionality that is closely related and interdependent should be in the same class or module, and functionality that is not closely related ...
JacquesB's user avatar
  • 62.3k
32 votes
Accepted

Does it make sense to define an interface if I already have an abstract class?

Yes, because C# doesn't allow multiple inheritance except with interfaces. So if I have a class which is both a TypeNameMapper and SomethingelseMapper I can do: class MultiFunctionalClass : ...
Ewan's user avatar
  • 84.4k
32 votes

In C#, is there a way to enforce behavior coupling in interface methods or is the fact that I am trying to do that a design smell?

You're asking too much of C# Interfaces. C# Interfaces are contracts. They say what pack of methods a given class implements, and they guarantee that those methods will be there if someone calls them....
T. Sar's user avatar
  • 2,166
30 votes

What does the author mean by casting the interface reference to any implementation?

The accepted answer is correct and very useful, but I would like to briefly address specifically the line of code you asked about: ISomeClass myClass = new SomeClass(); Broadly speaking, this isn't ...
Kamil Drakari's user avatar
27 votes

In a fluent interface with "with", is cloning expected?

No, the naming prefix with does not tell if it's cloning or mutating. There are popular examples of fluent interfaces using a mutating with and some language-specific conventions (e.g. Java) that ...
Christophe's user avatar
  • 82.2k
25 votes

How do I ensure that interface implementations are implemented in the manner I expected?

To complement the other answers, I'd like to partially comment on the following note in the OP by providing a broader context: An interface seems like a good concept, if only I could also specify ...
ComFreek's user avatar
  • 389
25 votes
Accepted

OOP Design considering no modifications to existing design

You could use the Decorator Pattern to add additional responsibilities to an Animal without subclassing. public interface Animal { void eat(); } public class Lion implements Animal { public ...
Tulains Córdova's user avatar
25 votes

In "I don’t want my users knowing that I’m handing them an interface.", why is the severity "I don’t want" instead of "Users don't need to know"?

Are you sure you understood the answer to the question you linked correctly? Because the answers agreed that putting an I in front of interfaces if that is the convention in your language of choice is ...
nvoigt's user avatar
  • 9,230
24 votes
Accepted

How does encapsulation actually work?

Coupling ClassA relies upon the interface only, delegating this responsibility of passing the classB object elsewhere This is the idea. If ...
Theraot's user avatar
  • 9,261
23 votes

How do I ensure that interface implementations are implemented in the manner I expected?

You're trying to design by contract, where that contract is that the return value must be greater than 5. Unfortunately, if you're relying on an interface, the only contract you have is the method ...
Ryan Palmer's user avatar
23 votes
Accepted

Where should interfaces be used?

I don't think there's really a simple answer that can do a substantially better job than the statements of the principles and practices that you've already encountered. I know you're aware of this on ...
Filip Milovanović's user avatar
23 votes
Accepted

Is it Good Practice to Only Expose Interfaces

While this may be an opinion-based question, and the real answer is the same as with many endeavors in software design and development ("it depends"), I'm going to say yes, do not expose ...
Jesse C. Slicer's user avatar
22 votes

In C#, is there a way to enforce behavior coupling in interface methods or is the fact that I am trying to do that a design smell?

State transitions can be represented by separate interfaces per state: public interface IEnabledComponent { IDisabledComponent ToDisabled(); } public interface IDisabledComponent { ...
JacquesB's user avatar
  • 62.3k
22 votes

How does encapsulation actually work?

Your confusion is probably caused by focusing on meaningless class and interface names, with no real usage scenario behind it. So better let us make a concrete example (I prefer C#, but it is not ...
Doc Brown's user avatar
  • 220k
22 votes

OOP Design considering no modifications to existing design

Perfect time for composition. Create a new implementation of Animal that does the counting, but also delegates the "real" function. Like this: public final class LoggingAnimal implements ...
Robert Bräutigam's user avatar
18 votes
Accepted

Java interface for comments only. Good practice?

No, you should not introduce documentation-only interfaces. This removes one of the big advantages of Javadoc: that the docs are right next to the code, which makes it easier to keep them in sync. ...
amon's user avatar
  • 136k

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