Click here to Skip to main content
15,896,207 members

OOP and the scope of a class, am I wrong?

Source: CodeProject     Posted by Sean Ewington    Monday, November 6, 2023 6:00pm    
Inquiring OOP minds want to know.

I'm hearing that professors are teaching that classes are effectively a single action like ReadTextFile and it makes me a lot more irritated than I probably should be.



Back to all news items

 
AnswerI don't recall that in university, but take everything with a truck of salt Pin
bantling7-Nov-23 0:54
bantling7-Nov-23 0:54 
Devs are known for being very stuck on their opinions, and as a someone said in a movie "opinions are like assholes, everybody has one".

There is no reason for a class to have a small scope, or to be small in size, but there is a reason for a class to have a single responsibility. Usually, a class should have one responsibility - but what about the "junk drawer" of small simple functions that encapsulate a few lines of oft-needed code that you don't want to repeat everywhere, yet these functions are not related to each other? They could be placed into a single Util or Funcs class. Arguably, the single responsibility of this class is to be a home for these kinds of functions.

Usually classes are small in size, but not always. The Java BigDecimal class has one responsibility - handling arbitrary precision math. It has 5,601 lines of code in Java 11.

I would say it depends on what you're writing:

- A custom app for a paying customer
These apps tend to be maintained for 10-12 years for private companies, 20-25 years for government agencies. They need to be maintainable. Making small, readable, single responsibility classes is of great benefit.

- A library to be reused across unrelated projects
A library needs to be good enough quality it does not introduce bugs and headaches to the projects it is used in, or people will just stop using it. Keeping classes to one scope and as independent as possible from other classes makes them like the parts of a swiss army knife, that can be combined in whatever way is needed - most importantly, in ways the library author never thought of.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.