The Simplest Abstractions
The most common programming abstractions involve associating names with data as a convenient and efficient means of using those data in repeated operations. For instance, consider the following function, which user the sum of its two arguments in several operations.
(defun example (x y)
(* (+ 13 (+ x y)) (- 7 (+ x y)) (+ x y)))
The variable x and y are added together every time their sum is needed as an operand. The following example binds a temporary variable to the sum of the two arguments; the sum is only computed once. Thereafter, the variable sum serves as the operand in the appropiate places:
(defun example (x y)
(let ((sum (+ x y)))
(* (+ 13 sum) (- 7 sum) sum)))
Repetitive code of any form is generally a good candidate for an abstraction.
Understanding a Programming Problem
The best first step in solving a programming problem is to discribe the problem to yourself. You cannot expect an adequate solucion unless you understand the problem in your own terms. Ask yourself some basic questions:
- What needs to be done? Describe the general goals that the program must achieve.
- What are the big pieces of the problem? Consider the steps required to reach the overall solution of the task. How do the pieces fit together?.
- What is the scope of the problem? Try to identify issues that concern you as well as those you can ignore.
- What resources are available to you as the programmer? Common operations are often captured in library routines. Often you can find examples of programs that perform similar tasks or that provide a certain part of you solution. In many cases, you can discuss the reasoning behind the example code with the programmer who wrote it. Avoid doing work that already been done. However, you must be aware that someone else's model for a solution may not be compatible with yours.
- What kind of inputs is supplied and who supplies it? Certain programs require user interaction, while others receive input from other programs.
- How does the program demostrate that the desired task is complete? The program might succeed quietly by calling other programs or by properly initializing a device. It could also return formatted results, including sophisticated charts and tables.
- Who will use the program? A novice user might require a simple and friendly interface, while a sophisticated user might expect more features and some access to program itself.
- What constraints limit the approach to the problem? Often the programming projects must conform to strict deadlines, or the resulting application must be compatible with the existing programs or must run within a limited amount of physical memory. The environment of the target system might also lack certain resources.
Conceptualizing the problem might give you some ideas about its solution and how that solution might best be expressed.