348 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 the ...
user avatar
  • 8,640
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 ...
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 ...
user avatar
  • 35.5k
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 ...
user avatar
  • 1,000
89 votes
Accepted

Better to have 2 methods with clear meaning, or just 1 dual use method?

You seem to suggest that the complexity of an interface is measured by the number of elements it has (methods, in this case). Many would argue that having to remember that the charge method can be ...
user avatar
79 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 ...
user avatar
  • 888
79 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 ...
user avatar
  • 1,642
78 votes
Accepted

Why does C# allow properties in interfaces?

I think the confusing part is that if you write int Property { get; set; } inside a class, then it's an auto-property with implicit backing field. But if you write exactly the same thing in an ...
user avatar
  • 9,911
78 votes
Accepted

If I implement an Interface, is it called an Inheritance?

UPDATE: I've revised this answer. A number of good points were raised in the comments that deserved calling out. If my class implements an interface then can I say that I'm following inheritance? ...
user avatar
  • 45.2k
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 ...
user avatar
  • 64.6k
66 votes

Do I need to use an interface when only one class will ever implement it?

No, you don't need them, and I consider it an anti-pattern to automatically make interfaces for every class reference. There is a real cost to making Foo/FooImpl for everything. The IDE may create ...
user avatar
  • 1,099
62 votes
Accepted

Programming for future use of interfaces

Invite him to learn about YAGNI. The Rationale part of Wikipedia page may be particularly interesting here: According to those who advocate the YAGNI approach, the temptation to write code that is ...
user avatar
57 votes
Accepted

Implementing an interface when you don't need one of the properties

This is a classical example of how people decide to violate the Liskov Subtitution Principle. I strongly discourage it but would encourage possibly a different solution: Perhaps the class you're ...
user avatar
  • 15.9k
53 votes

Why were default and static methods added to interfaces in Java 8 when we already had abstract classes?

The correct answer is in fact found in the Java Documentation, which states: [d]efault methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility ...
user avatar
  • 759
52 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 ...
user avatar
48 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....
user avatar
  • 9,804
45 votes

Interface design where functions need to be called in a specific sequence

It's a redesign but you can prevent misuse of many APIs but not having available any method that shouldn't be called. For example, instead of first you init, then you start, then you stop Your ...
user avatar
  • 1,600
42 votes

Should a front-end developer ever specify JSON format for back-end developers?

This is a conversation you should be having together, discussing the requirements and pros and cons of different formats. If one side or the other is dictating what happens, you're going to end up ...
user avatar
  • 2,079
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; ...
user avatar
39 votes
Accepted

Is it permissible to use explicit interface implementation to hide members in C#?

Yes, it's generally bad form. Thus, I thought it would make sense to hide them away from an implementing class via explicit implementation to avoid IntelliSense clutter. If your IntelliSense is ...
user avatar
  • 107k
39 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 ...
user avatar
  • 124k
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 ...
user avatar
39 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 ...
user avatar
  • 185k
37 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 ...
user avatar
  • 22.3k
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 ...
user avatar
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 ...
user avatar
  • 469
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 ...
user avatar
  • 2,501
33 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 ...
user avatar

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