350
votes
Accepted
I changed one method signature and now have over 25,000 errors. What now?
25000 errors basically means "don't touch that". Change it back. Create a new class that has the desired interface and slowly move the consumers of the class to the new one. Depending on ...
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 ...
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 ...
130
votes
What is meant by the phrase “Software can replace hardware”?
I am surprised nobody mentioned yet one of the most glaring examples: software-defined radio.
If you took a present-day smartphone back in time some 50 years and showed it to a competent engineer ...
81
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 ...
80
votes
I changed one method signature and now have over 25,000 errors. What now?
Divide and conquer with refactorings
Often, breaking up the change that you need to do into smaller steps can help because you can then perform most of the smaller steps in a way that doesn't break ...
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 ...
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 ...
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 ...
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....
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 ...
42
votes
What is meant by the phrase “Software can replace hardware”?
Consider this circuit:
It is a Flip Flop, aka a Bistable Multivibrator. It can be replaced with this code:
static bool toggle;
if (toggle == true)
{
lblTop.BackColor = Color.Black;
...
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 ...
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 ...
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 ...
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 ...
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 ...
36
votes
I changed one method signature and now have over 25,000 errors. What now?
Clarify your task with your boss to help him to understand the problem and your needs as a professional software developer.
If you are part of a team, look for the lead developer and ask him for ...
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 ...
33
votes
Accepted
Is this bad OOP design for a simulation involving interfaces?
In general you want to have interfaces for common characteristics of your clasess.
I semi-agree with @Robert Harvey in the comments, who said that usually interfaces represent more abstract features ...
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 : ...
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....
32
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 ...
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 ...
29
votes
Is this bad OOP design for a simulation involving interfaces?
Looks like you're creating a bunch of single method interfaces. This is fine on the face of it but keep in mind that interfaces are not owned by the class/es that implement them. They are owned by ...
29
votes
I changed one method signature and now have over 25,000 errors. What now?
Don't touch it. Don't commit anything.
Instead, sit down on your chair and shout "Heeeeelp!!!!!" as loud as you can.
Well, not exactly like that, but ask any of your senior colleagues for advice. ...
28
votes
What is meant by the phrase “Software can replace hardware”?
It means exactly what it sounds like.
A particularly famous example is the Disk II Drive designed by Steve Wozniak for the Apple II:
The chief innovation was making the controller compact by using ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
interfaces × 735java × 153
object-oriented × 152
c# × 150
design × 105
design-patterns × 95
object-oriented-design × 72
inheritance × 69
c++ × 54
abstract-class × 44
solid × 34
architecture × 28
php × 24
dependency-injection × 24
abstraction × 20
implementations × 20
polymorphism × 19
programming-practices × 18
api-design × 17
class × 17
generics × 17
naming × 15
.net × 14
api × 14
c × 13