art of programming

Upload: ehsanrana

Post on 14-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Art of Programming

    1/4

  • 7/30/2019 Art of Programming

    2/4

    Style

    Style is the most important part of programming. Style is what separates the gems from the junk. Itis what separates the programming artist from the butcher. You must learn good programming stylefirst, before typing in your first line of code, so that everything you write will be of the highest quality.

    A working but uncommented program is a time bomb waiting to explode. Sooner or later, someonewill have to fix a bug in the program, modify it, or upgrade it, and the lack of comments will make thejob much more difficult. A well-commented, simple program is a work of art. Learning how tocomment is as important as learning how to code properly.

    In order to write a program, you must have a clear idea of what you are going to do. One of the bestways to organize your thoughts is to write them down in a language that is clear and easy tounderstand. After the process has been clearly stated, it can be translated into a computer program.Your program should read like an essay. It should be as clear and easy to understand as possible.Good programming style comes from experience and practice.

    The list that follows contains some of the sections that should be included at the beginning of yourprogram. Not all programs will need all sections, so use only those that apply:

    1. Heading Name of the program/a short description of what the program does2. Author You've gone to a lot of trouble to create this program. Take credit for it. Also, anyone

    who has to modify the program can come to you for information and help.

    3. Purpose Why did you write this program? What does it do?4. Usage Short explanation of how to run the program5. References Creative copying is a legitimate form of programming (don't break the copyright

    laws in the process). Reference the original author of any work you copied.6. File formats List the files that program reads or writes and a short description of their

    formats.

    7. Restrictions List any limits or restrictions that apply to the program, such as "The programdoes not check for input errors."

    8. Revision history List indicating who modified the program, and when and what changes weremade.

    9. Error handling If the program detects an error, describe what the program does with it.10.Notes Include special comments or other information that has not already been covered.

  • 7/30/2019 Art of Programming

    3/4

    Common Coding Practices

    Indentation and Code Format

    A program should be concise and easy to read. It must serve as a set of computer instructions, butalso as a reference work describing the algorithms and data used inside it. Everything should bedocumented with comments. Comments serve two purposes. First, they tell the programmer to followthe code, and second, they help the programmer remember what he did.

    Class discussion: Create a style sheet for class assignments. Discuss what comments should go intothe programs and why.

  • 7/30/2019 Art of Programming

    4/4

    Elements of a Program

    If you are going to construct a building, the first two things you need are: the bricks and a blueprintthat tells you how to put them together. In computer programming, you need two things: data(variables) and instructions (code or functions). Variables are the basic building blocks of a program.Instructions tell the computer what to do with the variables.

    In construction, before we can start, we must order our materials: "We need 500 large bricks, 80 half-size bricks, and 4 flagstones." Similarly, in C, we must declare our variables before we can use them.We must name each one of our "bricks" and tell C what type of brick to use.

    After our variables are defined, we can begin to use them. In construction, the basic structure is aroom. By combining many rooms, we form a building. In C, the basic structure is a function. Functionscan be combined to form a program.

    An apprentice builder does not start out building the Empire State Building, but rather starts on a one-room house. Similarly a novice programmer will concentrate on constructing simple one-functionprograms.