Program Structure and Techniques

Download Report

Transcript Program Structure and Techniques

Best Practices
Philip Windridge
Links

Naming Conventions
 http://support.microsoft.com/kb/q173738/
 http://en.wikipedia.org/wiki/CamelCase

Pseudocode
 http://www.csc.calpoly.edu/~jdalbey/SWE/pdl
_std.html

A standard style for code written in PHP
 http://pear.php.net/manual/en/standards.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?
I use it as
much as I
have to and
no more
Find that
programming is
in just a source
of mental
anguish
I enjoy programming, I can clearly
see that it will be
useful
Find that
programming
fills a major
void in my life
What are Best Practices?
We are not (in the positive sense) hackers
 So we need to work at…

of code – planning
 Clarity of code – readability
 Suitability
Adherence to standards
 Keywords

 Planning,
consistency/style,
documentation
Plan
Hangman
Hangman
Do 1, then repeat 2 until either word correctly guessed
or maximum of wrong guesses reached, then do 3
1
2
3
Initialise game
Play game
Do 1.1, then 1.2
1.1
1.2
Get word to guess
Display initial
screen
Output result
Do 2.1, then do 2.2 then do 2.3
2.2
2.1
Read guess
Process guess
2.3
Display ongoing
game state
Do 2.2.1 for each letter in word to be guessed, then do 2.2.2
2.2.1
Compare guess to
current word letter
2.2.2
Process guessed
letter
Do 2.2.3.1, then do 2.2.3.2 if a
correct guess wasn’t made this turn
Do 2.2.1.1 if a word letter matches
a guess otherwise do 2.2.1.2
2.2.1.1
Register match
Do 2.2.1.1.1 if the guess made this turn matches
the word letter, do 2.2.1.1.2, then do 2.2.1.1.3
2.2.1.1.1
Correct guess =
true
2.2.1.1.2
Increment letters
guessed count
2.2.1.1.3
Display letter in
word to be guessed
2.2.1.2
Register difference
Do 2.2.1.2.1
2.2.1.2.1
Display blank in
word to be guessed
2.2.3.1
Remove letter from
potential guesses
2.2.3.2
Increment wrong
guesses count
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 .= "_&nbsp;";}}
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 .= "_&nbsp;";}}
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 .= "_&nbsp; ";
}
}
Documentation
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 .= "_&nbsp; ";
}
}
//Get the letter guessed
//Add it to the rest of the guesses
//Check each letter in the word
// against all the letters guessed
//If the letter has been guessed
//If current guess is successful
//if letter doesn’t match any letter
//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