Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Programming style
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Manner of writing source code}} {{More citations needed|date=June 2016}} '''Programming style''', also known as '''coding style''', refers to 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. 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 == Adherence to coding style can be enforced through automated tools, which format code according to predefined guidelines. These tools reduce the manual effort required to maintain style consistency, allowing programmers to focus on logic and functionality. For instance, tools such as <code>Black</code> for Python and <code>clang-format</code> for C++ automatically reformat code to comply with specified coding standards. == Style guidelines == 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 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 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"> if (hours < 24 && minutes < 60 && seconds < 60) { return true; } else { return false; } </syntaxhighlight> Arguably, poorly formatted: <!-- The code block below is mis-indented intentionally. Do not correct or fix it to use proper indentation. --> <syntaxhighlight lang="c"> if ( hours < 24 && minutes < 60 && seconds < 60 ) {return true ;} else {return false ;} </syntaxhighlight> ==== Notable indenting styles ==== ===== ModuLiq ===== The ModuLiq Zero Indentation Style groups by empty line rather than indenting. Example: <syntaxhighlight lang="c"> if (hours < 24 && minutes < 60 && seconds < 60) return true; else return false; </syntaxhighlight> ===== 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 return true else return false end </syntaxhighlight> Indenting is optional in Lua. <code>and</code>, <code>or</code>, and <code>not</code> function as logical operators. ===== Python ===== [[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 "<pre> ... </pre>" (for [[HTML]]), "[code]" ... "[/code]" (for [[bbcode]]), etc. <syntaxhighlight lang="python"> if hours < 24 and minutes < 60 and seconds < 60: return True else: return False </syntaxhighlight> Python starts a block with a colon (<code>:</code>). Python programmers tend to follow a commonly agreed style guide known as PEP8.<ref>{{cite web |url=https://www.python.org/dev/peps/pep-0008/ |title=PEP 0008: Style Guide for Python Code |publisher=python.org}}</ref> There are tools designed to automate PEP8 compliance. ===== 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. Example: <syntaxhighlight lang="haskell"> let c_1 = 1 c_2 = 2 in f x y = c_1 * x + c_2 * y </syntaxhighlight> may be written in one line as: <syntaxhighlight lang="haskell"> let {c_1=1;c_2=2} in f x y = c_1 * x + c_2 * y </syntaxhighlight> Haskell encourages the use of [[literate programming]], where extended text explains the genesis of the code. In literate Haskell scripts (named with the <code>lhs</code> extension), everything is a comment except blocks marked as code. The program can be written in [[LaTeX]], in such case the <code>code</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: <syntaxhighlight lang="haskell"> The function \verb+isValidDate+ test if date is valid \begin{code} isValidDate :: Date -> Bool isValidDate date = hh>=0 && mm>=0 && ss>=0 && hh<24 && mm<60 && ss<60 where (hh,mm,ss) = fromDate date \end{code} observe that in this case the overloaded function is \verb+fromDate :: Date -> (Int,Int,Int)+. </syntaxhighlight> And an example using plain text: <syntaxhighlight lang="haskell"> The function isValidDate test if date is valid > isValidDate :: Date -> Bool > isValidDate date = hh>=0 && mm>=0 && ss>=0 > && hh<24 && mm<60 && ss<60 > where (hh,mm,ss) = fromDate date observe that in this case the overloaded function is fromDate :: Date -> (Int,Int,Int). </syntaxhighlight> === 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. For example, unaligned: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; </syntaxhighlight> aligned: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; </syntaxhighlight> Unlike the unaligned code, the aligned code implies that the search and replace values are related since they have corresponding elements. As there is one more value for search than replacement, if this is a bug, it is more likely to be spotted via visual inspection. Cited disadvantages of vertical alignment include: * Dependencies across lines which leads to maintenance load. For example, if a long column value is added that requires a wider column, then all lines of the table must be modified (to maintain the tabular form) which is a larger change which leads to more effort to review and to understand the change at a later date * Brittleness: if a programmer does not correctly format the table when making a change, the result is a visual mess that is harder to read than unaligned code. Simple refactoring operations, such as renaming, can break the formatting. * More effort to maintain which may discourage a programmer from making a beneficial change, such as improving the name of an identifier, because doing so would require significant formatting effort * Requirement to use a fixed-width fonts; not proportional fonts Maintaining alignment can be alleviated by a tool that provides support (i.e. for [[elastic tabstop]]s), although that creates a reliance on such tools. As an example, simple refactoring operations to rename "$replacement" to "$r" and "$anothervalue" to "$a" results in: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; </syntaxhighlight> With unaligned formatting, these changes do not have such a dramatic, inconsistent or undesirable effect: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; </syntaxhighlight> === 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]]. The two code snippets below are the same logically, but differ in whitespace. <syntaxhighlight lang="c"> int i; for(i=0;i<10;++i){ printf("%d",i*i+i); } </syntaxhighlight> versus <syntaxhighlight lang="c"> int i; for (i = 0; i < 10; ++i) { printf("%d", i * i + i); } </syntaxhighlight> The use of [[Tab key|tabs]] for whitespace is debatable. Alignment issues arise due to differing tab stops in different environments and mixed use of tabs and spaces. As an example, one programmer prefers [[tab stop]]s of four and has their toolset configured this way, and uses these to format their code. <syntaxhighlight lang="c"> int ix; // Index to scan array long sum; // Accumulator for sum </syntaxhighlight> 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. <syntaxhighlight lang="c"> int ix; // Index to scan array long sum; // Accumulator for sum </syntaxhighlight> 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: <syntaxhighlight lang="cpp"> 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 }; </syntaxhighlight> == See also == * {{Annotated link|MISRA C}} * {{Annotated link|Naming convention (programming)}} == References == {{Reflist}} [[Category:Source code]] [[Category:Programming language comparisons]] <!-- Hidden categories below --> [[Category:Articles with example C code]] [[Category:Articles with example Haskell code]] [[Category:Articles with example PHP code]] [[Category:Articles with example Python (programming language) code]] [[Category:Articles with example pseudocode]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Annotated link
(
edit
)
Template:Cite web
(
edit
)
Template:More citations needed
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)