Coding Standards
in WordPress
@swapypatil
Swapnil V. Patil
patilswapnilv@gmail.com
What are coding
standards?
@swapypatil
Coding conventions are a set of guidelines for a specific
programming language that recommend programming
style, practices, and methods for each aspect of a program
written in that language. These conventions usually cover
file organization, indentation, comments, declarations,
statements, white space, naming conventions,
programming practices, programming principles,
programming rules of thumb, architectural best practices,
etc.
@swapypatil
Have you ever came across something
like this?
Have you ever came across
something like this?
THIS ITEM IS
REDUCED DUE
TO MISSPELLING
OF THE
WORD BIRD.
THANK YOU.
“
Coding Standards in
WordPress
Why are
Important?
Transition headline
The best applications are coded
properly. This sounds like an obvious
statement, but by ‘properly’, I mean
that the code not only does its job
well, but is also easy to add to,
maintain and debug.
Hello!
I am Swapnil Patil
I am here because I love the
open source community.
You can find me at:
@swapypatil
Transition headline
When we learn a new language, we usually begin to
code in a specific style. In most cases, we’ll write in a
style that we want, not one that has been suggested to
us.
But once we start to code using a particular style, like
a spoken language dialect, it will become second
nature — we’ll use that style in everything we create.
Such a style might include the conventions we use to
name variables and functions ($userName, $username
or $user_name for example), and how we comment
our work. Any style should ensure that we can read our
code easily.
Transition headline
A coding standards document tells developers how
they must write their code.
Instead of each developer coding in their own
preferred style, they will write all code to the standards
outlined in the document.
This makes sure that a large project is coded in a
consistent style — parts are not written differently by
different programmers.
Not only does this solution make the code easier to
understand, it also ensures that any developer who
looks at the code will know what to expect throughout
the entire application.
Transition headline
Can you actually read the code? Is it spaced out clearly?
o Do you separate blocks of code into ‘paragraphs’ so that
different sections are easily defined?
o Are you using indentation to show where control structures
(if, else, while and other loops) begin and end, and where the
code within them is?
o Are your variable naming conventions consistent throughout
the code and do they briefly describe that data that they’ll
contain?
o Are functions named in accordance with what they do?
Transition headline
If you come back to the code in a
few weeks or months, will you be
able to work out what’s happening
without needing to look at every line?
Transition headline
How are you commenting
the work?
Transition headline
How are you commenting
the work?
Transition headline
Have you used complex
language functions/constructs
that are quicker to write but
affect readability?
Transition headlineCreate Maintainable Code
Transition headline
Transition headline
Here are two ways we might write the same code — you can
see the difference between easily readable code and complex,
but more quickly written code. Even if you don’t know much
PHP, you probably noted that the second snippet is much tidier
and easier to understand. I’ve named functions using a verb
(get, display), used variable names that describe the data they
contain, and used brackets to help show what the for condition
is. Not only that, but I’ve also included the braces for the control
structures and used indentation to show which code appears
under each structure.
Transition headline
I have my own personal coding standards, because I liked the way
they were written. For example, in my own standards, I put the
braces for control structures on a new line:
Transition headline
But in the WordPress standards, the first brace is on the
same line as the if (condition):
“
Coding standards are great — but how
do you decide which standards you
want to apply, and how they will be
defined? When you formulate your
ideal coding style, you should think
about these points:
Transition headline
https://codex.wordpress.org/WordPress_Coding_Standards
https://make.wordpress.org/core/handbook/best-
practices/coding-standards/
https://make.wordpress.org/core/handbook/best-
practices/coding-standards/php/
https://varyingvagrantvagrants.org/
https://github.com/Varying-Vagrant-Vagrants/VVV
Thanks!
Any questions?
You can find me at:
@swapypatil on Twitter
patilswapnilv@gmail.com

WordCamp Pune 2017- WordPress Coding standards

  • 1.
    Coding Standards in WordPress @swapypatil SwapnilV. Patil patilswapnilv@gmail.com
  • 2.
  • 3.
    Coding conventions area set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. These conventions usually cover file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices, programming principles, programming rules of thumb, architectural best practices, etc. @swapypatil
  • 4.
    Have you evercame across something like this?
  • 5.
    Have you evercame across something like this? THIS ITEM IS REDUCED DUE TO MISSPELLING OF THE WORD BIRD. THANK YOU. “
  • 6.
  • 7.
    Transition headline The bestapplications are coded properly. This sounds like an obvious statement, but by ‘properly’, I mean that the code not only does its job well, but is also easy to add to, maintain and debug.
  • 8.
    Hello! I am SwapnilPatil I am here because I love the open source community. You can find me at: @swapypatil
  • 9.
    Transition headline When welearn a new language, we usually begin to code in a specific style. In most cases, we’ll write in a style that we want, not one that has been suggested to us. But once we start to code using a particular style, like a spoken language dialect, it will become second nature — we’ll use that style in everything we create. Such a style might include the conventions we use to name variables and functions ($userName, $username or $user_name for example), and how we comment our work. Any style should ensure that we can read our code easily.
  • 10.
    Transition headline A codingstandards document tells developers how they must write their code. Instead of each developer coding in their own preferred style, they will write all code to the standards outlined in the document. This makes sure that a large project is coded in a consistent style — parts are not written differently by different programmers. Not only does this solution make the code easier to understand, it also ensures that any developer who looks at the code will know what to expect throughout the entire application.
  • 11.
    Transition headline Can youactually read the code? Is it spaced out clearly? o Do you separate blocks of code into ‘paragraphs’ so that different sections are easily defined? o Are you using indentation to show where control structures (if, else, while and other loops) begin and end, and where the code within them is? o Are your variable naming conventions consistent throughout the code and do they briefly describe that data that they’ll contain? o Are functions named in accordance with what they do?
  • 12.
    Transition headline If youcome back to the code in a few weeks or months, will you be able to work out what’s happening without needing to look at every line?
  • 13.
    Transition headline How areyou commenting the work?
  • 14.
    Transition headline How areyou commenting the work?
  • 15.
    Transition headline Have youused complex language functions/constructs that are quicker to write but affect readability?
  • 16.
  • 17.
  • 18.
    Transition headline Here aretwo ways we might write the same code — you can see the difference between easily readable code and complex, but more quickly written code. Even if you don’t know much PHP, you probably noted that the second snippet is much tidier and easier to understand. I’ve named functions using a verb (get, display), used variable names that describe the data they contain, and used brackets to help show what the for condition is. Not only that, but I’ve also included the braces for the control structures and used indentation to show which code appears under each structure.
  • 19.
    Transition headline I havemy own personal coding standards, because I liked the way they were written. For example, in my own standards, I put the braces for control structures on a new line:
  • 20.
    Transition headline But inthe WordPress standards, the first brace is on the same line as the if (condition):
  • 21.
    “ Coding standards aregreat — but how do you decide which standards you want to apply, and how they will be defined? When you formulate your ideal coding style, you should think about these points:
  • 22.
  • 23.
    Thanks! Any questions? You canfind me at: @swapypatil on Twitter patilswapnilv@gmail.com