Best Practices Philip Windridge
Links Naming Conventions Pseudocode _std.html _std.html A standard style for code written in PHP
References Sommerville, I. 2007, Software engineering, 8th edn, Addison-Wesley, Harlow. Coggeshall, J. et. al 2005, PHP 5 Unleashed, SAMS
Lecture Outcomes To outline and show examples of simple, good practice when developing code.
Where are you? Find that programming is in just a source of mental anguish Find that programming fills a major void in my life I use it as much as I have to and no more I enjoy program- ming, I can clearly see that it will be useful
What are Best Practices? We are not (in the positive sense) hackers So we need to work at… Suitability of code – planning Clarity of code – readability Adherence to standards Keywords Planning, consistency/style, documentation
Plan
Hangman
Hangman (partial) READ guess APPEND guess to end of stringOfGuesses FOR each letter in wordToBeGuessed IF letter is in stringOfGuesses THEN INCREMENT lettersGuessed APPEND letter to end of wordToDisplay IF letter is same as current guess THEN SET letterWasFound to TRUE END IF ELSE APPEND “_” to end of wordToDisplay ENDIF END LOOP
Style
Hangman (partial) $a = $_POST['submit']; $b.= $a; for($i = 0; $i < $c; $i++){ if(strstr($b, $d[$i])){ $e++; $f.= $d[$i]; if($a === $d[$i]){$g = true;}} else {$f.= "_ ";}}
Hangman (partial) $guess = $_POST['submit']; $guesses.= $guess; for($i = 0; $i < $wordToBeGuessed_length; $i++){ if(strstr($guesses, $wordToBeGuessed[$i])){ $lettersGuessed++; $wordToDisplay.= $wordToBeGuessed[$i]; if($guess === $wordToBeGuessed[$i]){ $letterWasFound = true;}} else{ $wordToDisplay.= "_ ";}}
Hangman (partial) $guess = $_POST['submit']; $guesses.= $guess; for ($i = 0; $i < $wordToBeGuessed_length; $i++) { if (strstr($guesses, $wordToBeGuessed[$i])) { $lettersGuessed++; $wordToDisplay.= $wordToBeGuessed[$i]; if ($guess === $wordToBeGuessed[$i]) { $letterWasFound = true; } } else { $wordToDisplay.= "_ "; }
Documentation
Hangman (partial) $guess = $_POST['submit'];//Get the letter guessed $guesses.= $guess;//Add it to the rest of the guesses //Check each letter in the word for ($i = 0; $i < $wordToBeGuessed_length; $i++) {// against all the letters guessed if (strstr($guesses, $wordToBeGuessed[$i])) {//If the letter has been guessed $lettersGuessed++; $wordToDisplay.= $wordToBeGuessed[$i]; if ($guess === $wordToBeGuessed[$i]) {//If current guess is successful $letterWasFound = true; } } else {//if letter doesn’t match any letter $wordToDisplay.= "_ ";//guessed so far }
Standards
Web Standards Responsible designers create web sites that are standards compliant It is better for the web It is possible to design web sites that are not compliant to web standards Why do this? We know better already We are creating web sites that are compliant to XHTML 1.0 Supported by major browsers Comes in Strict, Transitional and Frameset flavours
Web Standards It is not enough to say: “It looks OK with Internet Explorer” or “Checked it with FireFox and it looked ok” It is not enough that mark-up merely looks right in the browser Design for the Internet, design for cross platform compatibility, design for professional pride and most of all… Make your design work in all the ways your users wish to interact with it Do not dictate the Internet to the user – it is not our job to do so!
Being nice to yourselves!
Improving Your Code Use functions where code can be generalised Use separate files for PHP where common code will be used across multiple pages
Improving Your Code Code available within the page User defined functions Code available across a number of web pages include require include_once require_once