Code appearance Programming style




1 code appearance

1.1 indentation

1.1.1 moduliq
1.1.2 lua
1.1.3 python
1.1.4 haskell


1.2 vertical alignment
1.3 spaces
1.4 tabs





code appearance

programming styles commonly deal visual appearance of source code, goal of readability. software has long been available formats source code automatically, leaving coders concentrate on naming, logic, , higher techniques. practical point, using computer format source code saves time, , possible enforce company-wide standards without debates.


indentation

indent styles assist in identifying control flow , blocks of code. in programming languages indentation used delimit logical blocks of code; correct indentation in these cases more matter of style. in other languages indentation , white space not affect function, although logical , consistent indentation makes code more readable. compare:



or



with like



the first 2 examples easier read because indented in established way (a hanging paragraph style). indentation style useful when dealing multiple nested constructs.


note example same simply:



moduliq

the moduliq 0 indent style groups carriage returns rather indents. compare of above to:



lua

lua not use traditional curly braces or parenthesis. if/else statements require expression followed then, , closing if/else statement end.



indentation optional. and,or,not used in between true/false statements.


they true/false statements, as



would mean false.


python

python uses indentation indicate control structures, correct indentation required. doing this, need bracketing curly braces (i.e. { , }) eliminated. on other hand, copying , pasting python code can lead problems, because indentation level of pasted code may not same indentation level of current line. such reformatting can tedious hand, text editors , ides have features automatically. there problems when python code being rendered unusable when posted on forum or web page removes white space, though problem can avoided possible enclose code in white space-preserving tags such <pre> ... </pre> (for html), [code] ... [/code] (for bbcode), etc.



notice python not use curly braces, regular colon (e.g. else:).


many python programmers tend follow commonly agreed style guide known pep8. there tools designed automate pep8 compliance.


haskell

haskell has off-side rule, i.e. has 2 dimension syntax indentation meaningful define blocks. although alternate syntax uses curly braces , semicolons. haskell declarative language, there statements, declarations within haskell script. example:



may written in 1 line as:



haskell encourage use of literate programming, extended text explain genesis of code. in literate haskell scripts (named lhs extension), comment except blocks marked code. program can written in latex, in such case code environment marks code. each active code paragraph can marked preceding , ending empty line, , starting each line of code greater sign , space. here example using latex markup:



and example using plain text:



vertical alignment

it helpful align similar elements vertically, make typo-generated bugs more obvious. compare:



with:



the latter example makes 2 things intuitively clear not clear in former:



the search , replace terms related , match up: not discrete variables;
there 1 more search term there replacement terms. if bug, more spotted.

however, note there arguments against vertical alignment:



inter-line false dependencies; tabular formatting creates dependencies across lines. example, if identifier long name added tabular layout, column width may have increased accommodate it. forces bigger change source code necessary, , essential change may lost in noise. detrimental revision control inspecting differences between versions essential.
brittleness; if programmer not neatly format table when making change, maybe legitimately previous point in mind, result becomes mess deteriorates further such changes. simple refactoring operations, such search-and-replace, may break formatting.
resistance modification; tabular formatting requires more effort maintain. may put off programmer making beneficial change, such adding, correcting or improving name of identifier, because mess formatting.
reliance on mono-spaced font; tabular formatting assumes editor uses fixed-width font. many modern code editors support proportional fonts, , programmer may prefer use proportional font readability.
tool dependence; of effort of maintaining alignment can alleviated tools (e.g. source code editor supports elastic tabstops), although creates reliance on such tools.

for example, if simple refactoring operation performed on code above, renaming variables $replacement $r , $anothervalue $a , resulting code this:



the original sequential formatting still fine after such change:



spaces

in situations white space required, grammars of free-format languages unconcerned amount appears. style related white space commonly used enhance readability. there no known hard facts (conclusions studies) of whitespace styles have best readability.


for instance, compare following syntactically equivalent examples of c code:



versus



versus



tabs

the use of tabs create white space presents particular issues when not enough care taken because location of tabulation point can different depending on tools being used , preferences of user.


as example, 1 programmer prefers tab stops of 4 , has toolset configured way, , uses these format code.



another programmer prefers tab stops of eight, , toolset configured way. when examines code, may find difficult read.



one used solution issue may involve forbidding use of tabs alignment or rules on how tab stops must set. note tabs work fine provided used consistently, restricted logical indentation, , not used alignment:









Comments

Popular posts from this blog

Journals by countries Pedophile press

The Story Of Sugriva Sugriva

History Thames Ironworks and Shipbuilding Company