Let’s talk about programming Classes. Classes are a pretty simple Object-Oriented Programming (abbreviated to OOP for the remainder of this post) convention. Classes are used to hold methods and use their methods to create objects that can then be used in programming to make either more objects or calculations. Classes can also be referenced by other classes in order to make applications more streamlined and look nicer. Remember that well written code should be self documenting. (That doesn’t mean don’t document.)
While recently coding a project that used Classes in order to reference a website’s data through its open source API, a friend of mine mentioned something that stuck pretty well. He didn’t want to be named on the internet, nor did he claim the quote but he said; “Every Class should have -A- Job.” His emphasis was on the -a- part. The context is that I was about to code a validation process for my project in the command line interface class (CLI for short). The CLI already had the job of taking input from the user and outputting data based off the user input. Adding another job to the CLI would bloat the purpose of that class, and wouldn’t be a streamlined or a good OOP designed application.
Instead, I made a separate class which had the sole purpose of using its single method to validate that all the data output by the CLI class wasn’t empty. For example if my CLI class was set to output:
Name: a_name | Address: a_address | Website:
It would take note of the fact that website was an empty space, and instead output:
Name: a_name | Address: a_address | Website: No website url found
This additional validation class helped my program follow OOP principles better and allowed it to present information to my users in a better way. Now my users would know that information they may have wanted didn’t exist for that particular listing. Overall, it was a good lesson in separation of concerns when it comes to OOP, and I won’t forget it soon.