Programming style: Difference between revisions

Content deleted Content added
m Disambiguating links to Source (link changed to Open source) using DisamAssist.
Tag: Reverted
 
(7 intermediate revisions by 7 users not shown)
Line 2:
{{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 [[StyleIndentation 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.
 
Style guidelines can be formalized in documents known as '''coding conventions''', which dictate specific formatting and naming rules. These conventions may be prescribed by official standards for a programming language or developed internally within a team or project. For example, [[Python (programming language)|Python]]'s [[PEP 8]] is a widely recognized style guide that outlines best practices for writing Python code. In contrast, languages like [[C (programming language)|C]] or [[Java (programming language)|Java]] may have industry standards that are either formally documented or adhered to by convention.
 
== Automation ==
Line 19:
 
=== 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:
 
<syntaxhighlight lang="c">
Line 47:
==== Notable indenting styles ====
===== ModuLiq =====
{{Further|ModuLiq}}
The '''ModuLiq''' Zero Indentation Style groups by empty line rather than indenting.
 
Example:
Line 60 ⟶ 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 72 ⟶ 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 86 ⟶ 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.