General Coding Guide

Know when to use GET and when to use POST.

  1. With GET, the form data is send as a query, visible in the URL. With POST, the data is sent in the body of the HTTP request, which not visible to the average user. POST is better for sensitive data (like passwords, we should really use SSL).
  2. With GET, there is a limit on length of the URL, and some user dislike long URLs, so use POST if you have to submit a large form.
  3. Use POST if the request would change something in the back-end (modify the database). After processing a POST request, the server should always do redirect. This is to prevent user from bookmarking a POST request. If users are able to bookmark a POST request, and later return to it, he would have modify the back-end database without intending to do so.
  4. Use POST for file upload. (File upload only works with POST)
  5. With POST, when user use the back button, he will see a dialog asking if he want to re-submit the form. This can be annoying.
  6. The browser, and the server have a limit on the length of the URL, so if you are submitting a form that involve a textarea, use POST.
  7. If we use GET request, especially when using AJAX, we have to remember to encode the form value. This can be done using encodeURIComponent.

Know when to use AJAX.

  1. AJAX, generally, improve page performance, because the entire page does not need to be re-rendered. However, if the result of an AJAX call is used to modify a significant area of the page, we should really consider whether we should use AJAX at all. If the result of an AJAX call is used to modify a significant area of the page, and something does not look quite right, or the user want to see the previously displayed data, chance are that he will hit the back button of the browser, which at this point in time (November 3rd, 2009), will not do what the user want it to do. The industry is still trying to make AJAX works with the back button, but that is experimental.

Always put a space between 'if' and the opening paranthesis, and a space between the closing parenthesis and the curly brace:

if ($x == 1) {

Always terminate a statement with semicolon.

Never write code like this:

if ($x == 1)

Although the the curly braces, '{' and '}', might not be required by the language, it is bad programming practice to omit them. So the '{' and '}' are required, and the above code should have been written as:

if ($x == 1) {

Please use:

if (condition) {
} else {

instead of:

if (condition) 
else {

Please do not put an empty line at the end of a function / method.

Keep the code readable by indenting. Configure your text editor or IDE for automatic indentation. Use tabs instead of spaces for indentation. Configure your IDE to display tab as 2 spaces, but preserve the tab in the source file.

Use meaningful and consistent naming conventions. The name of the function should reflect what it does. A function should do a single task without any extra side effect. If a function has side effect, its name should reflect it.

Comment, comment, comment

DRY. DRY stands for Don't Repeat Yourself, and it's a valuable programming concept, no matter what programming language is being used. DRY programming, as the name implies, is ensuring that you don't write duplicated code.

Validate on both the Client and Server. Validation is the bane of forms. But it is best to check the input both on the client and on the server. Validating on the client side allows you to warn the user of mistakes before they submit the form, which requires one less transaction with the server. However, always be sure to validate on the server as well, for security sake.

Avoid long argument list. Long argument list is bad because we have to remember to pass the parameters in the correct order. If we need to pass a lot of arguments, pass an associative array (for PHP code), or a JSON object (for JavaScript code).

Code Reuse
Loose Coupling

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License