OOP: When & Whether to Construct a New Class?

  • Thread starter Thread starter Red_CCF
  • Start date Start date
  • Tags Tags
    Oop
AI Thread Summary
Determining when to create a new class in programming often hinges on the logical organization of functions and variables. Classes typically suggest themselves based on the structure of the application; for instance, in a board game, a Game class may encompass a Board class, which manages Fields and Pieces. In simulations, classes can represent Objects in Space and include Manager classes for interactions. A key indicator for creating a class is when related functions share internal variables, such as file handling functions like SaveToFile or LoadFromFile, which could be encapsulated in a FileHandler class. Keeping classes small and manageable is also crucial; if a class declaration extends beyond a single screen or requires excessive parameters, it may be time to refactor into an object. This approach enhances code clarity and maintainability by ensuring that relevant data is contained within the object, reducing the need for cumbersome parameter lists.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top