Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why you should learn to love coding standards

Luca Ricci
October 27, 2018

Why you should learn to love coding standards

“How many people have heard about, and even understand coding standards, but nonetheless ignore them? Until a few months ago, Luca was definitely one of these people! As project size and complexity scales, and you’re working with other developers, designers and stakeholders, coding standards become increasingly important and will make your life much easier.
In this talk, Luca will give you an overview of why it’s important to follow coding standards, and deliver some practical examples of how they can improve your developing skills. Luca will also share some tools and workflows to make the job easier.
At the end of the talk, attendees will be able to integrate these tools into their day-to-day workflow.”

Luca Ricci

October 27, 2018
Tweet

More Decks by Luca Ricci

Other Decks in Technology

Transcript

  1. Why you should learn to love coding standards Luca Ricci

    | Full stack developer | @theskinnyghost | @pragmaticweb | WordCamp Manchester 2018 | #WCMCR
  2. Table of contents • What are Coding Standards? • Why

    are they important? • WordPress Coding Standards • How to get started • Q&A
  3. Coding standards are a set of guidelines that developers adhere

    to when writing source code for a project.
  4. • Improve readability. • Improve maintainability. • Save time (and

    money). • Help team member integration. Why are they important?
  5. • Improve readability. • Improve maintainability. • Save time (and

    money). • Help team member integration. • Security. Why are they important?
  6. • Improve readability. • Improve maintainability. • Save time (and

    money). • Help team member integration. • Security. • Code quality. Why are they important?
  7. “All code in any code-base should look like a single

    person typed it, no matter how many people contributed.” Principles of Writing Consistent, Idiomatic JavaScript https://github.com/rwaldron/idiomatic.js/
  8. What is TIDE? Tide is a service, consisting of an

    API, Audit Server, and Sync Server, working in tandem to run a series of automated tests against the WordPress.org plugin and theme directories. Through the Tide plugin, the results of these tests are delivered as an aggregated score in the WordPress admin that represents the overall code quality of the plugin or theme. https://make.wordpress.org/tide/ #tide on Slack See it in action
  9. PHP Coding Standards JS Coding Standards CSS Coding Standards HTML

    Coding Standards WordPress Coding Standards
  10. PHP Coding Standards • Use tabs not spaces • Use

    single and double quotes when appropriate.
  11. PHP Coding Standards • Use tabs not spaces • Use

    single and double quotes when appropriate. • Use elseif, not else if
  12. PHP Coding Standards • Use tabs not spaces • Use

    single and double quotes when appropriate. • Use elseif, not else if • Don’t use extract()
  13. • Use tabs not spaces • Use single and double

    quotes when appropriate. • Use elseif, not else if • Don’t use extract() • No shorthand PHP tags PHP Coding Standards
  14. • Use tabs not spaces • Use single and double

    quotes when appropriate. • Use elseif, not else if • Don’t use extract() • No shorthand PHP tags • Yoda conditions PHP Coding Standards
  15. “Yoda conditions are dumb and solve the wrong problem. You

    have my permission to not use Yoda conditions.” Ryan McCue
  16. • No whitespace at the end of line or on

    blank lines. JavaScript Coding Standards
  17. • No whitespace at the end of line or on

    blank lines. • Use semicolons. Never rely on Automatic Semicolon Insertion JavaScript Coding Standards
  18. • No whitespace at the end of line or on

    blank lines. • Use semicolons. Never rely on Automatic Semicolon Insertion • Strict equality checks (===) must be used in favor of abstract equality checks (==). JavaScript Coding Standards
  19. • No whitespace at the end of line or on

    blank lines. • Use semicolons. Never rely on Automatic Semicolon Insertion • Strict equality checks (===) must be used in favor of abstract equality checks (==). • Chain Method Calls when they don’t fit in one line. JavaScript Coding Standards
  20. • Each selector should be on its own line, ending

    in either a comma or an opening curly brace. CSS Coding Standards
  21. • Each selector should be on its own line, ending

    in either a comma or an opening curly brace. • Properties should be followed by a colon and a space. CSS Coding Standards
  22. • Each selector should be on its own line, ending

    in either a comma or an opening curly brace. • Properties should be followed by a colon and a space. • Properties should be logically ordered. CSS Coding Standards
  23. Properties should be logically ordered. Above all else, choose something

    that is meaningful to you and semantic in some way. Random ordering is chaos, not poetry. In WordPress Core, our choice is logical or grouped ordering, wherein properties are grouped by meaning and ordered specifically within those groups. The properties within groups are also strategically ordered to create transitions between sections, such as background directly before color. The baseline for ordering is: • Display • Positioning • Box model • Colors and Typography • Other
  24. • Each selector should be on its own line, ending

    in either a comma or an opening curly brace. • Properties should be followed by a colon and a space. • Properties should be logically ordered. • Use double quotes rather than single quotes, and only when needed. CSS Coding Standards
  25. HTML Coding Standards • All tags and attributes must be

    written in lowercase. • Self-closing tags must have a space before the forward slash.
  26. HTML Coding Standards • All tags and attributes must be

    written in lowercase. • Self-closing tags must have a space before the forward slash. • All attributes must have a value, and must use double- or single-quotes.
  27. PHP_CodeSniffer is an essential development tool that ensures your code

    remains clean and consistent. PHP Code Sniffer
  28. PHP_CodeSniffer is an essential development tool that ensures your code

    remains clean and consistent. • phpcs detects violations of a defined coding standards. PHP Code Sniffer
  29. PHP_CodeSniffer is an essential development tool that ensures your code

    remains clean and consistent. • phpcs detects violations of a defined coding standards. • phpcbf automatically corrects violations. PHP Code Sniffer
  30. REQUIREMENTS PHP_CodeSniffer requires PHP version >= 5.4.0. (However individual sniffs

    may have additional requirements such as external applications and scripts) PHP Code Sniffer
  31. INSTALLATION • Directly using curl or wget • Composer, globally

    or project • Homebrew https://github.com/squizlabs/PHP_CodeSniffer#installation PHP Code Sniffer
  32. USAGE • CLI • Editor or IDE integrations • WP

    Enforcer or GrumpPHP PHP Code Sniffer
  33. You can also.. • Ignore files • Disable parts of

    a file • Disable specific rules PHP Code Sniffer
  34. You can also.. • Ignore files • Disable parts of

    a file • Disable specific rules • Writing reports PHP Code Sniffer
  35. You can also.. • Ignore files • Disable parts of

    a file • Disable specific rules • Writing reports • Lint CSS/JS PHP Code Sniffer
  36. WPCS • WordPress - complete set with all of the

    sniffs in the project • WordPress-Core - main ruleset for WordPress core coding standards • WordPress-Docs - additional ruleset for WordPress inline documentation standards • WordPress-Extra - extended ruleset for recommended best practices, not sufficiently covered in the WordPress core coding standards • WordPress-VIP - extended ruleset for WordPress.com VIP coding requirements https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
  37. WPCS - Ignore rules • Escaping / XSS // WPCS:

    XSS ok. • Sanitization // WPCS: sanitization ok. • Nonce verification / CSRF // WPCS: CSRF ok. • Overriding WordPress globals // WPCS: override ok. https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
  38. • phpcs-neutron-standard: A set of phpcs sniffs for PHP >

    7 development • PHP Compatibility • PHP Compatibility WP • phpcs-security-audit: Finds vulnerabilities and weaknesses related to security in PHP • codor: Sniff to help find Code Smells • phpcs-calisthenics-rules: Focuses of maintainability, readability, testability and comprehensibility Useful packages
  39. .editorconfig EditorConfig helps developers define and maintain consistent coding styles

    between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.