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
(section)
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!
==== 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>
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)