Skip to content
This repository has been archived by the owner on Sep 24, 2023. It is now read-only.

Coding Practices

Matt Nadareski edited this page May 4, 2017 · 8 revisions

Coding Practices

Below is a list of coding practices for this project in no necessary order:

Brackets

The proper way to use brackets { } is to put them on the next new line not next to the statement.

INCORRECT

for ($i = 0; $i < 5; $i++) {
    // code }

CORRECT

for ($i = 0; $i < 5; $i++)
{
    // code
}

Naming

Function naming should use camel case, not an underscore to separate words

INCORRECT

function getDir (param1)
{
    // CODE
}

CORRECT

function get_dir (param1)
{
    // CODE
}

Inline Spacing

Use the following examples for spacing that should be followed:

EXAMPLES

foreach ($x = 0; $x < 5; $x++)
$x = explode("/n", $array)
if ($x == 5 || ($x + 5 < 11 && $x + 3 < 8))
$x = array[$x]
$x = array[$x - 1]

Comments

Comment code as often as necessary to clarify what functions or code blocks do. More specifically, place a block comment before all PHP code to explain the purpose of the page, any necessary parameters, and any developer notes and TODOs. Try to avoid placing notes and TODOs in the code itself unless it is required for clarity.

  • Comment code as often as necessary to clarify what functions or code blocks do.

EXAMPLES

// Create or update a table in the database
function create_update_table($tablename)
{
    // First, we check if the table exists
    ...

    // If it does, update the table
    ...

    // If it doesn't, create the table
    ...
}

Organization

  • Avoid duplicating code. If the code is used within the same page multiple times, create a function and abstract it. Similarly, if code is duplicated across multiple pages, either put a function in one of the pages and call it from the other, or create a helper page and put the function in there.
  • A class should represent a single function or set of highly related functions. For example, a page should not contain importing and exporting DATs, but having two different files for exporting different types of DAT is unnecessary. If it's an edge case, contact the project lead for the official ruling.

Code Additions

  • Be sure to look around the codebase before deciding to add a new function or page. Oftentimes, the code already contains something that you can use instead of creating your own version. In the case that it doesn't do exactly what you need, look to see if that function can be changed without affecting current usage. If all else fails, THEN you can create the new function or page.
Clone this wiki locally