OOP: When & Whether to Construct a New Class?

  • Thread starter Red_CCF
  • Start date
  • Tags
    Oop
In summary, classes suggest themselves when you start to see related tasks being handled by different parts of your program, and when you find yourself passing long lists of parameters to subroutines.
  • #1
Red_CCF
532
0
Hi

I have a question, how does one know when and/or whether to construct a new class or not? What are some indications that one should create a class to bunch all the functions/variables together?

Thanks.
 
Technology news on Phys.org
  • #2
I have no formal training in these things, but I do have about 10 years of experience in programming in which I did some OOP, so my point of view is a very practical one.

Most of the time, to me, classes logically suggest themselves. If you are writing a board game, you will start with a Game class, that will most likely contain a Board class which manages all the Fields, and the Pieces. If you are writing a simulation for an experiment, you would think about some Objects in Space, and probably some Manager class which takes care of the interactions and listing of all the objects and perhaps a set of Output classes which know how to draw the objects (e.g. on screen, on a printer, to a text file).

If you think about your program as a sort of machine in which every small part has its own task, and only the relevant data is handed from part to part to be processed in some way which other parts don't need to know about, classes often present themselves. If you find yourself writing some related functions (like SaveToFile, LoadFromFile, PrintToScreen, PrintToPrinter) with internal / local variables (filename, file_exists, file_is_open) a class (FileHandler) might be a good idea.

Also, I try to keep my classes small. A declaration should fit on one computer screen, and you should not need to copy/paste/modify code.
 
  • #3
If you find yourself passing a long and annoying list of parameters to a subroutine, then you really ought to be using an object instead. Most of those parameters can just live inside the object.
 

Related to OOP: When & Whether to Construct a New Class?

1. What is object-oriented programming (OOP)?

Object-oriented programming is a programming paradigm that focuses on creating objects, which are data structures that have properties and methods. These objects are used to model real-world entities and their interactions.

2. When should I construct a new class in OOP?

A new class should be constructed when you need to create a new type of object that is not already defined in your code. This allows for better organization and encapsulation of data and functionality.

3. What are the benefits of using classes in OOP?

Classes provide a number of benefits, including encapsulation, inheritance, and polymorphism. Encapsulation helps to organize code and prevent data from being accessed or modified in unintended ways. Inheritance allows for code reuse and easier maintenance. Polymorphism enables objects to have different forms or behaviors depending on the context in which they are used.

4. How do I determine whether to construct a new class or use an existing one?

The decision to construct a new class or use an existing one depends on the specific needs and goals of your program. If an existing class closely matches the functionality you need, it is generally better to use it. However, if you need a unique set of properties and methods, creating a new class may be necessary.

5. Are there any potential drawbacks to constructing a new class?

One potential drawback to constructing a new class is the added complexity it can introduce to your code. It may also require more time and effort to design and implement a new class. Additionally, creating too many classes can lead to a bloated and difficult-to-maintain codebase.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
953
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
474
  • Programming and Computer Science
Replies
5
Views
796
  • Programming and Computer Science
2
Replies
36
Views
2K
Back
Top