Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.

learn more… | top users | synonyms

73
votes
13answers
7k views

Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?

If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design? I have heard people say: If you made Square derive from Rectangle, then a Square ...
72
votes
7answers
6k views

Is this a violation of the Liskov Substitution Principle?

Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure ...
64
votes
17answers
13k views

Is it wrong to use a boolean parameter to determine behavior?

I have seen a practice from time to time that "feels" wrong, but I can't quite articulate what is wrong about it. Or maybe it's just my prejudice. Here goes: A developer defines a method with a ...
58
votes
16answers
22k views

Is it better to return NULL or empty values from functions/methods where the return value is not present?

I am looking for a recommendation here. I am struggling with whether it is better to return NULL or an empty value from a method when the return value is not present or cannot be determined. Take ...
51
votes
10answers
3k views

What is meant by, “A user shouldn't decide whether it is an Admin or not. The Privileges or Security system should.”

The example used in the question pass bare minimum data to a function touches on the best way to determine whether the user is an administrator or not. One common answer was: user.isAdmin() This ...
51
votes
10answers
3k views

My proposed design is usually worse than my colleague's - how do I get better? [closed]

I have been programming for couple of years and am generally good when it comes to fixing problems and creating small-to-medium scripts, however, I'm generally not good at designing large scale ...
51
votes
6answers
4k views

What really is the “business logic”?

I'm working with web development since 2009, when I started with PHP. When I moved to ASP.NET I've heard a lot about DDD and OOAD where a lot of focus is given to this "business logic" and "business ...
39
votes
9answers
4k views

Programming for future use of interfaces

I have a colleague sitting next to me who designed an interface like this: public interface IEventGetter { public List<FooType> getFooList(String fooName, Date start, Date end) ...
36
votes
5answers
4k views

Passing functions into other functions as parameters, bad practice?

We've been in the process of changing how our AS3 application talks to our back end and we're in the process of implementing a REST system to replace our old one. Sadly the developer who started the ...
33
votes
8answers
3k views

Is OOP becoming easier or harder?

When the concepts of Object Oriented Programming were introduced to programmers years back it looks interesting and programming was cleaner. OOP was like this Stock stock = new Stock(); ...
28
votes
3answers
2k views

Is a “start”, “run” or “execute” method a good practice?

I'm currently working on a code base that has many classes that implement a Start method. This seems like two-phase construction to me, which I had always considered a bad practice. I can't tell the ...
27
votes
3answers
4k views

Rich Domain Models — how, exactly, does behavior fit in?

In the debate of Rich vs. Anemic domain models, the internet is full of philosophical advice but short on authoritative examples. The objective of this question is to find definitive guidelines and ...
25
votes
10answers
6k views

Can we live without constructors?

Let's say in some reason all objects are created this way $obj = CLASS::getInstance(). Then we inject dependencies using setters and perform starting initialization using $obj->initInstance(); Are ...
25
votes
7answers
2k views

Are classes with only a single method a problem?

I am currently working on a software project that performs compression and indexing on video surveillance footage. The compression works by splitting background and foreground objects, then saving the ...
23
votes
10answers
2k views

How would Functional Programming proponents answer this statement in Code Complete?

On page 839 of the second edition, Steve McConnell is discussing all the ways that programmers can "conquer complexity" in big programs. His tips culminate with this statement: "Object-oriented ...
22
votes
17answers
2k views

What is the value in hiding the details through abstractions? Isn't there value in transparency?

Background I am not a big fan of abstraction. I will admit that one can benefit from adaptability, portability and re-usability of interfaces etc. There is real benefit there, and I don't wish to ...
21
votes
5answers
2k views

Do unit tests sometimes break encapsulation? [duplicate]

I very often hear the following: "If you want to test private methods, you'd better put that in another class and expose it." While sometimes that's the case and we have a hiding concept inside our ...
20
votes
3answers
11k views

Where to put business logic in MVC design?

I have created a simple MVC Java application that adds records through data forms to a database. My app collects data, it also validates it and stores it. This is because the data is being sourced ...
20
votes
7answers
7k views

Why avoid Java Inheritance “Extends”

Good day! Jame Gosling said “You should avoid implementation inheritance whenever possible.” and instead, use interface inheritance. But why? How can we avoid inheriting the structure of an object ...
19
votes
6answers
912 views

Should an object know its own ID?

obj.id seems fairly common and also seems to fall within the range of something an object could know about itself. I find myself asking why should my object know its own id? It doesn't seem to have a ...
18
votes
7answers
1k views

Does functional programming ignore the benefits gained from the “On the Criteria To Be Used in Decomposing Systems into Modules” (data hiding)?

There's a classic article named On the Criteria To Be Used in Decomposing Systems into Modules that I just read for the first time. It makes perfect sense to me, and is probably one of those articles ...
18
votes
8answers
2k views

What can go wrong if the Liskov substitution principle is violated?

I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what ...
17
votes
11answers
2k views

When would you want two references to the same object?

In Java specifically, but likely in other languages as well: when would it be useful to have two references to the same object? Example: Dog a = new Dog(); Dob b = a; Is there a situation where ...
17
votes
3answers
14k views

Best design for Windows forms that will share common functionality

In the past, I have used inheritance to allow the extension of Windows forms in my application. If all of my forms would have common controls, artwork, and functionality, I would create a base form ...
16
votes
10answers
909 views

Object Oriented Design

Suppose you have the following: +--------+ +------+ | Animal | | Food | +-+------+ +----+-+ ^ ^ | | | | ...
16
votes
3answers
2k views

Using public final rather than private getters

I see most immutable POJOs written like this: public class MyObject { private final String foo; private final int bar; public MyObject(String foo, int bar) { this.foo = foo; ...
15
votes
5answers
869 views

How are mock objects commonly misused?

I read an article recently which said that mock objects are often misunderstood and misused. Are there any clear mocking anti-patterns which I can look out for?
15
votes
6answers
1k views

Does multiple inheritance violate Single Responsibility Principle?

If you have a class which inherits from two distinct classes, does not this mean that your subclass automatically does (at least) 2 things, one from each superclass? I believe there is no difference ...
14
votes
6answers
1k views

Loose Coupling in Object Oriented Design

I am trying to learn GRASP and I found this explained (here on page 3) about Low Coupling and I was very surprised when I found this: Consider the method addTrack for an Album class, two possible ...
13
votes
6answers
2k views

Should a class know about its subclasses?

Should a class know about its subclasses? Should a class do something that is specific for a given subclass for instance? My instincts tells me that is a bad design, it seems like an anti-pattern of ...
13
votes
5answers
854 views

Is SRP (Single Responsibility Principle) objective?

Consider two UI designers who want to design "user attractive" designs. "User attraction" is a concept that is not objective and only resides in the mind of designers. Thus designer A could for ...
13
votes
4answers
678 views

Switch vs Polymorphism when dealing with model and view

I can't figure out a better solution to my problem. I have a view controller that presents a list of elements. Those elements are models that can be an instance of B, C, D, etc and inherit from A. So ...
12
votes
7answers
3k views

OO best practices for C programs

"If you really want OO sugar - go use C++" -- was the immediate response I got from one of my friends when I asked this. I know two things are dead wrong here. First OO is NOT 'sugar', and second, C++ ...
12
votes
9answers
5k views

When should you use a private/inner class?

To clarify, what I'm asking about is public class A{ private/*or public*/ B b; } vs. public class A{ private/*or public*/ class B{ .... } } I can definitely think of some ...
11
votes
3answers
1k views

How to create better OO code in a relational database driven application where the database is poorly designed

I am writing a Java web application that consists mainly of a bunch of similar pages in which every page has several tables and a filter that applies to those tables. The data on these tables comes ...
11
votes
7answers
2k views

From a design perspective, what are the best practices for logging? [closed]

I want to add logging to an application I'm currently working on. I've added logging before, that's not an issue here. But from a design perspective in an object-oriented language, what are the best ...
10
votes
7answers
3k views

Interfaces on an abstract class

My coworker and I have different opinions on the relationship between base classes and interfaces. I'm of the belief that a class should not implement an interface unless that class can be used when ...
10
votes
6answers
6k views

What are the differences between abstract classes, interfaces, and when to use them

Recently I have started to wrap my head around OOP, and I am now to the point where the more I read about the differences between abstract classes and interfaces the more confused I become. So far, ...
10
votes
7answers
2k views

What are the features required for Object Orientation?

I am just wondering, what exactly are the features a language or a library must provide in order for it to be defined as 'Object Oriented'. Is Object Orientation something that can, more or less, be ...
10
votes
9answers
2k views

SOLID vs. static methods

Here's a problem I frequently run into: Let there be a web shop project that has a Product class. I want to add a feature which allows users to post reviews to a product. So I have a Review class ...
10
votes
2answers
705 views

Design: Object method vs separate class's method which takes Object as parameter?

For example, is it better to do: Pdf pdf = new Pdf(); pdf.Print(); or: Pdf pdf = new Pdf(); PdfPrinter printer = new PdfPrinter(); printer.Print(pdf); Another example: Country m = new ...
9
votes
3answers
645 views

Do I suffer from encapsulation overuse?

I have noticed something in my code in various projects that seems like code smell to me and something bad to do, but I can't deal with it. While trying to write "clean code" I tend to over-use ...
9
votes
4answers
607 views

Good implementation strategies for encapsulating shared data in a software pipeline

I'm working on re-factoring certain aspects of an existing web service. The way the service APIs are implemented is by having a kind of "processing pipeline", where there are tasks that are performed ...
8
votes
4answers
909 views

Is the List interface a leaky abstraction?

If I have a variable containing a List it could contain objects of a lot of different types e.g. ArrayList or LinkedList. The difference between a LinkedList and an ArrayList is pretty big. The big O ...
8
votes
10answers
822 views

What should be first - functionality or design? [duplicate]

I've started reading a book from Head First series about OOP and Design. In a first chapter it is stated I have to worry about design of my application just after basic functionality is ready. Basic ...
8
votes
2answers
561 views

How to create the architecture/design of an application in Agile?

If I am about to develop an Enterprise application, but as far as I understand from agile process, I break features into small chunks and develop them iteratively. I used to create the database and ...
8
votes
7answers
6k views

OO Design related questions in technical interviews

I've been attending quite a few interviews recently and have been asked by companies to answer "design a [insert model]" questions more than a few times. Is this normal in the industry nowadays? ...
8
votes
4answers
680 views

Design in “mixed” languages: object oriented design or functional programming?

In the past few years, the languages I like to use are becoming more and more "functional". I now use languages that are a sort of "hybrid": C#, F#, Scala. I like to design my application using ...
8
votes
2answers
296 views

When following SRP, how should I deal with validating and saving entities?

I've been reading Clean Code and various online articles about SOLID lately, and the more I read about it, the more I feel like I don't know anything. Let's say I'm building a web application using ...
7
votes
11answers
2k views

What is the point of properties?

Here are some arguments for properties and my counter-arguments: Easier to use than writing getter and setter methods Getter and setter method pairs are a code smell. Making it easier to write ...