Programming style is a set of rules or guidelines used when writing source code for computer programs. It is often claimed that following a particular programming style will help programmers read and understand the source code that fits the style, and helps to avoid mistakes.
A classic work on the subject is the Programming Element , written in the 1970s, and illustrated with examples from the common Fortran and PL/I languages ââat the time.
The programming styles used in a particular program may be derived from corporate coding conventions or other computing organizations, as well as author's preference. Programming styles are often designed for specific programming languages ââ(or family languages): styles that are considered good in C source code may not be appropriate for BASIC source code, etc. However, some rules are usually applied to many languages.
Video Programming style
Good style elements
A good style is a subjective matter, and difficult to define. However, there are some common elements for a large number of programming styles. Problems are usually considered part of the programming style including source code layout, including indentation; use of free space around operators and keywords; capitalization or other keywords and variable names; style and spelling of user-defined identifiers, such as functions, procedures, and variable names; and the use and style of comments.
Maps Programming style
Appearance of code
The programming style is usually related to the visual appearance of the source code, with the purpose of being easy to read. Software has long been available that formats source code automatically, letting coders concentrate on naming, logic, and higher techniques. As a practical point, using computers to format source code saves time, and it is possible to then enforce company standards without debate.
Indents
Indent styles help in identifying control flows and code blocks. In some indentation programming languages ââare used to restrict logical code blocks; the correct curvature in these cases is more than just style. In other languages ââindentation and spaces do not affect the function, although logical and consistent indentation makes the code easier to read. Comparing:
or
with something like that
The first two instances may be easier to read because they are indented into an established way ("hanging paragraph" style). This indentation style is very useful when dealing with multiple nested constructs.
Note however that this example is the same as:
ModuLiq
ModuLiq Zero Indent Style Group with delivery carriage rather than indentation. Compare all of the above for:
Lua
Lua does not use traditional curly brackets or brackets. if/else statements just requires an expression followed by then
, and close the if/else statement with end
.
Indenting is optional. and
, or
, not
are used between true/false statements.
They are true/false statements, such as
would mean wrong.
Python
Python uses indentation to show the control structure, so the correct indentation is required. By doing this, the need for bracketing with curly brackets (ie {
and }
) is omitted. On the other hand, copying and pasting Python code can cause problems, since the level of indentation of the embedded code may not be the same as the indentation level of the current row. Such reformatting can be tediously done by hand, but some text editors and IDEs have the feature to do it automatically. There is also a problem when the given Python code can not be used when posted on a forum or web page that removes white space, although this problem can be avoided where it is possible to include the code in a white space preservation tag such as "& lt; pre & gt ;.. & lt;/pre & gt; "(for HTML)," [code] "..." [/code] "(for bbcode), etc.
Note that Python does not use curly braces, but a regular two dot (eg else:
).
Many Python programmers tend to follow the agreed general style guides known as PEP8. There are tools designed to automate PEP8 compliance.
Haskell
Haskell also has an off-side rule, which has a two-dimensional syntax where indentation is meaningful for defining blocks. Although the alternative syntax uses curly brackets and semicolons. Haskell is a declarative language, no statement, but a declaration in a Haskell script. Example:
can be written in one line as:
Haskell encourages the use of literacy programming, in which extended text explains the origin of the code. In the literate Haskell script (named after the lhs
extension), all are comments except blocks marked as code. This program can be written in L a T e X , in which case the code
environment indicates what code is. Also each paragraph of the active code can be marked by preceding and ending it with a blank line, and starting each line of code with larger marks and spaces. Here's an example of using L a T e X markup:
And examples using plain text:
Vertical alignment
Often helps to align similar elements vertically, to make the bugs generated by tipo more clear. Comparing:
with:
The last example makes two obviously intuitive things that are not clear in the first:
- search and replace related and matching terms: they are not discrete variables;
- there's one more search term than the alternate term. If this is a bug, it is now more likely to be visible.
However, note that there is a argument against vertical alignment:
- Incorrect cross-line dependency ; tabular formatting creates cross-line dependencies. For example, if an identifier with a long name is added to the tabular layout, the column width may need to be upgraded to accommodate it. This forces larger changes to the source code than necessary, and important changes may be lost in noise. This is detrimental to the Revision control where checking the differences between versions is very important.
- Fragility ; If a programmer does not neatly format a table when making changes, perhaps legally with the previous point in mind, the result becomes a worsening mess with such further changes. Simple refactoring operations, such as search and replacement, can also damage formatting.
- Resilience to modifications ; tabular format requires more effort to maintain. This may delay the programmer to make profitable changes, such as adding, fixing or correcting the name of the identifier, as this will disrupt the format.
- Dependency on fonts with a mono space ; tabular formatting assumes that the editor uses a fixed width font. Many modern code editors support proportional fonts, and programmers may prefer to use proportional fonts for legibility.
- Device dependency ; some efforts to maintain alignment can be reduced by tools (eg source code editors that support elastic tabstles), even if it creates a dependency on such a tool.
For example, if a simple refactoring operation is done in the code above, rename the variable "$ replacement" to "$ r" and "$ anothervalue" to "$ a", the resulting code would look like this:
The original sequential format will still look good after the change:
Spaces
In situations where some white space is required, the grammar of most of the free-format languages ââis not concerned with the number that appears. The style associated with white space is usually used to improve readability. There is currently no known hard fact (the conclusion of the study) about which of the whitespace styles have the best readings.
For example, compare the example C code equivalent to the following syntax:
against
melawan
Tab
The use of tabs to create white space presents a particular problem when insufficient care is taken because the location of the tabulation point may differ depending on the tools used and even the user preferences.
For example, one programmer would prefer a four stop tab and have his toolset configured in this way, and use this to format the code.
Other programmers prefer the eight stop tab, and the toolset is configured in this way. When he checks his code, he may find it difficult to read.
One widely used solution to this problem may involve prohibiting the use of tabs for alignment or rules about how the stop tab should be set. Note that the tab works fine as long as it is used consistently, limited to logical indentation, and not used for alignment:
See also
- Encoding convention
- Indented style
- MISRA C
- Naming convention (programming)
References
External links
- Source Logger in Curlie (based on DMOZ)
Source of the article : Wikipedia