OOP: When & Whether to Construct a New Class?

  • Thread starter Thread starter Red_CCF
  • Start date Start date
  • Tags Tags
    Oop
Click For Summary
SUMMARY

This discussion focuses on the practical considerations for constructing new classes in Object-Oriented Programming (OOP). Key indicators for creating a class include the need to group related functions and variables, such as in a Game class that contains Board and Piece classes, or a FileHandler class for file operations. The author emphasizes the importance of keeping classes small and manageable, suggesting that if a function requires numerous parameters, it is more efficient to encapsulate those within an object. Overall, the discussion provides actionable insights for developers on when to implement classes in their programming projects.

PREREQUISITES
  • Understanding of Object-Oriented Programming (OOP) principles
  • Familiarity with class and object concepts in programming
  • Experience in designing software architectures
  • Knowledge of function and variable organization in code
NEXT STEPS
  • Research best practices for class design in OOP
  • Explore design patterns that utilize class structures, such as MVC (Model-View-Controller)
  • Learn about encapsulation and its benefits in software development
  • Investigate techniques for refactoring code to improve class organization
USEFUL FOR

Software developers, particularly those involved in OOP, software architects, and anyone looking to improve their class design and organization skills in programming projects.

Red_CCF
Messages
530
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 5 ·
Replies
5
Views
1K