Programming Style: Improve It Talk Personal Reflection, Personal Essay, or Argumentative Essay
Programming Style: Improve It Talk Personal Reflection, Personal Essay, or Argumentative Essay
This article has multiple issues. Please help improve it or discuss these issues on the talk
page. (Learn how and when to remove these template messages)
This article is written like a personal reflection, personal essay, or argumentative essay that
states a Wikipedia editor's personal feelings or presents an original argument about a topic.
(October 2008)
This article needs additional citations for verification. (June 2016)
Programming style, also known as code style, is a set of rules or guidelines used when writing
the source code for a computer program. It is often claimed that following a particular
programming style will help programmers read and understand source code conforming to the
style, and help to avoid introducing errors.
A classic work on the subject was The Elements of Programming Style, written in the 1970s, and
illustrated with examples from the Fortran and PL/I languages prevalent at the time.
The programming style used in a particular program may be derived from the coding
conventions of a company or other computing organization, as well as the preferences of the
author of the code. Programming styles are often designed for a specific programming language
(or language family): style considered good in C source code may not be appropriate for BASIC
source code, etc. However, some rules are commonly applied to many languages.
Contents
1 Elements of good style
2 Code appearance
o 2.1 Indentation
2.1.1 ModuLiq
2.1.2 Lua
2.1.3 Python
2.1.4 Haskell
o 2.2 Vertical alignment
o 2.3 Spaces
o 2.4 Tabs
3 See also
4 References
5 External links
Code appearance
Programming styles commonly deal with the visual appearance of source code, with the goal of
readability. Software has long been available that formats source code automatically, leaving
coders to concentrate on naming, logic, and higher techniques. As a practical point, using a
computer to format source code saves time, and it is possible to then enforce company-wide
standards without debates.
Indentation
Indentation styles assist in identifying control flow and blocks of code. In some programming
languages, indentation is used to delimit logical blocks of code; correct indentation in these cases
is more than a matter of style. In other languages, indentation and white space do not affect
function, although logical and consistent indentation makes code more readable. Compare:
or
if ( hours < 24
&& minutes < 60
&& seconds < 60
)
{return true
;} else
{return false
;}
The first two examples are probably much easier to read because they are indented in an
established way (a "hanging paragraph" style). This indentation style is especially useful when
dealing with multiple nested constructs.
ModuLiq
The ModuLiq Zero Indentation Style groups with carriage returns rather than indentations.
Compare all of the above to:
else
return false;
Lua
Lua does not use the traditional curly braces or parenthesis. if/else statements only require the
expression be followed by then, and closing the if/else statement with end.
Indentation is optional. and, or, not are used in between true/false statements.
print(not true)
Python
Notice that Python does not use curly braces, but a regular colon (e.g. else:).
Many Python programmers tend to follow a commonly agreed style guide known as PEP8.[1]
There are tools designed to automate PEP8 compliance.
Haskell
Haskell similarly has the off-side rule, i.e. it has a two-dimension syntax where indentation is
meaningful to define blocks (although, an alternate syntax uses curly braces and semicolons).
Haskell is a declarative language, there are statements, but declarations within a Haskell script.
Example:
let c_1 = 1
c_2 = 2
in
f x y = c_1 * x + c_2 * y
Haskell encourage the use of literate programming, where extended text explain the genesis of
the code. In literate Haskell scripts (named with the lhs extension), everything is a comment
except blocks marked as code. The program can be written in LaTeX, in such case the code
environment marks what is code. Also each active code paragraph can be marked by preceding
and ending it with an empty line, and starting each line of code with a greater than sign and a
space. Here an example using LaTeX markup:
observe that in this case the overloaded function is fromDate :: Date ->
(Int,Int,Int).
Vertical alignment
It is often helpful to align similar elements vertically, to make typo-generated bugs more
obvious. Compare:
// Another example:
$value = 0;
$anothervalue = 1;
$yetanothervalue = 2;
with:
// Another example:
$value = 0;
$anothervalue = 1;
$yetanothervalue = 2;
The latter example makes two things intuitively clear that were not clear in the former:
the search and replace terms are related and match up: they are not discrete variables;
there is one more search term than there are replacement terms. If this is a bug, it is now
more likely to be spotted.
Inter-line false dependencies; tabular formatting creates dependencies across lines. For
example, if an identifier with a long name is added to a tabular layout, the column width
may have to be increased to accommodate it. This forces a bigger change to the source
code than necessary, and the essential change may be lost in the noise. This is detrimental
to Revision control where inspecting differences between versions is essential.
Brittleness; if a programmer does not neatly format the table when making a change,
maybe legitimately with the previous point in mind, the result becomes a mess that
deteriorates with further such changes. Simple refactoring operations, such as search-and-
replace, may also break the formatting.
Resistance to modification; tabular formatting requires more effort to maintain. This
may put off a programmer from making a beneficial change, such as adding, correcting or
improving the name of an identifier, because it will mess up the formatting.
Reliance on mono-spaced font; tabular formatting assumes that the editor uses a fixed-
width font. Many modern code editors support proportional fonts, and the programmer
may prefer to use a proportional font for readability.
Tool dependence; some of the effort of maintaining alignment can be alleviated by tools
(e.g. a source code editor that supports elastic tabstops), although that creates a reliance
on such tools.
For example, if a simple refactoring operation is performed on the code above, renaming
variables "$replacement" to "$r" and "$anothervalue" to "$a", the resulting code will look like
this:
// Another example:
$value = 0;
$a = 1;
$yetanothervalue = 2;
The original sequential formatting will still look fine after such change:
// Another example:
$value = 0;
$a = 1;
$yetanothervalue = 2;
Spaces
In those situations where some white space is required, the grammars of most free-format
languages are unconcerned with the amount that appears. Style related to white space is
commonly used to enhance readability. There are currently no known hard facts (conclusions
from studies) about which of the whitespace styles have the best readability.
int i;
for(i=0;i<10;++i){
printf("%d",i*i+i);
}
versus
int i;
for (i=0; i<10; ++i) {
printf("%d", i*i+i);
}
Tabs
The use of tabs to create white space presents particular issues when not enough care is taken
because the location of the tabulation point can be different depending on the tools being used
and even the preferences of the user.
As an example, one programmer prefers tab stops of four and has their toolset configured this
way, and uses these to format their code.
Another programmer prefers tab stops of eight, and their toolset is configured this way. When
someone else examines the original person's code, they may well find it difficult to read.
One widely used solution to this issue may involve forbidding the use of tabs for alignment or
rules on how tab stops must be set. Note that tabs work fine provided they are used consistently,
restricted to logical indentation, and not used for alignment:
class MyClass {
int foobar(
int qux, // first parameter
int quux); // second parameter
int foobar2(
int qux, // first parameter
int quux, // second parameter
int quuux); // third parameter
};
See also
Coding conventions
MISRA C
Naming convention (programming)
References
1.
Categories:
Source code
Navigation menu
Not logged in
Talk
Contributions
Create account
Log in
Article
Talk
Read
Edit
View history
Search
Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
Contribute
Help
Community portal
Recent changes
Upload file
Tools
Print/export
Download as PDF
Printable version
Languages
العربية
Deutsch
Español
Français
한국어
日本語
Русский
Tiếng Việt
中文
Edit links
Privacy policy
About Wikipedia
Disclaimers
Contact Wikipedia
Mobile view
Developers
Statistics
Cookie statement