Please see my other Object-Oriented Programming (OOP) articles.
Using Object-oriented Programming (OOP) to Design Robust Classes
At the beginning of a software project, ensure you capture requirements in a manner which developers may easily interpret.
Stating requirements using a story format allows users and actions to be clearly identified.
When telling the story, ensure you identify users as nouns and behaviors as verbs.
In this article, I will demonstrate some requirements for an example system.
A set of requirements for this system may contain the following:
- HR employees (noun) are the primary users of this system.
- Employees (noun) are the primary objects.
- HR employees must create an employee while setting their first and last name.
- HR employees must edit an employee’s first and last name.
- HR employees must retrieve an employee’s first and last name.
- HR employees must configure employees for taking a vacation.
From these requirements I may determine:
- Class(es) needed to created object: Employee
- Employee class requires the behavior: initialize first and last name during creation.
- Employee class requires the behavior: edit first and last names.
- Employee class requires the behavior: take vacation.
Attributes (data)
With the previous requirements defined, isolate and identify which portions represent data contained within Employee object(s).
For example, we know from the requirements “first and last name” an object should contain the attributes firstName and lastName.
Based on the requirement “retrieve an employee’s first and last name,” I may infer a new fullName attribute is needed.
Based on the requirement “Employees require the behavior: take vacation,” I may infer a vacationStatus attribute is needed.
Behavior (methods)
With the previous requirements defined, isolate and identify which portions represent behaviors are required by Employee object(s).
From the requirement “Employees require the behavior: initialize first and last name during creation,” I will include the method: Employee constructor to initialize first and last name.
From the requirement “Employees require the behavior: edit first and last name during creation,” I will include the method: SetNames to update the first and last names.
From the requirement “Employees require the behavior: take vacation,” I will include the method: TakeVacation to perform action(s) necessary for the employee to take a vacation and update their vacationStatus variable.
Naming Convention
While there are many philosophies regarding naming convention to use within a class, I’ve found the following simple rules have worked throughout my career, and within teams.
- Regardless of the convention chosen, consistency is the most important rule to follow.
- Anything accessed outside the class should have a name beginning with an uppercase letter.
- Anything accessed inside the class should have a name beginning with an lowercase letter.