Chapter 7. Organization of Object-Oriented Code

In this chapter, we will write code for a complex application that requires dozens of classes, interfaces, and constructor functions according to the programing language that we use. We will take advantage of the different available features to organize a large number of pieces of code in each of the three covered programming languages: Python, JavaScript, and C#. We will:

  • Understand the importance of organizing object-oriented code
  • Think about the best ways to organize object-oriented code
  • Work with source files organized in folders and module hierarchies in Python
  • Work with folders, namespaces, and namespace hierarchies in C#
  • Combine objects, nested objects, and constructor functions in JavaScript

Thinking about the best ways to organize code

When you have just a few classes or constructor functions and their prototypes, hundreds of lines of object-oriented code are easy to organize and maintain. However, as the number of object-oriented blueprints start to increase, it is necessary to follow some rules to organize the code and make it easy to maintain.

A very well written object-oriented code can generate a maintenance headache if it isn't organized in an effective way. We don't have to forget that a well written object-oriented code promotes code reuse.

As you learned in the previous six chapters, each programming language provides different elements and resources to generate object-oriented code. In addition, each programming language provides its own mechanisms that allow you to organize and group different object-oriented elements. Thus, it is necessary to define rules for each of the three programming languages: Python, C# and, JavaScript.

Imagine that we have to create and furnish house floor plans with a drawing software that allows you to load objects from files. We have a huge amount of objects to compose our floor plan, such as entry doors, interior doors, square rooms, interior walls, windows, spiral stairs, straight stairs, and kitchen islands. If we use a single folder in our file system to save all the object files, it will take us a huge amount of time to select the desired object each time we have to add an object to our floor plan.

We can organize our objects in the following five folders:

  • Build
  • Furnish
  • Decorate
  • Landscape
  • Outdoor

Now, whenever we need bathroom furniture, we will explore the Furnish folder. Whenever we need outdoor structures, we will explore the Outdoor folder. However, there are still too many objects in each of these folders. For example, the Build folder includes the following types of objects:

  • Rooms
  • Walls
  • Areas
  • Doors
  • Windows
  • Stairs
  • Fireplaces

We can create subfolders within each main category folder to provide a better organization of our object files. The Build category will have one subfolder for each of the types of objects indicated in the previous list.

The Furnish category will have the following subfolders:

  • Living room
  • Dining room
  • Kitchen
  • Bathroom
  • Bedroom
  • Office
  • Laundry and utility
  • Other rooms

The Decorate category will have the following subfolders:

  • Paint and walls
  • Flooring
  • Countertops
  • Art and decor
  • Electronics
  • Lighting and fans

The Landscape category will have the following subfolders:

  • Areas definition
  • Materials
  • Trees and plants

Finally, the Outdoor category will have the following subfolders:

  • Living
  • Accessories
  • Structures

This way, the Build/Rooms subfolder will include the following four objects:

  • Square room
  • L-shaped room
  • Small room
  • Closet

However, the Furnish/Bedroom subfolder includes too many objects that we can organize in seven types. So, we will create the following six subfolders:

  • Beds
  • Kids' beds
  • Night tables
  • Dressers
  • Mirrors
  • Nursery

Whenever we need bedroom mirrors, we will go to the Furnish/Bedroom/Mirrors subfolder. Whenever we need beds, we will go to the Furnish/Bedroom/Beds subfolder. Our objects are organized in a hierarchical directory tree.

Now, let's go back to object-oriented code. Instead of objects, we will have to organize classes, interfaces, constructor functions, and prototypes according to the programming language used. For example, if we have a class that defines the blueprint for a square room, we can organize it in such a way that we can find it in a build.rooms container. This way, we will find all the classes related to Build/Rooms in the build.rooms container. If we need to add another class related to Build/Rooms, we would add it in the build.rooms container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.98.18