The second concept in code optimization is associated with the piece parts of the model components. Consequently, this programming style affects all 3 components of the MVC model. At a deeper level of granularity, all 3 components of the MVC model are composed of tasks. A task can be composed of subtasks. Many models do not differentiate between a component being a task and a component being a subtask; the word "task" is reasonable for specifying the code architecture.
Whether a task is named a task, or a task is named a subtask, either way, it is composed of functions, or methods, that perform that task. It is important to remember "one method one task", which is not the same as "one task one method". Eventually, what you will develop is many small pieces of code that are methods. Each method should perform one and only one task.
For example, the vehicle model contains a task named "CreateDecal". If you want to ask the user a question about their favorite color, the method that performs this task might be the FavoriteColor method. This is not the same as methods that answer the question "what kinds of stripes do you like?" or "what shape should be used for the decal?". Those are two other questions that the CreateDecal task can use, but they do not work with the same properties as the FavoriteColor method. Consequently, the FavoriteColor method does not need to perform those parts of the CreateDecal task; it performs only one task.
Likewise, when there is one task that you want to develop, try to write the entire task in one method. A method that does only one thing can be called by several tasks, which increases reusability.
The overarching task can have a meaningful name, even if it has to call several methods. This means that the overarching task is only one method long. There is a one-to-one correspondence between the task and its piece parts. The piece parts may have a one-to-many correspondence between the piece parts themselves and the tasks of the overall project.
This is the point at which you have to remember that the MVC model is not a blueprint. There may be times when some code must be duplicated in order for the overarching task to be contained in one method, or to circumvent compile warnings or errors. It is normal to include some extra methods in a task. However, using this second concept of code optimization will reduce redundancy.
Select the "Next" link to discuss the third guideline.
|Previous Page||Page 5 of 9||Next Page|
|Copyright 2012 Yvonne V. Richardson|