23
Coding Standards in WordPress @swapypatil Swapnil V. Patil [email protected]

WordCamp Pune 2017- WordPress Coding standards

Embed Size (px)

Citation preview

Coding Standards in WordPress

@swapypatil

Swapnil V. [email protected]

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 headlineHow are you commenting

the work?

Transition headlineHow 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

[email protected]