|
|
|
|
| Tutorial |
Structuring Code into Logical Groups
Applications usually consist of different function groups, each handling a special task
and/or area of the application. For example, when writing a database-backed application,
a function group should be responsible solely for handling database access.This
code builds an entity of its own and can safely be detached from the rest of the
program—if you designed it well. Function groups that logically perform only a
certain task should be designed in such a way that they can be treated independently
from the rest of the code.These functions should also be physically separated from the
main code, building a module.
Before implementing an application, you should create a list of all functions that
can be grouped together, forming a module, and create a separate design plan for each
module.Take great care to create detailed data flowcharts in order to make the modules
capable of handling all demands of the application.The importance of outlining
on paper shouldn’t be underestimated. Space restrictions prevent us from going into
detail on this topic, but we encourage you to educate yourself with some of the very
good books available on design methods.
Abstracting Separate Chunks of Code
Abstracting blocks of code is a task that should be done during both planning and
implementation. Let’s say that a function will perform the following jobs:
1. Open a file.
2. Read a block of data from the file.
3. Validate the data.
4. Correct any errors in the data.
5. Write the data back to the file.
6. Close the file.
Each step can be packed into a separate block of code. It’s good style to abstract these
blocks and create separate functions out of them. Not only will this enable you to
reuse each code block in other functions (you’ll probably need file operation support
somewhere else as well), but it will also make the code much easier to read and debug.
You can make the abstracted parts bulletproof, equip them with error-handling support,
and much more. If you tried to do this inline, your code would quickly grow
beyond manageable size and become very clumsy. Plus, if you use the same code
blocks in other functions and notice an error or need to change something, you’d have
to make the same corrections over and over in every other function using this block.
By abstracting, you centralize the critical points; by correcting a single line, you can
change the behavior of all related functions.
|
|
|
|
|
|
| Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist |
|