Clean Code Practices
Since we use Bootstrap/Bootscore as a starter for many of our themes and we operate inside of WordPress, we should follow similar language style conventions to make things uniform.
Clean Code Practices π
Here’s a good summary of Robert Martin’s book, Clean Code.
- Add type declarations to function arguments in PHP
- Comment your code according JSDoc/PHPDoc syntax so VS Code’s Intelephense can pick up function description and argument expectations on hover.
- β οΈ Function/Variable names should be descriptive enough that the code reads like good documentation β οΈ
- Don’t be lazy about this, as comments are prone to grow stale or be moved separately from their related functions.
- All names, regardless of which language we’re writing in, should be in English and not Choctaw, as none of the developers (at the time of writing) are fluent Choctaw speakers.
The Ternary Operator
Read more about the ternary operator.
PHP π
General Conventions
- If WPCS is giving you a red squiggly line, change your code to get rid of it!
// phpcs:ignore
should almost never be used. - Comment your files! Copying and Pasting without an edit is poor practice and makes it harder to keep track of where you areβespecially if you’re copying a bootscore/plugin file to make changes to.
- Always prefer
snake_case
for function/variable names overcamelCase
orkebab-case
(WPCS will squiggle this, anyways). - Place as much PHP in the top of a file as possible so the the HTML can remain, largely, pure HTML.
- If the top of a file becomes inordinately long (e.g. 20+ lines), consider trying to refactor into something else.
- Stay in PHP as long as you can (e.g. your top-matter should only have the opening
<?php
and closing tag at the end. - The last line should generally be
get_header('some_header') ?>
whenever possible, to signify to the author that we are breaking into the HTML matter.
Breaking Changes π¨
Prefer single line statements. Ternary operators can be used if it increases readability (i.e. keeps things on one line).
Mixed Markup (HTML + PHP)
- In the case of single-line HTML + PHP, consider using string literals to enhance readability and reduce silly errors (e.g. a floating
>
after dropping in-and-out of PHP and HTML for an<a>
tag
Refactoring
- Theme functions and plugin-like files (e.g. CPT Declarations) should live inside
inc/
folder and appropriate sub-directories.
CSS π
General Conventions
- Prefer SCSS over pure CSS
- Always uses
kebab-case
, nevercamelCase
for class/id names - Override Bootstrap variables
- Use Bootstrap utility classes wherever possible
- Use
@use
over@import
wherever possible - Bootstrap is, unfortunately, built on top of
@import
statements for simple overrides, but this is poor practice as@import
will be deprecated. Typically, I (KJ) have found that I only really need thebreakpoints
mixins, and that is easy enough to copy into my ownmixins.scss
file to reuse with the@use
rule. Also, since Bootstrap is open-source, it’s totally legal to do so π
File Architecture π
- The top-level scss file (named either
main.scss
should@use
all components that are global to a site. Thestyles/pages
directory should@use
styles that simply override globals or contain page-specific styles. - The
src/styles
folder is the root directory for all SCSS/CSS files, and should utilize the 7-1 Architecture pattern.- We may not have all 7 folders, but this convention is still helpful to use, especially when telling Webpack what to look for.
Enqueuing Styles (with PHP π)
global.css
should be enqueued dist/css
folder as second-to-last in the chain.
Enqueue Order:
- /dist/vendors/*.css
- dist/css/global.css
- style.css (required by WordPress)
Each php (π) page should enqueue its own css (π) on the line before get_header('some-header') ?>
so the page only ships with the css it needs. Thus, the Enqueue Order might be understood as:
1. vendors.css
2a. dist/css/global.css
2b. dist/css/page/page.css
3. style.css (required by WordPress)
Where 2b
is dynamically injected on each page by the server.