GTU PHP Project Training Guidelines

Page 1

By TOPS Technologies

1

TOPS Technologies:http://www.tops-int.com/phptraining-course.html

http://www.tops-int.com/php-training-course.html

9/11/2013

GTU GUIDELINES FOR PHP CODING‌


PHP CODING GUIDELINES

TOPS Technologies:http://www.topsint.com/php-training-course.html

These are the guidelines that you should follow when writing your PHP scripts, unless a coding standard already exists for the project working on. It can be helpful to have something like this if you’re working on a joint project. N.B. These are only the guidelines that we personally chose to follow for the code when I was student in GTU. This is not an indication that any other coding styles, guidelines or standards are wrong. Feel free to use this document as a template to manage your own coding guideline and change whatever you wish as you see fit.

9/11/2013

2


WHY ARE GUIDELINES IMPORTANT? TOPS Technologies:http://www.topsint.com/php-training-course.html

First of all, let’s make one point crystal clear: it doesn’t matter what your guidelines are, so long as everyone understands and sticks to them. I’ve worked on a team where the person in charge preferred to put braces following the expression, rather than on a line all by themselves. Whilst I didn’t necessarily agree with this convention, I stuck to it because it made maintaining the whole project much easier.

9/11/2013

3


WHY ARE GUIDELINES IMPORTANT?

TOPS Technologies:http://www.topsint.com/php-training-course.html

It cannot be emphasised enough that guidelines are only useful if they are followed. It’s no use having a definitive set of coding guidelines for a joint programming project if everyone continues to write in their own style regardless. It is arguable that you can get away with different coding styles if every team member works on a different section which is encapsulated and therefore their coding style doesn’t affect the other developers. Unfortunately, this only holds true until a developer leaves and someone else has to take over their role.

9/11/2013

4


WHY ARE GUIDELINES IMPORTANT? TOPS Technologies:http://www.topsint.com/php-training-course.html

If you are running a joint project, you might consider putting your foot down and basically refuse to accept any code that does not conform to your published guidelines, regardless of its technical merit (or lack thereof). This may sound somewhat draconian and off-putting to developers at first, but once everyone begins to code to the guidelines you’ll find it a lot easier to manage the project and you’ll get more work done in the same time. It will require a lot of effort from some of your developers who don’t want to abandon their coding habits, but at the end of the day different coding styles will cause more problems than they’re worth.

9/11/2013

5


EDITOR SETTINGS

9/11/2013

TOPS Technologies:http://www.topsint.com/php-training-course.html

Tabs v. spaces  Linefeeds

6


TABS V. SPACES TOPS Technologies:http://www.topsint.com/php-training-course.html

Ahh, the endless debate of tabs v. spaces. I used to be a fan of tabs, but I’ve come round to the argument that spaces are better — apart from anything else you can guarantee that they will look the same regardless of editor settings. The other benefit to using two spaces (which is the number I work with) is that code doesn’t start to scroll off the right side of the screen after a few levels of indentation.

9/11/2013

7


LINEFEEDS

I use simple newlines all the time, because the Windows way just doubles the size of your line breaks and the Mac OS way is technically incorrect in that a carriage return should only return you to the beginning of the line, ala the old typewriter systems.

If you develop on Windows (and many people do), either set up your editor to save files in Unix format or run a utility that converts between the two file formats.

TOPS Technologies:http://www.topsint.com/php-training-course.html

The three major operating systems (Unix, Windows and Mac OS) use different ways to represent the end of a line. Unix systems use the newline character (\n), Mac systems use a carriage return (\r), and Windows systems use a carriage return followed by a line feed (\r\n). If you’ve ever opened a file created in Windows on a Unix system, you will probably have seen lots of odd characters (possibly represented by ^M) where you would expect to see a clean line break.

9/11/2013

8


NAMING CONVENTIONS 9/11/2013

Variable names  Loop indices  Function names  Function arguments 

TOPS Technologies:http://www.topsint.com/php-training-course.html

9


VARIABLE NAMES

Variable names should be all lowercase, with words separated by underscores. For example, $current_user is correct, but $currentuser, $currentUser and $CurrentUser are not.

Names should be descriptive, but also concise. Wherever possible, keep variable names to under 15 characters, although be prepared to sacrifice a few extra characters to improve clarity. There’s no hard and fast rule when it comes to the length of a variable name, so just try and be as concise as possible without affecting clarity too much. Generally speaking, the smaller the scope of a variable, the more concise you should be, so global variables will usually have the longest names (relative to all others) whereas variables local to a loop might have names consisting only of a single character.

Constants should follow the same conventions as variables, except use all uppercase to distinguish them from variables. So USER_ACTIVE_LEVEL is correct, but USERACTIVELEVEL oruser_active_level would be incorrect.

TOPS Technologies:http://www.topsint.com/php-training-course.html

A lot of textbooks (particulary those about Visual C++) will try to drum Hungarian notationinto your head. Basically, this means having rules such as pre-pending g_ to global variables, i to integer data types etc. Not only is a lot of this irrelevant to PHP (being a typeless language), it also produces variable names such as g_iPersonAge which, to be honest, are not easy to read at a glance and often end up looking like a group of random characters strung together without rhyme or reason.

9/11/2013

10


LOOP INDICES •

TOPS Technologies:http://www.topsint.com/php-training-course.html

9/11/2013

This is the only occassion where short variable names (as small as one character in length) are permitted, and indeed encouraged. Unless you already have a specific counting variable, use $i as the variable for the outermost loop, then go onto $j for the next most outermost loop etc. However, do not use the variable $l (lowercase ‘L’) in any of your code as it looks too much like the number ‘one’. Example of nested loops using this convention: for ( $i = 0; $i < 5; $i++ ) { for ( $j = 0; $j < 4; $j++ ) { for ( $k = 0; $k < 3; $k++ ) { for ( $m = 0; $m < 2; $m++ ) { foo($i, $j, $k, $m); } } } } If, for some reason, you end up nesting loops so deeply that you get to $z, consider re-writing your code. I’ve written programs (in Visual Basic, for my sins) with loops nested four levels deep and they were complicated enough. If you use these guidelines in a joint project, you may way to impose an additional rule that states a maximum nesting of xlevels for loops and perhaps for other constructs too.

11


FUNCTION NAMES

TOPS Technologies:http://www.topsint.com/php-training-course.html

names should follow the same guidelines as variable names, although they should include a verb somewhere if at all possible. Examples include get_user_data() andvalidate_form_d ata(). Basically, make it as obvious as possible what the function does from its name, whilst remaining reasonably concise. For example,a_function_which_gets_user_data _from_a_file() would not be appropriate!

9/11/2013

 Function

12


FUNCTION ARGUMENTS Since function arguments are just variables used in a specific context, they should follow the same guidelines as variable names.  It should be possible to tell the main purpose of a function just by looking at the first line, e.g. get_user_data($username). By examination, you can make a good guess that this function gets the user data of a user with the username passed in the $username argument.  Function arguments should be separated by spaces, both when the function is defined and when it is called. However, there should not be any spaces between the arguments and the opening/closing brackets. 

9/11/2013 TOPS Technologies:http://www.topsint.com/php-training-course.html

13


CODE LAYOUT Including braces  Where to put the braces  Spaces between tokens  Operator precedence  SQL code layout 

9/11/2013 TOPS Technologies:http://www.topsint.com/php-training-course.html

14


SQL CODE LAYOUT

TOPS Technologies:http://www.topsint.com/php-training-course.html

When writing SQL queries, capitialise all SQL keywords (SELECT, FROM, VALUES, AS etc.) and leave everything else in the relevant case. If you are using WHERE clauses to return data corresponding to a set of conditions, enclose those conditions in brackets in the same way you would for PHP if blocks, e.g. SELECT * FROM users WHERE ( (registered = 'y') AND ((user_level = 'administrator') OR (user_level = 'moderator')) ).

9/11/2013

•

15


GENERAL GUIDELINES 9/11/2013 TOPS Technologies:http://www.topsint.com/php-training-course.html

Quoting strings  Shortcut operators  Optional shortcut constructs  Use constants where possible  Turn on all error reporting 

16


SIDE EFFECTS OF SHORT-CIRCUIT LOGIC EVALUATION 

9/11/2013 TOPS Technologies:http://www.topsint.com/php-training-course.html

One feature of PHP that can catch even expert developers out, as well as being hard to track down, is the shortcuts taken when evaluating boolean expressions. This is when PHP stops evaluating a boolean expression part way through because it already knows the result. For example, if you have two expressions combined with the && (AND) operator, you know that if the first expression is false then the whole thing must be false because anything AND’d with false is false. This short-circuit evaluation can catch you out if one or more of the expressions performs and operation as part of being evaluated. For example, $a = 5 sets the value of $a to 5 andevaluates to 5. The safest route is to perform all of your operations first, store any results in variables and then evaluate them.

17


BIO 9/11/2013 TOPS Technologies:http://www.topsint.com/php-training-course.html

In detailed knowledge and guidelines kindly visit us at your nearest center.  You can visit us at • http://www.tops-int.com • http://www.tops-int.com/php-training-course.html  We also Do live project training, free workshop and seminar hosted by our former students and faculties every Saturday to share their knowledge and experience with newbie's. 

18


FOR MORE VISIT US AT •

TOPS Technologies:http://www.topsint.com/php-training-course.html

http://www.tops-int.com http://www.tops-int.com/php-training-course.html

9/11/2013

19


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.