General guidelines on how to design classes with best known industry practices.

learn more… | top users | synonyms

49
votes
9answers
3k views

Is there any “real” reason multiple inheritance is hated? [closed]

I've always liked the idea of having multiple inheritance supported in a language. Most often though it's intentionally forgone, and the supposed "replacement" is interfaces. Interfaces simply do ...
44
votes
12answers
3k views

Is it bad practice to pass instances through several layers?

In my program design, I often come to the point where I have to pass object instances through several classes. For example, if I have a controller that loads an audio file, and then passes it to a ...
24
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 ...
19
votes
2answers
1k views

Is it a good idea to provide different function signatures that do the same thing?

Here is a C++ class that gets constructed with three values. class Foo{ //Constructor Foo(std::string, int, char); private: std::string foo; char bar; int baz; }; All of ...
16
votes
5answers
2k views

How can you decompose a constructor?

Lets say I have a Enemy class, and the constructor would look something like: public Enemy(String name, float width, float height, Vector2 position, float speed, int maxHp, int ...
14
votes
9answers
1k views

Isn't class scope purely for organization? [duplicate]

Isn't scope just a way to organize classes, preventing outside code from accessing certain things you don't want accessed? More specifically, is there any functional gain to having public, protected, ...
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 ...
12
votes
4answers
2k views

Is It “Wrong”/Bad Design To Put A Thread/Background Worker In A Class?

I have a class that will read from Excel (C# and .Net 4) and in that class I have a background worker that will load the data from Excel while the UI can remain responsive. My question is as follows: ...
11
votes
9answers
4k 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
4answers
1k views

Object Oriented Programming: getters/setters or logical names

I'm currently thinking about an interface to a class I'm writing. This class contains styles for a character, for example whether the character is bold, italic, underlined, etc. I've been debating ...
11
votes
3answers
3k views

Abstract Base Class with Interfaces as Behaviors?

I need to design a class hierarchy for my C# project. Basically, class' functionalities are similar to WinForms classes so let's take WinForms toolkit as an example. (However, I can't use WinForms or ...
10
votes
2answers
670 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
2answers
292 views

is it a reasonable practice to extend a class just to reuse a single function?

I am developing a range of post filters for a wordpress site, and I have built the first 4 with a single class. The final two are different enough in scope to only share a single function (the ...
9
votes
5answers
1k views

How do we know to favour composition over generalisation is always the right choice?

Whether an object physically exists or not, we can choose to model it in different ways. We could arbitarily use generalisation or composition in many cases. However, the GoF principle of "favour ...
9
votes
4answers
444 views

What would be the best way to store movements on a game to allow a rollback?

I'm developing a board game that has a game class that controls the game flow, and players attached to the game class. The board is just a visual class, but the control of the movements is all by the ...
9
votes
4answers
3k views

C++ - Constructor or Initialize Method to Startup [duplicate]

Possible Duplicate: Avoid having an initialization method I want to determine when to do non-trivial initialization of a class. I see two times to do initialization: constructor and other ...
8
votes
3answers
1k views

Base classes as factories?

I was writing some code over the weekend and I found myself wanting to write a factory as a static method in a base class. My question is simply to know if this is a c# idomatic approach? My sense ...
8
votes
4answers
623 views

Why define a Java object using interface (e.g. Map) rather than implementation (HashMap)

In most Java code, I see people declare Java objects like this: Map<String, String> hashMap = new HashMap<>(); List<String> list = new ArrayList<>(); instead of: ...
8
votes
7answers
725 views

Is it appropriate for a class to only be a collection of information with no logic?

Say I have a class Person that has instance variables age, weight, and height, and another class Fruit that has instance variables sugarContent and texture. The Person class has no methods save ...
7
votes
7answers
2k views

At what point do immutable classes become a burden?

When designing classes to hold your data model I've read it can be useful to create immutable objects but at what point does the burden of constructor parameter lists and deep copies become too much ...
7
votes
5answers
2k views

Can You Have “Empty” Abstract/Classes?

Of course you can, I'm just wondering if it's rational to design in such a way. I'm making a breakout clone and was doing some class design. I wanted to use inheritance, even though I don't have to, ...
7
votes
5answers
2k views

Interface and Inheritance: Best of both worlds?

I 'discovered' interfaces and I started to love them. The beauty of an interface is that it is a contract, and any object that fulfills that contract can be used wherever that interface is required. ...
6
votes
10answers
480 views

Teaching Classes and Objects

I'm trying to teach how an object is just an instance of a class to a buddy of mine. However, he doesn't seem to understand it so well. I've heard a ton of the examples (blueprint to a house, etc.) ...
6
votes
3answers
2k views

Data classes: getters and setters or different method design

I've been trying to design an interface for a data class I'm writing. This class stores styles for characters, for example whether the character is bold, italic or underlined. But also the font-size ...
6
votes
6answers
644 views

Organizing Class Members in Regards to Access Modifier

If we look at typical implementation of a Class, we usually see the private members defined at the beginning and public( mostly functions and Accessors) defined towards the bottom. Now, is this a ...
6
votes
5answers
1k views

Prefer class members or passing arguments between internal methods?

Suppose within the private portion of a class there is a value which is utilized by multiple private methods. Do people prefer having this defined as a member variable for the class or passing it as ...
6
votes
3answers
158 views

Grouping conceptual algorithms into one class or associate with their object models

If I have a number of classes, lets say a variety of buildings of some sort. Each of these buildings have various properties and do some stuff etc Now there is a set of operations I need to do on ...
6
votes
4answers
3k views

Use Dependency Injection For Data Objects?

I'm just learning about dependency injection, and am stuck on something. Dependency Injection recommends sending dependent classes through the constructor, but I'm wondering if this is necessary for ...
5
votes
4answers
3k views

Is it a good practice to create a ClassCollection of another Class?

Lets says I have a Carclass: public class Car { public string Engine { get; set; } public string Seat { get; set; } public string Tires { get; set; } } Lets say we're making a system ...
5
votes
4answers
494 views

Method flags as arguments or as member variables?

I think the title "Method flags as arguments or as member variables?" may be suboptimal, but as I'm missing any better terminology atm., here goes: I'm currently trying to get my head around the ...
5
votes
3answers
3k views

Can a class be inside another class?

Here's an excerpt from "The C++ Programming Language" template<classT> class List { // optimal public: class Link { /* ... */ }; List (); // initially empty void put(T *); // put before current ...
5
votes
7answers
2k views

Where should I put my utility methods?

I am very new to Visual Studio and C# and am wondering how best to create a repository of sorts for utility methods. For instance, we need a method that returns the current fiscal year and other ...
5
votes
4answers
260 views

Best way to handle class relationship

Take a User class and the idea that a User is to be profiled. I see four ways to handle this: Write the code for the profile into the User class. I am dismissing this right away. Create a Profile ...
5
votes
3answers
211 views

How to name an subclass that add a minor, detailed thing?

What is the most concise (yet descriptive) way of naming a subclass that only add a specific minor thing to the parent? I encountered this case a lot in WPF, where sometime I have to add a small ...
5
votes
1answer
165 views

How to design an algorithm-heavy and object-light application

Other than vertices, edges, faces and meshes, my geometry processing project/application has only about half a dozen other entities such as curves on surfaces. However the majority of my code is ...
5
votes
2answers
442 views

Should exceptions of a subclass extend the superclass exceptions or my own namespace?

Our library extends another (third-party) library. When we create a child class in our library, and want to throw exceptions, should those exceptions extend the exceptions from the parent class or ...
4
votes
6answers
291 views

Fields vs method arguments [closed]

I just started writing some new class and it occurred to me that I was adding a lot of method arguments that are not strictly needed. This is following a habit to avoid having state in classes that is ...
4
votes
3answers
413 views

Why is it so difficult to know where to use interfaces,generics in program design?

I am attempting to learn c# from the head first series, in addition i also consult other books such as Pro C# by Andrew Tolson. Now the thing is that i perfectly understand the interface,generics ...
4
votes
3answers
158 views

What class structure allows for a base class and mix/match of subclasses? (Similar to Users w/ roles)

I have a set of base characteristics, and then a number of sub-types. Each instance must be one of the sub-types, but can be multiple sub-types at once. The sub-types of each thing can change. In ...
3
votes
3answers
529 views

How bad is it to have two methods with the same name but different signatures in two classes?

I have a design problem related to a public interface, the names of methods, and the understanding of my API and code. I have two classes like this: class A: ... function collision(self): ...
3
votes
6answers
3k views

How to properly design classes for a big project?

If we need to represent classes in a class diagram for a big project that is not completely designed yet, and the classes have to be actual tables in a database, how would we predict and design the ...
3
votes
5answers
814 views

What classes to put exactly in a class diagram?

What classes must I put in a class diagram? Only classes used in Business Layer and associations between them? Or also other classes in the Data Access Layer, Service Layer, etc.?
3
votes
5answers
230 views

How to decide which classes are necessary for the system

I know design principles and design patterns in theory. But it's embarrassing to realize that it's not clear when applying it in practice. For example, let's think about simple tic tac toe console ...
3
votes
3answers
274 views

Proper use of classes

I'm writing a class that draws a very complex image, with multiple parts. I also plan on adding to the class to add more functionality. Would it be right to make a function for each part, even if that ...
3
votes
3answers
309 views

Design: Method that loads data from one of two file formats?

Say I have a class Person: class Person { string FullName; int Age; } And I need to load a list of Person's from a plain text file. The text file could be in one of two formats. I want a ...
3
votes
3answers
155 views

Class fields/variables. Keep single reference point at class level or pass to individual methods?

Often I see a class where a value is injected into a method or even a constructor. That value is then used by several methods within that class and a reference is simply passed through to each method ...
3
votes
3answers
344 views

Is partial classes modern Subject-Oriented programming?

Important discussion follows: C# and other language have made partial classes more popular. But isn't this really a tool that have reinvented the subject-orientated programming wheel? What do you ...
3
votes
2answers
227 views

Designing class methods: self contained or explicit calls?

I'm probably butchering the terms, if someone knows the appropriate terms - that'd be great. But my question is: should I design my classes so that their methods' parameters be accessible from the ...
3
votes
2answers
561 views

How to deal with pointers from child to parent?

I have a class that represents a file in specific binary format on disk (the parent in the title) and another class that represents an object inside that file (child). When the object changes, it ...
3
votes
1answer
108 views

Naming a class that processes orders

I'm in the midst of refactoring a project. I've recently read Clean Code, and want to heed some of the advice within, with particular interest in Single Responsibility Principle (SRP). Currently, ...