programming style programming 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 pli 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 elements of good style good style is a subjective matter and is difficult to define however there are several elements common to a large number of programming styles the issues usually considered as part of programming style include the layout of the source code including indentation the use of white space around operators and keywords the capitalization or otherwise of keywords and variable names the style and spelling of userdefined identifiers such as function procedure and variable names and the use and style of comments 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 companywide standards without debates indentation indent 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 if hours 24 minutes 60 seconds 60 return true else return false or if hours 24 minutes 60 seconds 60 return true else return false with something like 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 note however that this example is the same as simply return hours 24 minutes 60 seconds 60 moduliq the moduliq zero indent style groups with carriage returns rather than indents compare all of the above to if hours 24 minutes 60 seconds 60 return true else return false lua lua does not use the traditional curly braces or parenthesis ifelse statements only require the expression be followed by then and closing the ifelse statement with end if hours 24 and minutes 60 and seconds 60 then return true else return false end indentation is optional and or not are used in between truefalse statements they are truefalse statements as printnot true would mean false python python uses indentation to indicate control structures so correct indentation is required by doing this the need for bracketing with curly braces ie and is eliminated on the other hand copying and pasting python code can lead to problems because the indentation level of the pasted code may not be the same as the indentation level of the current line such reformatting can be tedious to do by hand but some text editors and ides have features to do it automatically there are also problems when python code being rendered unusable when posted on a forum or web page that removes white space though this problem can be avoided where it is possible to enclose code in white spacepreserving tags such as pre pre for html for bbcode etc if hours 24 and minutes 60 and seconds 60 return true else return false notice that python does not use curly braces but a regular colon eg else many python programmers tend to follow a commonly agreed style guide known as pep8 there are tools designed to automate pep8 compliance haskell haskell similarly has the offside rule ie 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 c1 1 c2 2 in f x y c1 x c2 y may be written in one line as let c11c22 in f x y c1 x c2 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 the function verbisvaliddate test if date is valid begincode isvaliddate date bool isvaliddate date hh0 mm0 ss0 hh24 mm60 ss60 where hhmmss fromdate date endcode observe that in this case the overloaded function is verbfromdate date intintint and an example using plain text the function isvaliddate test if date is valid isvaliddate date bool isvaliddate date hh0 mm0 ss0 hh24 mm60 ss60 where hhmmss fromdate date observe that in this case the overloaded function is fromdate date intintint vertical alignment it is often helpful to align similar elements vertically to make typogenerated bugs more obvious compare search arraya b c d e replacement arrayfoo bar baz quux another example value 0 anothervalue 1 yetanothervalue 2 with search arraya b c d e replacement arrayfoo bar baz quux 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 however note that there are arguments against vertical alignment interline 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 searchandreplace 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 monospaced font tabular formatting assumes that the editor uses a fixedwidth 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 eg 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 search arraya b c d e r arrayfoo bar baz quux another example value 0 a 1 yetanothervalue 2 the original sequential formatting will still look fine after such change search arraya b c d e r arrayfoo bar baz quux another example value 0 a 1 yetanothervalue 2 spaces in those situations where some white space is required the grammars of most freeformat 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 for instance compare the following syntactically equivalent examples of c code int i fori0i10i printfdiii versus int i for i0 i10 i printfd iii versus int i for i 0 i 10 i printfd 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 his toolset configured this way and uses these to format his code int ix index to scan array long sum accumulator for sum another programmer prefers tab stops of eight and his toolset is configured this way when he examines his code he 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 coding conventions misra c naming convention programming source code formatters at curlie 