Wiktionary:Coding conventions: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
m Reverted edits by 112.215.239.164. If you think this rollback is in error, please leave a message on my talk page.
Tag: Rollback
→‎Customary abbreviations: delete form-of inflection tag abbreviations; out of date; properly maintained version found on Template:inflection of documentation
 
(43 intermediate revisions by 32 users not shown)
Line 4: Line 4:
* Language, family and script codes are based on [[w:BCP 47|BCP 47]].
* Language, family and script codes are based on [[w:BCP 47|BCP 47]].
** Non-standard language and family codes should consist of a (preferably standard) parent family code and a Wiktionary-specific suffix.
** Non-standard language and family codes should consist of a (preferably standard) parent family code and a Wiktionary-specific suffix.
** Codes for proto-languages should consist of a family code and a <code>-pro</code> suffix.
** Codes for protolanguages should consist of a family code and a <code>-pro</code> suffix.
** Non-standard script codes should consist of a language code prefix, and a standard ISO 15924 script code. (Exceptions: <code>CGK</code>, <code>IPAchar</code>, <code>Latinx</code>, <code>musical</code>, <code>polytonic</code>, <code>Ruminumerals</code>.)
** Non-standard script codes should consist of a language code prefix, and a standard {{w|ISO 15924}} script code. (Exceptions: <code>Ipach</code>, <code>Music</code>, <code>Polyt</code>, <code>Rumin</code>.)


== Templates ==
== Templates ==
* Names of templates related to a particular language should be prefixed by the language code, see [[WT:List of languages]]. Likewise for templates specific to a script; see [[WT:List of scripts]] for a list of script codes.
* Names of templates related to a particular language should be prefixed by the language code, see [[WT:List of languages]]. Likewise for templates specific to a script; see [[WT:List of scripts]] for a list of script codes.
** For proto-languages, it is permitted to drop <code>-pro</code> from the code, and use the code of the family alone.
** For proto-languages, it is permitted to drop <code>-pro</code> from the code, and use the code of the family alone.
* Names of reference templates should have the prefix "<code>R:</code>". If the reference is for one specific language, it is often followed by language code and a colon.
* Names of reference templates should have the prefix <code>R:</code>. If the reference is for one specific language, it is often followed by language code and a colon.
* Likewise, usage note templates should have the prefix "<code>U:</code>".
* Likewise, usage note templates should have the prefix <code>U:</code>.
* Relatively complex templates, especially those with parser functions (like {{temp|#if:}} or {{temp|#switch:}}), should have the code split up onto multiple lines for readability. Wikicode constructs should be aligned so that the starting curly braces and ending curly braces line up vertically. The line breaks themselves should be wrapped in comments; each line should end with a begin-comment tag <code><nowiki><!--</nowiki></code> paired with an end-comment tag <code><nowiki>--></nowiki></code> on the next line. This avoids line breaks accidentally appearing in the output or parameters of templates. Like so:
* Relatively complex templates, especially those with parser functions (like {{temp|#if:}} or {{temp|#switch:}}), should have the code split up onto multiple lines for readability. Wikicode constructs should be aligned so that the starting curly braces and ending curly braces line up vertically. The line breaks themselves should be wrapped in comments; each line should end with a begin-comment tag <code><nowiki><!--</nowiki></code> paired with an end-comment tag <code><nowiki>--></nowiki></code> on the next line. This avoids line breaks accidentally appearing in the output or parameters of templates. Like so:
<pre>-->{{#if:{{{something|}}}<!--
<pre><!--
-->{{#if:{{{something|}}}<!--
-->|<!--
-->|<!--
-->do things<!--
-->do things<!--
-->}}<!--
-->}}<!--
-->
</pre>
</pre>
* Templates which add categories that are only intended for entries should have extra code that only adds the category in the entry namespaces (main namespace and Appendix:). The templates {{temp|categorize}}, {{temp|catlangname}} and {{temp|catlangcode}} do this automatically, so using them is preferred.
* Templates which add categories that are only intended for entries should have extra code that only adds the category in the entry namespaces (main namespace and Appendix:). The templates {{temp|cat}} (for ''raw categories''), {{temp|cln}} (for categories beginning with a language name, aka ''grammar categories'') and {{temp|C}} (for categories beginning with a language code, aka ''topic categories'') do this automatically, so using them is preferred.
* If a template adds a category, but it would be desirable to suppress that category in some cases, it should have a {{para|nocat}} parameter which prevents that.
* If a template adds a category, but it would be desirable to suppress that category in some cases, it should have a {{para|nocat}} parameter which prevents that.
* If you want to accept a variable number of named parameters of the same type in a template, the first shall be named {{para|<var>foo</var>}}, while the rest shall be {{para|<var>foo</var>2}}, {{para|<var>foo</var>3}}, {{para|<var>foo</var>4}} and so on.
* If you want to accept a variable number of named parameters of the same type in a template, the first shall be named {{para|<var>foo</var>}}, while the rest shall be {{para|<var>foo</var>2}}, {{para|<var>foo</var>3}}, {{para|<var>foo</var>4}} and so on.
Line 24: Line 26:
* Headword-line templates should either be implemented in a separate module using [[Module:headword]], or use {{temp|head}}.
* Headword-line templates should either be implemented in a separate module using [[Module:headword]], or use {{temp|head}}.
* Redirects for templates intended for entries should be created sparingly.
* Redirects for templates intended for entries should be created sparingly.
* Avoid doing string processing in templates. If you find yourself in need of a "substring" function, you might have crossed the point where the template should be converted to Lua.
* Avoid doing string processing in templates. If you find yourself in need of a “substring” function, you might have crossed the point where the template should be converted to Lua.


== Lua ==
== Lua ==
Line 33: Line 35:
* Variables holding return values from {{code|lua|require()}} (i.e. imported modules) should have their names prefixed with <code>m_</code>.
* Variables holding return values from {{code|lua|require()}} (i.e. imported modules) should have their names prefixed with <code>m_</code>.
* Words in variable and function names should be separated with underscores. Variables should be all-lowercase unless other reasons dictate otherwise.
* Words in variable and function names should be separated with underscores. Variables should be all-lowercase unless other reasons dictate otherwise.
* [http://blog.codinghorror.com/coding-without-comments/ Comments should be used sparingly; good code does not need much commenting.] Keep comments brief and to the point. Do not put [[ASCII art]] in comments.
** Comments should not be used for documentation; use the documentation subpage instead.
* Unit tests for a module, if any, shall be put on a <code>/testcases</code> subpage.
* Unit tests for a module, if any, shall be put on a <code>/testcases</code> subpage.
** Unit test modules should use [[Module:UnitTests]]. See that module's page for usage details.
** Unit test modules should use [[Module:UnitTests]]. See that module’s page for usage details.
** If a specific kind of test is to be performed repeatedly, it shall have its own method whose name starts with <code>check_</code>.
** If a specific kind of test is to be performed repeatedly, it shall have its own method whose name starts with <code>check_</code>.
** An alternative test framework, [[Module:ScribuntoUnit]], has been imported from Wikipedia and can also be used.
** An alternative test framework, [[Module:ScribuntoUnit]], has been imported from Wikipedia and can also be used.
Line 55: Line 55:


== Customary abbreviations ==
== Customary abbreviations ==
* <code>a</code> - aspect
* <code>a</code> aspect
* <code>adj</code> - adjective
* <code>adj</code>&nbsp;– adjective
* <code>adv</code> - adverb
* <code>adv</code>&nbsp;– adverb
* <code>alt</code> - alternative display (link label)
* <code>alt</code>&nbsp;– alternative display (link label)
* <code>c</code> - common gender
* <code>c</code>&nbsp;– common gender
* <code>conj</code> - conjugation ''or'' conjunction
* <code>conj</code>&nbsp;– conjugation ''or'' conjunction
* <code>d</code> ''or'' <code>du</code> - dual
* <code>d</code> ''or'' <code>du</code>&nbsp;– dual
* <code>decl</code> - declension
* <code>decl</code>&nbsp;– declension
* <code>f</code> - feminine
* <code>f</code>&nbsp;– feminine
* <code>g</code> - gender
* <code>g</code>&nbsp;– gender
* <code>gloss</code> ''or'' <code>t</code> - translation gloss (for non-English terms)
* <code>gloss</code> ''or'' <code>t</code>&nbsp;– translation gloss (for non-English terms)
* <code>id</code> - sense ID (see {{temp|senseid}})
* <code>id</code>&nbsp;– sense ID (see {{temp|senseid}})
* <code>impf</code> - imperfective
* <code>impf</code>&nbsp;– imperfective
* <code>infl</code> - inflection
* <code>infl</code>&nbsp;– inflection
* <code>lang</code> - language code
* <code>lang</code>&nbsp;– language code
* <code>m</code> - masculine (exception: {{temp|m}} = mention)
* <code>m</code>&nbsp;– masculine (exception: {{temp|m}} = mention)
* <code>n</code> - neuter
* <code>n</code>&nbsp;– neuter
* <code>p</code> ''or'' <code>pl</code> - plural
* <code>p</code> ''or'' <code>pl</code>&nbsp;– plural
* <code>pf</code> - perfective
* <code>pf</code>&nbsp;– perfective
* <code>pos</code> - part of speech
* <code>pos</code>&nbsp;– part of speech
* <code>pron</code> - pronoun ''or'' pronunciation
* <code>pron</code>&nbsp;– pronoun ''or'' pronunciation
* <code>pronunc</code> - pronunciation
* <code>pronunc</code>&nbsp;– pronunciation
* <code>rf...</code> - request for...
* <code>rf...</code>&nbsp;– request for...
* <code>s</code> ''or'' <code>sg</code> - singular
* <code>s</code> ''or'' <code>sg</code>&nbsp;– singular
* <code>sc</code> - script code
* <code>sc</code>&nbsp;– script code
* <code>tr</code>, <code>translit</code> - transliteration
* <code>tr</code>, <code>translit</code>&nbsp;– transliteration

=== Verb inflection ===
* <code>actv</code> - [[active voice]]
* <code>aori</code> - [[aorist tense]]
* <code>cond</code> - [[conditional mood]]
* <code>futr</code> - [[future tense]]
* <code>imp</code> ''or'' <code>impr</code> - [[imperative mood]]
* <code>impf</code> - [[imperfect tense]]
* <code>ind</code> ''or'' <code>indc</code> - [[indicative mood]]
* <code>inf</code> - [[infinitive]]
* <code>ptc</code> ''or'' <code>part</code> - [[participle]]
* <code>past</code> - [[past tense]]
* <code>pasv</code> - [[passive voice]]
* <code>perf</code> - [[perfect tense]]
* <code>potn</code> - [[potential mood]]
* <code>pres</code> - [[present tense]]
* <code>pret</code> - [[preterite tense]]
* <code>sub</code> ''or'' <code>subj</code> - [[subjunctive mood]]
* <code>opt</code> - [[optative mood]]

=== Grammatical cases ===
* <code>abe</code> - [[abessive case]]
* <code>abl</code> - [[ablative case]]
* <code>abs</code> - [[absolutive case]]
* <code>acc</code> - [[accusative case]]
* <code>ade</code> - [[adessive case]]
* <code>all</code> - [[allative case]]
* <code>cfi</code> - [[causal-final case]]
* <code>com</code> - [[comitative case]]
* <code>dat</code> - [[dative case]]
* <code>del</code> - [[delative case]]
* <code>ela</code> - [[elative case]]
* <code>erg</code> - [[ergative case]]
* <code>esf</code> - [[essive-formal case]]
* <code>esm</code> - [[essive-modal case]]
* <code>ess</code> - [[essive case]]
* <code>gen</code> - [[genitive case]]
* <code>ill</code> - [[illative case]]
* <code>ine</code> - [[inessive case]]
* <code>ins</code> - [[instrumental case]] ''or'' [[instructive case]]
* <code>loc</code> - [[locative case]]
* <code>nom</code> - [[nominative case]]
* <code>par</code> - [[partitive case]]
* <code>pre</code> ''or'' <code>prep</code> - [[prepositional case]]
* <code>sbl</code> - [[sublative case]]
* <code>spe</code> - [[superessive case]]
* <code>ter</code> - [[terminative case]]
* <code>tra</code> - [[translative case]]
* <code>voc</code> - [[vocative case]]


== And above all ==
== And above all ==
Line 135: Line 86:


== External links ==
== External links ==
* [http://blog.codinghorror.com/spartan-programming/ Jeff Atwood, "Spartan Programming"]
* [http://blog.codinghorror.com/spartan-programming/ Jeff Atwood, “Spartan Programming”]
* [http://javascript.crockford.com/code.html Douglas Crockford's coding style guidelines]
* [http://javascript.crockford.com/code.html Douglas Crockford’s coding style guidelines]

[[Category:Modules| ]]

Latest revision as of 05:46, 9 March 2024

Codes

  • Language, family and script codes are based on BCP 47.
    • Non-standard language and family codes should consist of a (preferably standard) parent family code and a Wiktionary-specific suffix.
    • Codes for protolanguages should consist of a family code and a -pro suffix.
    • Non-standard script codes should consist of a language code prefix, and a standard ISO 15924 script code. (Exceptions: Ipach, Music, Polyt, Rumin.)

Templates

  • Names of templates related to a particular language should be prefixed by the language code, see WT:List of languages. Likewise for templates specific to a script; see WT:List of scripts for a list of script codes.
    • For proto-languages, it is permitted to drop -pro from the code, and use the code of the family alone.
  • Names of reference templates should have the prefix “R:”. If the reference is for one specific language, it is often followed by language code and a colon.
  • Likewise, usage note templates should have the prefix “U:”.
  • Relatively complex templates, especially those with parser functions (like {{#if:}} or {{#switch:}}), should have the code split up onto multiple lines for readability. Wikicode constructs should be aligned so that the starting curly braces and ending curly braces line up vertically. The line breaks themselves should be wrapped in comments; each line should end with a begin-comment tag <!-- paired with an end-comment tag --> on the next line. This avoids line breaks accidentally appearing in the output or parameters of templates. Like so:
<!--
-->{{#if:{{{something|}}}<!--
-->|<!--
  -->do things<!--
-->}}<!--
-->
  • Templates which add categories that are only intended for entries should have extra code that only adds the category in the entry namespaces (main namespace and Appendix:). The templates {{cat}} (for raw categories), {{cln}} (for categories beginning with a language name, aka grammar categories) and {{C}} (for categories beginning with a language code, aka topic categories) do this automatically, so using them is preferred.
  • If a template adds a category, but it would be desirable to suppress that category in some cases, it should have a |nocat= parameter which prevents that.
  • If you want to accept a variable number of named parameters of the same type in a template, the first shall be named |foo=, while the rest shall be |foo2=, |foo3=, |foo4= and so on.
  • When naming template parameters (or their values), try referring to #Customary abbreviations instead of inventing your own; likewise, use appropriate codes to refer to languages, scripts and families.
  • Headword-line templates should either be implemented in a separate module using Module:headword, or use {{head}}.
  • Redirects for templates intended for entries should be created sparingly.
  • Avoid doing string processing in templates. If you find yourself in need of a “substring” function, you might have crossed the point where the template should be converted to Lua.

Lua

  • The main table, containing functions available to templates and other modules shall be named export, and declared as a local variable at the top of the module.
  • Exported functions to be exclusively invoked from templates shall accept only one argument, named frame.
  • Code should be indented with tabs.
  • Variables should be explicitly declared local. No exported functions should be declared with the local function construct. Global variables should not be used.
  • Variables holding return values from require() (i.e. imported modules) should have their names prefixed with m_.
  • Words in variable and function names should be separated with underscores. Variables should be all-lowercase unless other reasons dictate otherwise.
  • Unit tests for a module, if any, shall be put on a /testcases subpage.
    • Unit test modules should use Module:UnitTests. See that module’s page for usage details.
    • If a specific kind of test is to be performed repeatedly, it shall have its own method whose name starts with check_.
    • An alternative test framework, Module:ScribuntoUnit, has been imported from Wikipedia and can also be used.
  • When feasible, functions should be written to be callable both from templates and directly from other Lua functions, without creating a frame (or a table imitating one). To accomplish that, check at the very beginning of the function if its first argument is a table, using type() (or check if it equals mw.getCurrentFrame()).
    • Alternatively, you may designate a separate function or module for functions callable from templates. By convention, this is done by creating a submodule named /templates.
  • Conversely, when a native Lua function is available for a particular task, use it directly instead of calling a template. frame:expandTemplate should be used sparingly.
  • Don't forget to monitor Category:Pages with module errors after making substantial edits.

JavaScript

  • Scripts should start with a prologue containing // {{documentation}} <nowiki>, preferably preceded by a strict mode declaration.
  • Enabling at least the following JSHint options is recommended: /*jshint strict:true, undef:true, latedef:true */ /*global mw, jQuery */
    • Preferably, strict mode should be enabled, globally or per-function. This makes the strict JSHint option implicit.
    • Enabling the following options is permitted: boss, loopfunc, debug, sub, scripturl, validthis, unused.
    • Warnings raised by JSHint should be addressed either by code refactoring or enabling an option listed above.
  • Identifiers should use Java camelCase.
  • Code should be indented with tabs. Placement of curly brackets should follow Crockford style, based on K&R: always on the same line as the control structure. Omitting a bracket pair not required by syntax is permissible.
    • Not increasing an indentation level is permissible in cases where mere boilerplate (e.g. a wrapper callback function, or a condition placed over the entirety of code) is what would normally warrant increasing indentation.

Customary abbreviations

  • a – aspect
  • adj – adjective
  • adv – adverb
  • alt – alternative display (link label)
  • c – common gender
  • conj – conjugation or conjunction
  • d or du – dual
  • decl – declension
  • f – feminine
  • g – gender
  • gloss or t – translation gloss (for non-English terms)
  • id – sense ID (see {{senseid}})
  • impf – imperfective
  • infl – inflection
  • lang – language code
  • m – masculine (exception: {{m}} = mention)
  • n – neuter
  • p or pl – plural
  • pf – perfective
  • pos – part of speech
  • pron – pronoun or pronunciation
  • pronunc – pronunciation
  • rf... – request for...
  • s or sg – singular
  • sc – script code
  • tr, translit – transliteration

And above all

VI. Break any of these rules sooner than say anything outright barbarous.