XXXI layout and style

The basic principle

The basic principle formatted

Formatted by this principle states that, good layout highlighting the logical structure of the program.

Any interpretation of computer programs

The layout is a useful hint about the program structure.

What is a good layout with

In some style to write a program and not just in aesthetic considerations. Written in the usual way with a program of psychological factors - programmers have a strong sense that other programmers will follow the discussion of these rules.

The layout as a religion

Layout involves not only aesthetic, but also have a taste of logical problems, which means debate over programming format sounds more like a religious struggle, rather than discussing philosophy.

Good layout of the target

  • Accurate representation of the logical structure of the code;
  • Consistently exhibit the logical structure of the code;
  • Improve readability;
  • Stand modifications.

Layout Techniques

  • blank;
    • Blank can enhance readability. Including blank spaces, tabs, line breaks, blank lines, it is the primary means to show the program structure;
    • Grouping;
    • Blank line;
    • indentation;
  • brackets.

Layout style

  • Pure block structure;
  • Mimic pure block structure;
  • Use of the begin-end (braces) specified block boundary;
  • End of the line layout.

Layout control structure

Formatting control structure block point

  • Do not use indented begin-end pair;
  • Do not let the begin-end twice indent.

Other considerations

  • To use the blank lines between paragraphs;
  • Single statement format to be uniform before and after the code block;
  • For complex expressions, conditional on the partition lines;
  • No goto;
  • caseStatements do not have an exception end of the line layout.

Layout single statement

Sentence length

A common but somewhat outdated rules, is the sentence length should be limited to 80 characters, for the following reasons:

  • More than 80 characters in the statement hard to read;
  • 80-character limit does not encourage deeply nested;
  • Statement line is longer than 80 characters on 8.5 × 11 inch size paper does not fit, particularly when the bar code is printed on one side when the two.

Use spaces to make statements seem clear

  • Use spaces make logical expressions more readable;
  • Use spaces to make it easier to read array references;
  • Use spaces to make it easier to read subroutine parameters;

Formatting subsequent line

  • The continuation lines clear;
  • The control statements subsequent lines indented in a standard number of spaces;
  • Do not aligned assignment equal sign;
  • Follow-up to the standard amount of indentation for assignment.

Write only one statement per line

  • Each statement individually placed in a line, can provide an accurate perspective on program complexity.
  • For modern compilers, the multiple statements on a line to optimize not provide any clues.
  • Each statement on a separate line, the code just read from top to bottom, rather than the top down, but also read from left to right;
  • Each statement on a separate line, when the line number of the compiler to point out an error in a row, you will be easier to find syntax errors;
  • Each statement on a separate line, and easier to edit a single statement - delete a row or a row temporarily changed to comment.

Layout data declaration

  • Declare one per line data;
  • Variable declaration should be as close to the location of its first use;
  • Rational organization of declaration order;
  • C ++, please put an asterisk near the variable name, or to declare a pointer type when you declare a pointer variable.

Notes layout

  • Note should be consistent with the corresponding indentation of the code;
  • Each comment line at least one blank line separately.

Subroutine layout

  • Each line separated by portions of the subroutine;
  • The subroutine parameters as standard indentation.

The layout class

Layout of the class interface

When the layout of the class interface, class members should generally be expressed in the following order:

  1. Description head class and its complete use of the comment;
  2. The constructor and destructor;
  3. public subroutine;
  4. protected subprograms;
  5. private subprograms and data members.

Class layout implementation

Class should implement generally arranged in the following order:

  1. Head of the content of the file where the class described in the comment;
  2. Class data;
  3. public subroutine;
  4. protected subprograms;
  5. private subroutine.

Layout files and programs

  • A file should be only one class;
  • Name the file should be associated with the class name;
  • Clearly separate the subroutine in the file;
  • Alphabetical subroutine;
  • For C ++, carefully organize the contents of the source file in the order:
    1. Descriptive file comments;
    2. #includeFile line;
    3. A plurality of constants for use in the class definitions;
    4. Enumeration used in multiple class;
    5. Macro function definition;
    6. A plurality of types of definitions used in the class;
    7. Imported global variables and functions;
    8. Export of global variables and functions;
    9. Private variables and functions used in the present document;
    10. Each class, including the various class constant definitions, and the enumeration type definition.

Checklist: Layout

General Issues

  • [] Format is mainly to show the logical structure of the code do?
  • [] Your layout program can be unified to use it?
  • [] Your layout program makes your code easier to maintain it?
  • [] Your layout program and whether it helps readability of the code?

Layout control structure

  • [] Your code to avoid the begin-end pair or {}double indentation yet?
  • [] Between adjacent blocks separated by empty lines yet?
  • [] To take into account the complex expressions format readability it?
  • [] The layout of the blocks only one statement consistently do?
  • [] caseStatement formatting and other control structures consistent yet?
  • [] To gotoformat the statement whether it allowed a conspicuous?

Layout single statement

  • [] Readability logical expression, array subscript and subroutine parameters and use the space yet?
  • [] Incomplete statements end of the line is obviously the wrong way to the end of it?
  • [] The following line as the number of standard indentation yet?
  • [] At most, only one statement per line do?
  • [] Each statement written no side effects?
  • [] Per line at most only declare a data?

Notes layout

  • [] The same amount to indent the code comments and annotations they do?
  • [] Annotation style easy to maintain it?

Subroutine layout

  • [] You understand easy way to format each subprogram parameters, modify, annotate it?
  • [] The use of blank lines separate various parts of subroutines yet?

Layout classes, files and programs

  • It is one to one relationship between [] the majority of classes and files?
  • [] If more than one class within a file, all kinds of routines are grouped into classes yet? All kinds are clearly identified yet?
  • Subroutine blank line [] file clearly separated yet?
  • [] In the case there is no better form of organization, all subprograms are in alphabetical order yet?

Points

  • The primary task is to specify the visual layout of the logical organization of code. Indicators to assess whether the task implemented include accuracy, consistency, readability and ease of maintenance;
  • Looks pleasing to the eye than the other indicators are the most important. However, if other indicators have reached with the code and of good quality, then the layout will also effect looks pretty good;
  • Visual Basic code block with a pure style, and traditional practices is to use the Java plain block style, so if these programming languages, please use plain block style. In C ++, a pure block or simulate begin-end block boundary are effective;
  • In particular, object code structure itself. Consistently follow a few habits to innovation. The layout specification can not be sustained only damage readability;
  • Many aspects of the design layout of matters of faith. We should try to distinguish between objective needs and subjective preferences area. Set a clear goal, to choose on the basis of the discussion style parameters.

Guess you like

Origin www.cnblogs.com/liam-ji/p/11605324.html