Refers to structural definition of class unit in object-oriented languages.
Questions tagged [class-design]
1125 questions
48
votes
5 answers
python circular imports once again (aka what's wrong with this design)
Let's consider python (3.x) scripts:
main.py:
from test.team import team
from test.user import user
if __name__ == '__main__':
u = user()
t = team()
u.setTeam(t)
t.setLeader(u)
test/user.py:
from test.team import team
class user:
…

ts.
- 10,510
- 7
- 47
- 73
47
votes
6 answers
static const Member Value vs. Member enum : Which Method is Better & Why?
If you want to associate some constant value with a class, here are two ways to accomplish the same goal:
class Foo
{
public:
static const size_t Life = 42;
};
class Bar
{
public:
enum {Life = 42};
};
Syntactically and semantically they…

John Dibling
- 99,718
- 31
- 186
- 324
46
votes
13 answers
How would you code an efficient Circular Buffer in Java or C#?
I want a simple class that implements a fixed-size circular buffer. It should be efficient, easy on the eyes, generically typed.
For now it need not be MT-capable. I can always add a lock later, it won't be high-concurrency in any case.
Methods…

Cheeso
- 189,189
- 101
- 473
- 713
46
votes
11 answers
Which class design is better?
Which class design is better and why?
public class User
{
public String UserName;
public String Password;
public String FirstName;
public String LastName;
}
public class Employee : User
{
public String EmployeeId;
public…

Ramesh Soni
- 15,867
- 28
- 93
- 113
45
votes
18 answers
List or BusinessObjectCollection?
Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable
IE:
public class CollectionBase : IEnumerable
and then would derive their Business Object collections from…

FlySwat
- 172,459
- 74
- 246
- 311
43
votes
4 answers
why is java.lang.Throwable a class?
In java adjectives ending in -able are interfaces Serializable, Comparable etc... So why is Throwable a class? Wouldn't exception handling be easier if Throwable were an interface? (Edit: e.g. Exception classes don't need to extend…

mdma
- 56,943
- 12
- 94
- 128
43
votes
7 answers
OO Javascript constructor pattern: neo-classical vs prototypal
I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes
were opened. At one point he said, something like, "Javascript is the only language where good programmers believe they can use it effectively, without learning it." …

Cheeso
- 189,189
- 101
- 473
- 713
41
votes
3 answers
Using a class' __new__ method as a Factory: __init__ gets called twice
I encountered a strange bug in python where using the __new__ method of a class as a factory would lead to the __init__ method of the instantiated class to be called twice.
The idea was originally to use the __new__ method of the mother class to…

xApple
- 6,150
- 9
- 48
- 49
41
votes
7 answers
Large scale usage of Meyer's advice to prefer Non-member,non-friend functions?
For some time I've been designing my class interfaces to be minimal, preferring namespace-wrapped non-member functions over member functions. Essentially following Scott Meyer's advice in the article How Non-Member Functions Improve Encapsulation. …

ergosys
- 47,835
- 5
- 49
- 70
40
votes
11 answers
Why put private fields and methods at the top of class?
I've seen this de facto standard in many places in many languages, but I've never understood it - why put your private fields and methods at the top of a class declaration? Metaphorically it seems like private things should be located at the bottom…

JimDaniel
- 12,513
- 8
- 61
- 67
40
votes
2 answers
Why is ASP.NET Core's Startup class not an interface or abstract class?
This is in regards to the design principals behind the Startup class explained here:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1
I understand that the class needs to include methods like ConfigureServices…

Allan Xu
- 7,998
- 11
- 51
- 122
40
votes
10 answers
Classes to avoid (code complete)
I am somewhat confused about a paragraph in the code complete book.
In the section "Classes to avoid" it reads:
"Avoid classes named after verbs A class that has only behavior but no data is generally not really a class. Consider turning a class…

adrianm
- 14,468
- 5
- 55
- 102
37
votes
7 answers
Why is 16 byte the recommended size for struct in C#?
I read the Cwalina book (recommendations on development and design of .NET applications).
He says that a good designed struct has to be less than 16 bytes in size (for performance purposes).
Why exactly is this?
And (more important) can I have…
please delete me
34
votes
7 answers
C# generics - Can I make T be from one of two choices?
Suppose I have the following class hierarchy:
Class A {...}
Class B : A {...}
Class C : A {...}
What I currently have is
Class D where T : A {...}
but I'd like something of the form
Class D where T in {B,C}
This is due to some odd…

Jean-Bernard Pellerin
- 12,556
- 10
- 57
- 79
34
votes
11 answers
OOP: Which class should own a method?
I’m having trouble understanding how classes relate to their methods. Is a method something that the object does, or something that’s done to it? Or is this a different concept entirely?
Specifically, in a library’s software system, should the…

Frungi
- 506
- 5
- 16