Tagged Questions

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

learn more… | top users | synonyms

1
vote
1answer
95 views

When should I pass value as class variable and when as a method argument?

Is there a general rule of thumb, when we should pass a value as as class variable and when as a method argument? Or is it just a choice of the developer? For example -- are there any reasons, why ...
0
votes
1answer
104 views

How can I layout this class to make it easier to use for API developers?

I have an application that allows the user to place a string of text into a PDF document. I have three different ways that they can do this: Use a Form Field. Then they have four properties to ...
1
vote
4answers
685 views

Call the database every time, or create an object in memory, and if so, how and how often to update?

I'm creating an app in javascript/nodejs and using neo4j as database. Let's say I have an entity User in my database. It has a bunch of properties, like boxes_collected and places_visited, which keep ...
1
vote
1answer
353 views

A class with only methods

What's the name for a class that has only methods? There are no fields/properties. Just two methods with the ability to parse some file in two ways. I have named the class Parser, but it just doesn't ...
9
votes
4answers
2k 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: ...
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 ...
2
votes
1answer
317 views

C# dynamically linking different libraries and/or external classes

tl;dr Is it possible to include an external library (and create instances of objects represented within that library) based on some condition? This must be done at compile-time, so separate builds? ...
1
vote
2answers
197 views

An alternative to having fields that might not be used in a class

I'm writing a card/ board game in Java. Since the game has lots of cards that interact in different ways, my Player class has become sort of bloated with all these different fields used for keeping ...
2
votes
1answer
60 views

what's the proper way to organize code that straddles multiple objects?

Let's say I have the following classes: person, widget, foo_bars. A person can have multiple widgets and foo_bars. When an admin deletes a person, I want my code to do a cascading delete... and ...
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 ...
0
votes
3answers
359 views

PHP class data implementation

I'm studying OOP PHP and have watched two tutorials that implement user login\registration system as an example. But implementation varies. Which way will be more correct one to work with data such as ...
68
votes
9answers
5k views

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

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 ...
0
votes
2answers
329 views

OOP PHP make separate classes or one

I'm studying OOP PHP and working on a small personal project but I have hard time grasping some concepts. Let's say I have a list of items, each item belongs to subcategory, and each subcategory ...
1
vote
3answers
462 views

C# vector class - Interpolation design decision

Currently I'm working on a vector class in C# and now I'm coming to the point, where I've to figure out, how i want to implement the functions for interpolation between two vectors. At first I came up ...
0
votes
2answers
184 views

Subclassing to avoid line length

The standard line length of code is 80 characters per line. This is accepted and followed by the most of programmers. I working on a state machine of a character and is necessary for me follow this ...
3
votes
3answers
590 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): ...
2
votes
2answers
326 views

Class design for calling “the same method” on different classes from one place

Let me introduce my situation: I have Java EE application and in one package, I want to have classes which will act primarily as cache for some data from database, for example: class that will hold ...
0
votes
2answers
1k views

How to handle lookup data in a C# ASP.Net MVC4 application?

I am writing an MVC4 application to track documents we have on file for our clients. I'm using code first, and have created models for my objects (Company, Document, etc...). I am now faced with the ...
3
votes
2answers
958 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 ...
6
votes
6answers
335 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 ...
1
vote
1answer
161 views

Class Design for special business rules

I'm developing an application that allows people to place custom manufacturing orders. However, while most require similar paperwork, some of them have custom paperwork that only they require. My ...
0
votes
1answer
252 views

Code design: is this specific case of monkeypatching in python acceptable?

I'm using python to do some research tasks. I have a class hierarchy for "tools", where each object is an instance of a particular tool. They all share some functionality and have many similarities in ...
0
votes
2answers
156 views

Is it valid to initialize an instance of a class within the same class?

I was wondering if it's valid to initialize an instance of a class within the same class? For example: public class Person() { string name; string age; public Person getPerson() { ...
0
votes
3answers
747 views

Have Superclass Contain List of Subclass?

For the GUI of a program, I want it to list several items, all of which are, from a programming side, just subclasses. They can add one of these items to a list. I don't want to hard-code which ...
0
votes
3answers
3k views

What is good practice when inheriting static classes isn't possible/allowed

I am developing a project in C# and due to a design decision it is not possible to have a static class inherit another static class. But I have, in my opinion, a case where this would make sense. I ...
2
votes
5answers
603 views

Why is TDD not working here?

I want to write a class A that has a method calculate(<params>). That method should calculate a value using database data. So I wrote a class Test_A for unit testing (TDD). The database access ...
1
vote
1answer
156 views

Is it evil to model JSON responses to classes when they are mostly smilar?

Here's the problem : While implementing a C# wrapper for an online API (Discogs) I've been faced to a dilemma : quite often the responses returned have mostly similar members and while modeling these ...
0
votes
3answers
177 views

which style of member-access is preferable [duplicate]

the purpose of oop using classes is to encapsulate members from the outer space. i always read that accessing members should be done by methods. for example: template<typename T> class foo_1 { ...
2
votes
2answers
2k views

When to use inheritance or composition/aggregation?

In general, how do I decide whether to use make a class a super class, or to make it a private data member of another class? For example, given two classes, how does one decide whether to do this: ...
0
votes
1answer
119 views

Refactoring classes with ref to themselves

How can I refactor this code? class Node { public Node Parent { get; set; } } class AVLNode { public AVLNode Parent { get; set; } } I tried to use inheritance, but then I have to use type ...
0
votes
3answers
221 views

How do you balance out code structuring (few big functions vs. many small ones)?

The golden rule of code structuring is always said as splitting into many sub functions is a good thing. Though I noticed it becomes a problem in complex applications when a class of e.g. 10 bigger ...
2
votes
2answers
710 views

Same constructor signature with different semantics?

Occasionally, I'm confronted with the problem that I have to provide some constructors in order to initialize objects with different sets of data. These sets can be mathematically transformed into ...
1
vote
2answers
110 views

When to store values in constants/finals

This might seem like an odd question, but I'm worried that I'm putting too many things as constants/finals at the top of my java class. I've started to put every value that is in my program into a ...
0
votes
2answers
223 views

What is the convention regarding class names that may already exist in the ORM?

This is a generic question about class names and ORMs, but for context; I'm working on a Flask web app in Python using SQLalchemy. I'm struggling with naming a new class that will contain the ...
9
votes
4answers
7k 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 ...
3
votes
5answers
1k 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.?
8
votes
4answers
483 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 ...
0
votes
1answer
368 views

How can my code structure be improved? [closed]

I am looking for advice on the neatest way to structure my code. class Password { private string cipher; Password(string cipher) { this.cipher = cipher; } public string ...
4
votes
3answers
437 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 ...
1
vote
1answer
216 views

Designing a list class with filtering and sorting

The app I'm developing needs to display lists of items. Simple enough, but there are a number of things which can change based on user input: Items can be added to/removed from the list. The items ...
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 ...
3
votes
3answers
346 views

Architecting persistence (and other internal systems). Interfaces, composition, pure inheritance or centralization?

Suppose that you need to implement persistence, I think that you're generally limited to four options (correct me if I'm wrong, please) Each persistant class: Should implement an interface ...
8
votes
7answers
739 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 ...
0
votes
3answers
591 views

UML - Class Diagrams Order -> Products

I have a class diagram that is like this: < Order > (1) CAN HAVE (M) < products > But therefore Order has the following: Order_Id Customer_Id Order_date_day Order_date_month ...
0
votes
1answer
1k views

Class Design and Structure Online Web Store

I hope I have asked this in the right forum. Basically, we're designing an Online Store and I am designing the class structure for ordering a product and want some clarification on what I have so ...
5
votes
3answers
234 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 ...
10
votes
4answers
5k 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 ...
2
votes
2answers
265 views

Strategies for invoking subclass methods on generic objects [duplicate]

Possible Duplicate: Alternatives to type casting in your domain I've run into this issue in a number of places and have solved it a bunch of different ways but looking for other solutions ...
2
votes
2answers
3k views

DB Object passing between classes singleton, static or other?

So I'm designing a reporting system at work it's my first project written OOP and I'm stuck on the design choice for the DB class. Obviously I only want to create one instance of the DB class ...
14
votes
9answers
2k 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, ...