Code Formatting Standards - BA-Development-Team/Public_Data GitHub Wiki

Note that formatting standards apply only to the files that we made. We leave all libraries and other files that were not created by us as is.

General

  • All files should end with a newline

  • There should be no trailing whitespace in any file

  • All binary operators should have a space on either side (except for SQL queries of course)

      // Incorrect:
      $foo="";
      // Correct:
      $foo = "";
    
  • Proper tabbing should be used at all times - everything should align neatly

  • SQL commands should be written in uppercase

      // Incorrect:
      $queryString = "select * from sample where something=1";
      // Correct:
      $queryString = "SELECT * FROM sample WHERE something=1";
    
  • All if/else statements should always use braces even if it is for just one line

      // Incorrect:
      if (true) echo "woo!";
      // Correct:
      if (true) {
          echo "woo!";
      }
    
  • Put a space before and after comment indicators

      //This is wrong
      /*This is wrong*/
      // This is correct
      /* This is correct */
    
  • Put a space after commas

      // Incorrect:
      foo($arg1,$arg2,$arg3);
      // Correct:
      foo($arg1, $arg2, $arg3);
    
  • Do not put spaces before/after parentheses

      // Incorrect:
      if ( 1 + 1 === 2 ) {
          foo( $arg1 );
      }
      // Correct:
      if (1 + 1 === 2) {
          foo($arg1);
      }
    

PHP

Basics

  • 4-space tabs

  • same-line start brace syntax:

      function foo() {
          // code
      }
    
  • same-line else syntax

      if ($bool1) {
          // code1
      } else if ($bool2) {
          // code2
      } else {
          // code3
      }
    
  • PHP-only files should not have an end tag (?>)

  • String Concatenation - Put a space before and after each dot

      "this "."is "."incorrect"
      "this " . "is " . "incorrect"
    
  • Quotes - For all Strings except array keys, use double quotes. For array keys, use single quotes.

      $someArray['thisIsCorrect'];
      $someString = "this is some string";
    

Naming Conventions

  • Camel Case will be used for all variable/filenames

      // Camel Case Example
      $exampleOfCamelCase;
      // No underscores
      $this_is_incorrect;
      // Capitalize Each Word
      $thisisnotCorrect;
    
  • Classes will start with a capital letter

  • Everything else will start with a lowercase letter

  • Exception: constant values will be named using all caps underscore notation

      // Constant Example outside of a class
      define('NUMBER_OF_ROWS', '8');
      // Constant Example inside of a class
      const NUMBER_OF_ROWS = 8;
    

JavaScript

All JavaScript code should adhere to the Google JavaScript Style Guide.

Some highlights:

  • 2-space tabs

  • same-line start brace syntax:

      function foo() {
        // code
      }
    
  • same-line else syntax

      if (bool1) {
        // code1
      } else if (bool2) {
        // code2
      } else {
        // code3
      }
    
  • always declare variables using var

  • always use semicolons

  • From the style guide:

    • In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS, foo.namespaceNamesLikeThis.bar, and filenameslikethis.js
  • Prefer single quotes for strings

HTML/CSS

All HTML/CSS should adhere to the Google HTML/CSS Style Guide.

Some highlights:

HTML

  • 2-space tabs

  • all HTML tags in lowercase

  • omit type attributes for style sheets and scripts

  • use double quotes for attribute values in HTML tags

CSS

  • 2-space tabs

  • omit units after 0 values

  • use 3 character hex values where possible (#fff instead of #ffffff)

  • alphabetize declarations