Coding-Guidelines

Spielregeln

  • Code muss mit PHP 5.2 und MySQL 5.0 (mit InnoDB) kompatibel sein.
  • error_reporting = E_ALL | E_STICT | E_NOTICE (für PHP 5.3-Nutzer sollte auch E_DEPRECATED aktiviert sein)
  • display_errors = On
  • Dateien sind als UTF-8 ohne BOM zu verfassen.
  • Warnungen und Notices sind die kleinen Schwestern von Fatal und Error und sollten ausgemerzt werden, bevor sie ihe Brüder rufen.
  • Trailing Whitespace sollte vom Editor beim Speichern entfernt werden.
  • Benutze Tabs zur Einrückung und Leerzeichen zum Ausrichten deines Codes!

PHP

<?php
/*
 * Copyright (c) 2010, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/* align the equal signs with SPACES only */

$myVar           = 'myvalue';
$anotherVariable = 5;
$anObjectToGo    = new stdClass();

switch ($anotherVariable) {
    case 1:
        doSomething();
        break;

    case 2:
        doSomethingOther();
        break;
}

/* It's okay to omit the brackets if your line doesn't get ridiculously long. */

if ($shortCond) shortAction();
else another();

/* But never do this: */

if ($short) action();
else {
   /* grr(); */
}

if ($reallyLongAndStupid) {
   /* muh() */
}
else foo();

/* Either use both or use none, but don't mix it! */
/* And now play attention to the whitespaces... */

function mySuperAwesomeFunction($does, $stuff, $with = true, $things = array()) {
    // I'm a comment and indented with a single [TAB].
    /* Me too! */

    if ($does && $stuff) {
        // Ha, I've got two [TAB]s!
        /* My [TAB]s can beat your [TAB]s! */

        foreach ($things as $key => $value) {
             print $value;
        }
    }
    else {
        callingAnotherFunction();
    }

    for ($i = 0; $i < count($things); ++$i) {
        /* Shame on me, did not store count() in a variable! */
    }
}

/* BTW: It doesn't matter which comment style you use. */

$foo = array(1, 2, 3);
$bar = array(
    'indented' => 'with a tab',
    'me'       => 'too' // There should be ONLY spaces between 'me' and the arrow!
);

$anotherVar = $inlineConditions ? $are : $okayIfNotTooLong;

class MyClass {
    public $foo;
    protected $bar;
    private $muh;

    /* normal methods should look like this: */

    public function __construct($a, $b) {
        $this->foo = (int) $a;
        $this->bar = (int) $b;
        $this->muh = 4;
    }

    /* It's okay to put many similar functions like this */

    public function getFoo() { return $this->foo; }
    public function getBar() { return $this->bar; }
    public function getMuh() { return $this->muh; }

    /* Be reasonable when deciding how far to go with alignments! */

    public function getXYZ()       { return $this->xyz;       }
    public function getSomething() { return $this->something; }

    public function getADamnLongVariableSoItDoesNotMakeSenseToAlignTheOthers() {
        return 7;
    }
}

/* Inside of templates, you should use the alternative control structure. */

if (true):
else:
endif;

/* end each file with one empy line*/
_

SQL

INSERT INTO mytable (id, bar) VALUES (1, 'hallo welt');

-- or use different quotes
INSERT INTO `mytable` (`id`, `bar`) VALUES (1, "hallo welt");

CREATE TABLE bar (
  id INT AUTO_INCREMENT,
  PRIMARY KEY(id)
);

Stelle sicher, dass dein SQL keine Zeilenumbrüche enthält, wenn du es PHP-seitig zusammensetzt. Das macht es schwer möglich, den SQL-Log im Fehlerfall auszuwerten. Verwende lieber

<?
$query =
    'SELECT * FROM mytable WHERE 1 AND '.
    'x = 4 AND foo = "bar"';

JavaScript

(function() {
    /* keep your code in your own anonymous function */

    var x = 5, y = 6; // try to use a single var statement per scope

    function bar(a, b) {
        return a + b;
    }

    jQuery('#myselector').click(function() {
        console.log('clicked!'); /* never in production code! */
        var $this = $(this);    /* <-- NEVER WRITE THIS. That is just fucking confusing... Use 'self' instead. */
    });
})();

/* if you use jQuery, use one of the following */

/* this one runs IMMEDIATELY after loading the JS file */

(function($) {
    $(...);
})(jQuery);

/* this one fires when DOM:LOADED is fired */

jQuery(function($) {
    $(...);
});

XHTML

<lowercase><please also="for attribute names" /></lowercase>
<!-- close tags like the <please>-tag above, with one spacing before the '/' -->
<!-- write XHTML 1.0 Strict compliant code -->
<!-- indentation is also your friend -->

<body>
<head>
	<title>I am indented with a [TAB]!</title>
	<link href="..." />
	<script type="..." src="..."></script>
</head>
<body id="my-page" class="can-beat-your-page">
</body>
</html>

CSS

CSS wird automatisch via CSScaffold verarbeitet. Daher sind Dinge wie verschachtelte Selektoren oder andere Spielereien möglich, die jeden CSS-Parser aus dem Konzept bringen. Nicht wundern, wenn das Syntax-Highlighting im Editor der Wahl nicht mehr zu gebrauchen ist.

.class {
	.tab-indented {
		#my-id {
			color: blue;
			margin: 10px;
			background: red;
		}

		/* Comments should still be written in C style */

		/* Multiple short definitions can be written in a block */

		.short { single: definition; }
		.wee   { another: def; }
		.tiny  { please: endthis; }
	}

	&-pay-attention { /* <- results in 'class-pay-attention' */
		hard-to: find-this-classname;
	}
}

Das obige Beispiel-CSS würde (unter Missachtung einer CSS-Validierung) folgendes ergeben:

.class .tab-indented #my-id{color:blue;margin:10px;background:red}
.class .tab-indented .short{single:definition}
.class .tab-indented .wee{another:def}
.class .tab-indented .tiny{please:endthis}
.class-pay-attention{hard-to:find-this-classname}

Inhalt

Vorheriges Thema

An der Dokumentation mitarbeiten

Nächstes Thema

Beispieldatei

Diese Seite