Class vs Function/Method


#1

I have a “best practice” question. In this chapter, we created a new class called TrueFalse. We could have, of course, just created an array in QuizActivity without the class and created a couple functions and been done with it. However, here we chose to create a Class with getters and setters. I would like to make good decisions going forth on which approach to take.

I’d just like some feedback on when it is best to use one over the other. One reason I understand is reusability; another reason might be encapsulation. Are there other reasons? Are my two given reasons correct? Thanks in advance!


#2

Yep, those are good reasons. Some other ones I like:

1- Refactorability. If you’re using objects already, it’s straightforward to switch from objects to an interface, or use a different subclass of that object. Much easier to make those kinds of changes.

2- Clarity. When you make an object, you assign a concept a meaning. That’s hard, but it means you’re not just writing something that works, you’re also writing a program that can be understood. And in the long run, that’s far more important than whether the program works initially.