Programming style: Difference between revisions

Content deleted Content added
 
(13 intermediate revisions by 12 users not shown)
Line 1:
{{Short description|Manner of writing source code}}
{{More citations needed|date=June 2016}}
}}
 
'''Programming style''', also known as '''coding style''', refers toare the conventions and patterns used in writing [[source code]], resulting in a consistent and readable [[codebase]]. These conventions often encompass aspects such as [[Indentation style|indentation]], [[Naming convention (programming)|naming conventions]], [[capitalization]], and [[Comment (computer programming)|comments]]. Consistent programming style is generally considered beneficial for [[code readability]] and [[maintainability]], particularly in collaborative environments.
 
Maintaining a consistent style across a codebase can improve readability and ease of software maintenance. It allows developers to quickly understand code written by others and reduces the likelihood of errors during modifications. Adhering to standardized coding guidelines ensures that teams follow a uniform approach, making the codebase easier to manage and scale. Many organizations and [[Open source|open-source]] projects adopt specific coding standards to facilitate collaboration and reduce cognitive load.
Line 15 ⟶ 14:
Common elements of coding style include:
* ''Indentation and [[whitespace character]] use'' – Ensures consistent block structures and improves readability.
* ''Naming conventions'' – Standardizes how [[Variable (computer science)|variables]], [[Function (computer programming)|functions]], and [[Class (computer programming)|classes]] are named, typically adhering to [[Camel case|camelCase]], [[snake_casesnake case]], or [[Pascal (programming language)|Pascal]]Case, depending on the language.
* ''Capitalization'' – Dictates whether keywords and identifiers are capitalized or lowercase, in line with language syntax.
* ''Comment use'' – Provides context and explanations within code without affecting its execution.
 
=== Indentation ===
Indentation style can assist a reader in various wayways including: identifying control flow and blocks of code. In some programming languages, indentation is used to [[off-side rule|delimit blocks of code]] and therefore is not matter of style. In languages that ignore whitespace, indentation can affect readability.
 
For example, formatted in a commonly- used style:
Indentation style can assist a reader in various way including: identifying control flow and blocks of code. In some programming languages, indentation is used to [[off-side rule|delimit blocks of code]] and therefore is not matter of style. In languages that ignore whitespace, indentation can affect readability.
 
For example, formatted in a commonly-used style:
 
<syntaxhighlight lang="c">
Line 48 ⟶ 46:
 
==== Notable indenting styles ====
 
===== ModuLiq =====
{{Further|ModuLiq}}
 
The '''ModuLiq''' Zero Indentation Style groups by empty line rather than indenting.
 
Example:
Line 64 ⟶ 61:
 
===== Lua =====
{{Further|Lua}}
 
[['''Lua (programming language)|Lua]]''' does not use the traditional [[curly braces]] or [[parentheses]]; rather, the expression in a conditional statement must be followed by <code>then</code>, and the block must be closed with <code>end</code>.
<syntaxhighlight lang="lua">
if hours < 24 and minutes < 60 and seconds < 60 then
Line 77 ⟶ 74:
 
===== Python =====
{{Further|Python (programming language)}}
 
[['''Python (programming language)|Python]]''' relies on the ''[[off-side rule]]'', using indenting to indicate and implement control structure, thus eliminating the need for bracketing (i.e., <code>{</code> and <code>}</code>). However, copying and pasting indented code can cause problems, because the indent level of the pasted code may not be the same as the indent level of the target line. Such reformatting by hand is tedious and error prone, but some [[text editor]]s and [[integrated development environment]]s (IDEs) have features to do it automatically. There are also problems when indented code is rendered unusable when posted on a forum or web page that removes whitespace, though this problem can be avoided where it is possible to enclose code in whitespace-preserving tags such as "&lt;pre&gt; ... &lt;/pre&gt;" (for [[HTML]]), "[code]" ... "[/code]" (for [[bbcode]]), etc.
 
<syntaxhighlight lang="python">
Line 92 ⟶ 89:
 
===== Haskell =====
{{Further|Haskell}}
[['''Haskell]]''', like Python, has the ''off-side rule''. It has a two-dimension syntax where indenting 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.
[[Haskell]], like Python, has the ''off-side rule''. It has a two-dimension syntax where indenting 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:
Line 135 ⟶ 132:
 
=== Vertical alignment ===
 
Some programmers consider it valuable to align similar elements vertically (as tabular, in columns), citing that it can make typo-generated bugs more obvious.
 
Line 194 ⟶ 190:
 
=== Whitespace ===
A [[free-format language]] ignores [[whitespace character]]s: spaces, tabs and new lines so the programmer is free to style the code in different ways without affecting the meaning of the code. Generally, the programmer uses style that is considered to enhance [[readability]].
 
A [[free-format language]] ignores [[whitespace character]]s: spaces, tabs and new lines so the programmer is free to style the code in different ways without affecting the meaning of the code. Generally, the programmer uses style that is considered to enhance [[readability]].
 
The two code snippets below are the same logically, but differ in whitespace.
Line 249 ⟶ 244:
== References ==
{{Reflist}}
 
== External links==
*{{Curlie|Computers/Programming/Development_Tools/Source_Code_Formatters|Source Code Formatters}}
 
[[Category:Source code]]