Help:Lua/Lua best practice/ru

Как и на любой другой такой сборник, на эту статью, содержащую советы и рекомендации по применению Lua, повлияли мнения отдельных составителей по поводу того, какие практики использования следует рекомендовать. Для своего конкретного проекта вы можете использовать такие методы работы, которые сочтёте нужными, но не забывайте, что работоспособность перечисленных в этой статье принципов подтверждена редакторами на собственном опыте!

Приведённые в статье примеры используют язык программирования Lua, но их несложно переформулировать, например, для JavaScript, PHP или Python. Причина, по которой здесь приведены примеры на Lua, в том, что модули на этом языке широко используются на вики-проектах, причём нередко людьми с небольшим опытом программирования, не всегда способными осознавать последствия своих решений в этой области.

Общий совет: может оказаться очень полезным скопировать код в полноценную среду разработки и проверить его там на предмет очевидных ошибок и недостатков. Существует ряд подходящих для этого бесплатных программ, среди которых Eclipse, TextMate и Visual Studio Code.

При написании кода следует воспользоваться статическим анализатором, например lualint или luacheck; последний из этих двух немного лучше. Также проверьте, нет ли для вашей среды разработки инструментов для обнаружения ошибок или другого анализа кода.

И помните, использование одного и того же стиля программирования в каком-либо проекте важнее использования того стиля, который вы считаете правильным, как бы вы его не предпочитали!

Названия

 * Названия должны быть наглядными
 * Используйте наглядные названия и избегайте чрезмерно общих названий наподобие, если только ваш код не является действительно настолько общим. Не повторяйте одни и те же термины на нескольких уровнях, например, не включайте название класса в название метода, если только этот метод не возвращает объект этого класса. Используйте ёмкие слова, более наглядные в широкой области употребления, но в то же время выбрасывайте из названий слова, которые там не нужны.


 * Краткость названий
 * Избегайте использования коротких и малопонятных названий. Не используйте чрезмерных сокращений, но оставайтесь немногословными. Не называйте переменную, лучше назовите её   или  . Нередко можно переформулировать названия групп, не делая их длинными, например   или  . Как пример, используйте множественное число в названиях таблиц, содержащих несколько значений.


 * Названия переменных цикла
 * Переменные цикла в Lua обычно имеют короткие названия, и эти названия имеют определённое значение. Использование одних и тех же названий в нескольких вложенных циклах, скорее всего, приведёт к проблемам. Чтобы этого избежать, нередко в начало или в конец названий таких переменных дописывают дополнительную информацию, обычно зависящую от того, какого рода данные обрабатываются в цикле. Например, переменная цикла может быть названа формой единственного числа названия таблицы, по элементам которой проходит цикл.


 * Названия с указанием на тип значения
 * В Lua используется довольно слабая система типов, даже если она в некоторых случаях выглядит сильной. Переданные функции аргументы при возможности следует привести к нужному типу, а при невозможности вызвать ошибку. Если у функции есть несколько версий, отличающихся обрабатываемыми типами, это следует указать в названиях вариантов функции.


 * Сокращения как названия
 * Аббревиатуры бесполезны для тех, кто не знает их значения. Не используйте их, кроме как в самых простых и очевидных случаях. В библиотеке для создания HTML-разметки функция  вполне может существовать, но называть так же функцию в библиотеке для построения информационных карточек было бы неуместно.


 * Форматирование названий
 * Форматирование названий должно следовать принципам, принятым в проекте. Lua использует несколько стилей названий, но на проектах Викимедиа наиболее распространено именовать классы в UpperCamelCase, функции, методы и переменные в lowerCamelCase, как и названия модулей. Определённых принципов именования констант не сложилось, но использование UPPER_CASE_EMBEDDED_UNDERSCORE проблем вызвать не должно.


 * Одно нижнее подчёркивание
 * В Lua название, состоящее только из одного символа нижнего подчёркивания, используется тогда, когда предоставить название переменной необходимо, но сама переменная использоваться не будет. Нижнее подчёркивание — название-заполнитель, впоследствии игнорируемое. Приведённый принцип обработки таких названий распознаётся некоторыми статическими анализаторами, и в обычном коде его следует соблюдать.


 * Leading underscore
 * By convention a leading underscore is used for names that are somewhat private but of some reason must be placed so it is accessible from the outside. Variables with such names should be treated as private to whatever defined them, unless they have some very clear explanation that say you can do some specific operations on them.

Formatting

 * Limit line length
 * Lines should be limited to a reasonable length, typically around 80 characters. The reason is that long lines are hard to read, and as they grow longer they tend to contain more intermingled concepts, and editing them will tend to create new bugs.


 * Indentation of newlines
 * Programmers have opinions on how to do indentation of newlines. Whatever opinion you have, do remember that crowdsourced projects should adhere to some common standards. Note that while a tab looks like the same as 4 spaces in the online editor, it does not show up like this in the normal view. Because the online editor do indentation in specific ways you should set your programming environment to act the same way.


 * Binaries and line breaks
 * Sometimes it is necessary to put a line break inside an expression, typically an if-clause. This pose a special problem, as it opens up for copy-paste induced errors. To avoid problems a simple formatting tricks can be used, simply put the logical binary operator in front of the expression on a new line.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Whitespace
 * Whitespace is usually a GoodThing™, but people tend to disagree on how to use it. Done right it tends to increase readability of your code, but done wrong your code can be really hard to read. Find a style guide and stick to it. Or use a tool.
 * Whitespace
 * Whitespace is usually a GoodThing™, but people tend to disagree on how to use it. Done right it tends to increase readability of your code, but done wrong your code can be really hard to read. Find a style guide and stick to it. Or use a tool.

Documentation

 * Interface documentation
 * The interface is the exposed functions from the module, that is the functions you can invoke from the parser function. As a minimum document whatever arguments the interface function takes, and whatever value it returns. The parser function will only use a single returned value, no matter what the function returns. Remember to document if and how the metafunction  will transform the return value.


 * Overview documentation
 * The documentation should have an overview to give a general explanation of why and how the module, function, or variable is the way it is. This is not just the intended behaviour, it is why the behaviour is the way it is.


 * Parameter annotation
 * Each parameter should be properly annotated. That is the expected type should be given and a short descriptive string. Write as if LDoc or a similar tool was available, usually as.


 * Return annotation
 * Each return value should be properly annotated. That is the expected type should be given and a short descriptive string. Write as if LDoc or a similar tool was available, usually as.

Comments
Inline comments for the code itself.


 * Why-form of comments
 * Comments are not for you who code some algorithm, they are for the one who reads your code. In the code path there should be answers to the how-questions, and in the comments there should be answers to the why-questions. Do not fall in the trap of replicating what you do in the code path in the comments, good comments provide new insight into the code.


 * State your intent
 * State the intent of your code, in a clear and precise language. Do not just add a lot of words, think about the reader, what do she need to know to understand your code.


 * Refactor on comments
 * When the code has become to complex, it is common to start explaining the code for yourself. If you need your own comments, then something has gotten to involved and you should refactor the code.


 * Comments on bad names
 * A comment saying that a name is bad in some respect does not help. Do not comment on bad names, fix them!


 * "Todo" comments
 * The marker todo is one of several common ones, and can be interpreted as "code the main programmer has not gotten around to fix yet". Usually you should write it as.


 * "Fixme" comments
 * The marker fixme is one of several common ones, and can be interpreted as "code other than main programmer has identified as broken". Usually you should write it as . This is an invitation to other to try to fix the code.


 * "Hack" comments
 * The marker hack is one of several common ones, and can be interpreted as "code other than main programmer has identified as broken and tried fixing in an unelegant way". Usually you should write it as . This is an invitation to other to try to fix the code.


 * "XXX" comments
 * The marker xxx is one of several common ones, and can be interpreted as "code identified as dangerously broken". Usually you should write it as . This marker should not be in production code, the code should be fixed asap.


 * Comments on magic numbers
 * Constants and other magic numbers should have comments explaining why they have the specific content. Why is the line length set to 80 chars? Why is π truncated to 3.14?

Code
Small patterns and coding practices.


 * Software patterns
 * A lot of work has gone into identifying common software design patterns, and developing good general solutions to those patterns. If you suspect you are attempting to do something that has a common pattern then do a search and check out how to implement the pattern. Still, know that patterns for languages with closures can be very different from those without closures. Usually tables are used in Lua to create objects, but they can also be created as closures.


 * String quotes
 * In Lua there are several types of string quotes. Pick one of them and stick with it. If you need several types of quotes, either use your typical quote at the outermost level or at the innermost, and work your way out. Inside out with single quote as the primary quote and double quotes as secondary seems to be common, but could simply accidental. If more quotes are necessary, then start using double square brackets at the ternary level.


 * Return early
 * Return as soon as possible, especially if something fails. Usually programmers are told to stick with a single return point, but due to the language constructs in Lua this seems to create deeply nested code. It is thus better to return early.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Avoid "else"
 * All conditionals can be written so en explicit else-clause goes away. All of them. But sometimes the else-clause makes the code easier to read or make double set of if-clauses go away. In those cases it is acceptable to keep the, otherwise it should be removed. Be always suspicious about else-clauses.
 * Avoid "else"
 * All conditionals can be written so en explicit else-clause goes away. All of them. But sometimes the else-clause makes the code easier to read or make double set of if-clauses go away. In those cases it is acceptable to keep the, otherwise it should be removed. Be always suspicious about else-clauses.


 * Truthy expressions first
 * Usually you are free to order the order of then- and else-clauses as you see fit, and then you should order them so the if-clause has a positive test. This makes the code easier to read. If you can avoid the else-clause that is although more important.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Interrogated first
 * Usually the interrogated value goes first in a logical expression, as this is more natural to read and thus less error prone. The inverted form is sometimes called the Yoda form. Sometimes (but rarely) the natural form does not work as expected, and the code terminates. This can be because Lua fails figure out a proper tail recursive form of the expression, and then the stack overflows.
 * {| class="wikitable mw-collapsible mw-collapsed"
 * Usually the interrogated value goes first in a logical expression, as this is more natural to read and thus less error prone. The inverted form is sometimes called the Yoda form. Sometimes (but rarely) the natural form does not work as expected, and the code terminates. This can be because Lua fails figure out a proper tail recursive form of the expression, and then the stack overflows.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Provide defaults
 * In languages with an  operator like in Lua, it is very easy to provide defaults. This is nice as some common constructs fails badly if given an uninitialized value. This happen for example when a table is uninitialized and the code tries to index the nil value.
 * {| class="wikitable mw-collapsible mw-collapsed"
 * In languages with an  operator like in Lua, it is very easy to provide defaults. This is nice as some common constructs fails badly if given an uninitialized value. This happen for example when a table is uninitialized and the code tries to index the nil value.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Binary conditional
 * Lua does not have a binary conditional operator, but you can do the same anyhow with an . What goes on is although a little mysterious to newcomers to Lua. The operators   and   pass on truthy values (materialized values), and thus they can be part of other expressions than just logical ones.
 * {| class="wikitable mw-collapsible mw-collapsed"
 * Lua does not have a binary conditional operator, but you can do the same anyhow with an . What goes on is although a little mysterious to newcomers to Lua. The operators   and   pass on truthy values (materialized values), and thus they can be part of other expressions than just logical ones.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Ternary conditional
 * Lua does not have a ternary conditional operator, but you can do the same anyhow with an  and an  . What goes on is although a little mysterious to newcomers to Lua. The operators   and   pass on truthy values (materialized values), and thus they can be part of other expressions than just logical ones.
 * {| class="wikitable mw-collapsible mw-collapsed"
 * Lua does not have a ternary conditional operator, but you can do the same anyhow with an  and an  . What goes on is although a little mysterious to newcomers to Lua. The operators   and   pass on truthy values (materialized values), and thus they can be part of other expressions than just logical ones.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Avoid repeat…until
 * The loop construct  delays the test until after the loop. This is error prone and should be avoided. The only times this is acceptable is when the test itself is costly compared to the executed block, or avoiding the repeat-clause would imply additional cleanup or finalize code.
 * Avoid repeat…until
 * The loop construct  delays the test until after the loop. This is error prone and should be avoided. The only times this is acceptable is when the test itself is costly compared to the executed block, or avoiding the repeat-clause would imply additional cleanup or finalize code.


 * Test arguments
 * There is a small utility libraryUtil to test arguments types, and it should be used to avoid simple mistakes. Often the arguments are valid when other code is correct, but in fringe cases something creates a call outside the expected type range and the code fails in mysterious ways. Proper testing might catch an error early, and increase the probability of discovering the real root cause. Still note that type testing is a test for failure, and not a test for correctness.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * }
 * }

Patterns
A few very common software patterns.


 * Command pattern
 * The single most common software pattern in Lua modules, that is most commonly missing, is the command pattern. Some data is available, and some action should be chosen given this data. A typical smell that a command pattern is missing is long chains of if-then-elseif-end tests. The most common variant does not have named commands, but an accept-qualifier function. Both versions are very common in parsing arguments from the "invoke" parser function.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example
 * }
 * }
 * }
 * }
 * }

Testability
About testing and how to make the code testable. Note that the original book from the gang of four has software patterns for strongly typed languages, which does not fit a weakly typed language very well. Whether Lua is strongly typed, or weakly, is up for debate.


 * Complexity
 * There are usually several execute paths through a given code, and this makes the code difficult to understand. The more code paths the more difficult the code become. This can be measured as cyclomatic complexity, and is a count of paths by counting branch points. This gives a fair, but not very good measure on code quality.


 * Avoid globals
 * Functions with globals are hard to test, no matter how the globals are included in the code. To avoid this accidental trap libraries can be included to wet the module, like . Still note that requiring some code is like importing a global, so be careful not to create a bigger problem.
 * {| class="wikitable mw-collapsible mw-collapsed"

!colspan="2"|Example !Good !Bad
 * - valign="top"
 * }
 * Avoid changing scope
 * In Lua it is possible to check the scope of a function. Don't do that, as the function will then depend on "mini-globals", which is nearly as bad as globals. If everything works as it should, then the mini-globals are probably semi-constant, but there are no such guarantee.
 * Avoid changing scope
 * In Lua it is possible to check the scope of a function. Don't do that, as the function will then depend on "mini-globals", which is nearly as bad as globals. If everything works as it should, then the mini-globals are probably semi-constant, but there are no such guarantee.

Security
Lua runs in a kind of sandboxed environment, you may say that the Lua code executes in a dmz on the outside of the usual server, where only a few calls pass through the wall and those should be properly validated by the internal code. In general there should be no reason to take any special precautions to make the code secure, as the result from the code execution will pass through proper cleaning and escaping before being included on the page.

Programming environment
Some advices for how to set up your external programming environment.

Visual Studio Code
Use something like the following extensions.
 * Lua
 * vscode-luacheck
 * This needs a file  with a single line , otherwise it will complain a lot about the   global variable.
 * Lua snippets for Wikimedia

Other tools
This is tools you might find useful.
 * GitHub: LuaDist/luametrics

Reading

 * Kerievsky, Joshua; Refactoring to Patterns
 * Boswell, Dustin; Foucher, Trevor; The Art of Readable Code
 * Stefanov, Stoyan; JavaScript Patterns
 * Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John; Design Patterns: Elements of Reusable Object-Oriented Software
 * Programming in Lua (5.0)
 * Lua 5.1 Reference Manual
 * LuaUsers: Sample Code
 * PEP 8 -- Style Guide for Python Code
 * LuaUsers: Lua Style Guide
 * The Power of Ten – Rules for Developing Safety Critical Code