Code style

From Freephile Wiki
Jump to navigation Jump to search

When doing Continuous Integration, or really any development process where you want to enforce a coding standard (aka coding conventions, or code style), you want to be able to do that automatically. You might have code reviews in your process, but you don't want engineers using eyeballs to check the syntax of code and brace style etc. -- that is so last century. Developer tools like jscodesniffer or PHP CodeSniffer can be used to clean up old inconsistent code.

While PHP CodeSniffer is great for PHP projects, you can use an EditorConfig file in your project which is supported natively by most leading IDEs and many open source projects for all programming languages. See https://editorconfig.org/ on how to systematically define and enforce your coding style and syntactic standards.

jscodesniffer is pretty cool in that it's a nodejs app can be easily integrated with your Jenkins environment [1], or used with a commit hook in your Version Control system.

MediaWiki editorconfig[edit | edit source]

Here is an example of an editor config file. This is the one used by the MediaWiki project

root = true

[*]
indent_style = tab
indent_size = tab
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# Ensure text editors don't turn leading spaces into tabs,
# e.g. in multi-line bullet list items
[*.md]
indent_style = space
indent_size = 2

# Tabs may not be valid YAML
# @see https://yaml.org/spec/1.2/spec.html#id2777534
[*.{yml,yaml}]
indent_style = space
indent_size = 2

[.git/**]
indent_style = space
indent_size = 2

# Trailing whitespace is intended in parser test files
# T278066
[**/tests/parser/*.txt]
trim_trailing_whitespace = false

Plugin for vim[edit | edit source]

Want a plugin for vim? https://github.com/editorconfig/editorconfig-vim

References[edit source]

  1. The example given is using it with the static code analyzer 'CheckStyle' for Java projects via the CheckStyle plugin for Jenkins. However, you can have Jenkins employ jscodesniffer on any source