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
Indentation 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!
== C derived language styles == The following styles are common for various languages derived from C that are both significantly similar and dissimilar. And, they can be adapted to C as well. They might be applied to C code written as part of a project ''mostly'' written in one of these other languages, where maintaining a consistent ''look and feel'' to the project's core code overrides considerations of using more conventional C style. === Lisp style === While [[#GNU|GNU style]] is sometimes characterized as C code indented by a Lisp programmer, one might even go so far as to insert closing braces together in the last line of a block. This style makes indentation the only way to distinguish blocks of code, but has the advantage of containing no uninformative lines. This could easily be called the Lisp style because this style is very common in Lisp code. In Lisp, the grouping of identical braces at the end of expression trees is meant to signify that it is not the user's job to visually track nesting levels, only to understand the structure of the tree. The traditional Lisp variant of this style prefers extremely narrow levels of indentation (typically two spaces) because Lisp code usually nests very deeply since Lisp features only [[Expression (computer science)|expression]]s, with no distinct class of [[Statement (computer science)|statement]]s; function arguments are mostly indented to the same level to illustrate their shared status within the enclosing expression. This is also because, braces aside, Lisp is conventionally a very terse language, omitting even common forms of simple boilerplate code as uninformative, such as the <code>else</code> keyword in an <code>if : then | else</code> block, instead rendering it uniformly as <code>(if expr1 expr2 expr3)</code>. <syntaxhighlight lang="c"> // C for (i = 0; i < 10; i++) {if (i % 2 == 0) {do_something(i);} else {do_something_else(i); do_third_thing(i);}} </syntaxhighlight> <syntaxhighlight lang="lisp"> ;; Lisp (dotimes (i 10) (if (= (rem i 2) 0) (do-something i) (progn (do-something-else i) (do-third-thing i)))) </syntaxhighlight> <sup>Note: <code>progn</code> is a procedure for evaluating multiple sub-expressions sequentially for [[Side effect (computer science)|effects]], while discarding all but the final (nth) return value. If all return values are desired, the <code>values</code> procedure would be used.</sup> === Haskell style === [[Haskell (programming language)|Haskell]] layout can make the placement of braces optional, although braces and semicolons are allowed in the language. <ref>{{cite web |url=https://www.haskell.org/onlinereport/lexemes.html |title=The Haskell 98 Report |access-date=3 March 2016 |website=haskell.org}}</ref> The two segments below are equally acceptable to the compiler: <syntaxhighlight lang=haskell> braceless = do text <- getContents let firstWord = head $ words text bigWord = map toUpper firstWord putStrLn bigWord braceful = do { text <- getContents ; let { firstWord = head $ words text ; bigWord = map toUpper firstWord } ; putStrLn bigWord } </syntaxhighlight> In Haskell, layout can replace braces. Usually the braces and semicolons are omitted for [[Procedural programming|procedural]] <code>do</code> sections and the program text in general, but the style is commonly used for lists, records and other syntactic elements made up of some pair of parentheses or braces, which are separated with commas or semicolons.<ref>{{cite web |url=http://learnyouahaskell.com/making-our-own-types-and-typeclasses |title=Making Our Own Types and Typeclasses |access-date=3 February 2016 |website=learnyouahaskell.com |last=Lipovača |first=Miran}}</ref> If code following the keywords <code>where</code>, <code>let</code>, or <code>of</code> omits braces and semicolons, then indentation is significant.<ref>Haskell Report 1.2 (1992), p.131 B.4 "Layout"</ref> === APL style === For an example of how terse APL typically is, here is the implementation of the step function for the Game of Life: <syntaxhighlight lang=apl> life←{⊃1⍵∨.∧3 4=+/+⌿¯1 0 1∘.⊖¯1 0 1⌽¨⊂⍵} </syntaxhighlight> [[APL (programming language)|APL]] style C resembles the terse style of APL code, and is commonly used in their implementations.<ref>{{cite web |url=https://www.jsoftware.com/ioj/iojATW.htm |title=The J Incunabulum |access-date=19 May 2022 |website=jsoftware.com}}</ref> This style was pioneered by [[Arthur Whitney (computer scientist)|Arthur Whitney]], and is heavily used in the implementation of [[K (programming language)|K]], Arthur's own project. The [[J (programming language)|J]] programming language is implemented in this style as well. Notably, not all implementations of APL use this style of C, namely: GNU APL and Dyalog APL. In addition to APL style C indentation, typically the names are shortened to either single or double characters: To reduce the amount of indentation, and expressions spanning multiple lines.<ref>{{cite web |url=https://github.com/jsoftware/jsource/tree/master/jsrc |title=The J source code |access-date=12 September 2024 |website=github.com}}</ref>
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)