Naming conventions: Difference between revisions

added Factor
(→‎{{header|Go}}: Reworded point 3 which wasn't entirely accurate previously.)
(added Factor)
Line 460:
 
* [https://github.com/bbatsov/clojure-style-guide The Clojure Style Guide]
 
=={{header|Factor}}==
Identifiers are <code>named-with-dashes</code> instead of <code>named_with_underscores</code> or <code>namedWithCamelCase</code>. We avoid abbreviating names. Since we typically don't name many throwaway values, this improves clarity. Parsing words are <code>NAMED-LIKE-THIS:</code> so words that perform parse time look-ahead can be easily identified.
 
Since words can be named anything as long as they don't parse as a number or a string, word names follow an expressive mnemonic system, outlined below. This is not enforced in any way, but encouraged as a way to improve clarity of intent.
 
{| class="wikitable"
|-
! General form
! Description
! Examples
|-
| <tt>foo?</tt>
| outputs a boolean
| <tt>empty?</tt>
|-
| <tt>foo!</tt>
| a variant of <tt>foo</tt> which mutates one of its arguments
| <tt>append!</tt>
|-
| <tt>?foo</tt>
| conditionally performs <tt>foo</tt>
| <tt>?nth</tt>
|-
| <tt><foo></tt>
| creates a new <tt>foo</tt>
| <tt><array></tt>
|-
| <tt>>foo</tt>
| converts the top of the stack into a <tt>foo</tt>
| <tt>>array</tt>
|-
| <tt>foo>bar</tt>
| converts a <tt>foo</tt> into a <tt>bar</tt>
| <tt>number>string</tt>
|-
| <tt>new-foo</tt>
| creates a new <tt>foo</tt>, taking some kind of parameter from the stack which determines the type of the object to be created
| <tt>new-sequence, new-lexer, new</tt>
|-
| <tt>foo*</tt>
| alternative form of <tt>foo</tt>, or a generic word called by <tt>foo</tt>
| <tt>at*, pprint*</tt>
|-
| <tt>(foo)</tt>
| implementation detail word used by <tt>foo</tt>
| <tt>(clone)</tt>
|-
| <tt>set-foo</tt>
| sets <tt>foo</tt> to a new value
| <tt>set-length</tt>
|-
| <tt>foo>></tt>
| gets the <tt>foo</tt> slot of the tuple at the top of the stack
| <tt>name>></tt>
|-
| <tt>>>foo</tt>
| sets the <tt>foo</tt> slot of the tuple at the top of the stack
| <tt>>>name</tt>
|-
| <tt>with-foo</tt>
| performs some kind of initialization and cleanup related to <tt>foo</tt>, usually in a new dynamic scope
| <tt>with-scope, with-input-stream, with-output-stream</tt>
|-
| <tt>$foo</tt>
| help markup
| <tt>$heading, $emphasis</tt>
|}
 
Stack effects also follow conventions. A stack effect of a word tells the stack effect checker how many objects a word takes from the stack and how many objects it leaves on the stack. For example, the stack effect for <code>+</code> looks like this: <code>( x y -- z )</code>. This declares that <code>+</code> takes two numbers from the stack and leaves one number on the stack. What you name these inputs and outputs does not matter to the stack effect checker; it only looks at how many inputs and outputs there are.
 
Inputs and outputs are typically named after some pun on their data type, or a description of the value's purpose if the type is very general.
 
{| class="wikitable"
|-
! Name
! Value
|-
| <tt>?</tt>
| a boolean
|-
| <tt><=></tt>
| an ordering specifier
|-
| <tt>m, n</tt>
| an integer
|-
| <tt>obj</tt>
| an object
|-
| <tt>quot</tt>
| a quotation
|-
| <tt>seq</tt>
| a sequence
|-
| <tt>assoc</tt>
| an associative mapping
|-
| <tt>str</tt>
| a string
|-
| <tt>x, y, z</tt>
| a number
|-
| <tt>loc</tt>
| a screen location specified as a two-element array holding x and y coordinates
|-
| <tt>dim</tt>
| a screen dimension specified as a two-element array holding width and height values
|-
| <tt>*</tt>
| when this symbol appears by itself in the list of outputs, it means the word unconditionally throws an error
|}
 
=={{header|Forth}}==
1,820

edits