programming style
≈ 7 min read
Names in other languages
- Españolestilo de programación
- Češtinastyl zápisu programu
- ltProgramavimo stilius
- Englishprogramming style
- Nederlandsprogrammeerstijl
- DeutschProgrammierstil
- العربيةأسلوب البرمجة
- 日本語プログラミング作法
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Programming style, also known as coding style, are the conventions and patterns used in writing source code, resulting in a consistent and readable codebase. These conventions often encompass aspects such as indentation, naming conventions, capitalization, and 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 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's PEP 8 is a widely recognized style guide that outlines best practices for writing Python code.…
Programming style, also known as coding style, are the conventions and patterns used in writing source code, resulting in a consistent and readable codebase. These conventions often encompass aspects such as indentation, naming conventions, capitalization, and 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 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's PEP 8 is a widely recognized style guide that outlines best practices for writing Python code. In contrast, languages like C or Java may have industry standards that are either formally documented or adhered to by convention. if (hours Arguably, poorly formatted: if ( hours if (hours if hours Indenting is optional in Lua. and , or , and not function as logical operators. if hours Python starts a block with a colon ( : ). Python programmers tend to follow a commonly agreed style guide known as PEP8. There are tools designed to automate PEP8 compliance. let c_1 = 1 c_2 = 2 in f x y = c_1 * x + c_2 * y may be written in one line as: let {c_1=1;c_2=2} in f x y = c_1 * x + c_2 * y Haskell encourages the use of literate programming, where extended text explains 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: The function \verb+isValidDate+ test if date is valid \begin{code} isValidDate :: Date -> Bool isValidDate date = hh>=0 && mm>=0 && ss>=0 && hh (Int,Int,Int)+. And an example using plain text: The function isValidDate test if date is valid > isValidDate :: Date -> Bool > isValidDate date = hh>=0 && mm>=0 && ss>=0 > && hh where (hh,mm,ss) = fromDate date observe that in this case the overloaded function is fromDate :: Date -> (Int,Int,Int). $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; aligned: $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; 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 fixed-width fonts, not proportional fonts Maintaining alignment can be alleviated by a tool that provides support (i.e. for elastic tabstops), 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: $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; With unaligned formatting, these changes do not have such a dramatic, inconsistent or undesirable effect: $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; int i; for(i=0;i versus int i; for (i = 0; i The use of 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 stops of four and has their toolset configured this way, and uses these to format their code. int ix; // Index to scan array long sum; // Accumulator for sum 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. int ix; // Index to scan array long sum; // Accumulator for sum 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 };
Text derived from https://en.wikipedia.org/w/index.php?oldid=1365647330 under CC BY-SA 4.0; modifications have been made.