Home | 簡體中文 | 繁體中文 | 雜文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | Email

第 13 章 編碼風格

目錄

13.1. php 檔案
13.1.1. 格式與 編碼
13.1.2. 循環嵌套
13.1.3. 取出行尾的空格以及多餘的換行符
13.1.4. php 標籤
13.1.5. 頭部註釋
13.2. String
13.3. Database
13.3.1. 結果集使用注意事項
13.3.2. 索引
13.3.3. 緩存
https://code.google.com/p/google-styleguide/
http://lxr.linux.no/linux/Documentation/CodingStyle
http://perldoc.perl.org/perlstyle.html
http://www.gnu.org/prep/standards/
	

13.1. php 檔案

13.1.1. 格式與 編碼

使用 UNIX 風格換行, 請在你的編輯器內調整

UNIX (LF或"\n")
MAC OS (CR 或"\r")
Windows CRLF \r\n
			

源碼檔案使用 UTF-8

有些IDE環境 UTF-8 BOM

13.1.2. 循環嵌套

if, while, for, foreach, do ... loop, switch... 等的嵌套必須小於等於3層

如下面的例子,可讀性極差。

if (xxx){
	if (xxx){
		if(xxx){
			if(xxx){
				if(xxx){

				}
			}
		}
		if(xxx){
			if(xxx){
			}
		}
	}
	if (xxx){
		if(xxx){
			if(xxx){
			}
		}
		if(xxx){
			if(xxx){
			}
		}
	}
}
			

加以改造

func aaa(p){
	if(p){
		if(xxx){
		}
	}
}
func bbb(b){
	if(b){
		if(xxx){
			if(xxx){

			}
		}
	}
}

if(xxx){
	aaa(xxx)
}
if(b){
	bbb(b)
}			

13.1.3. 取出行尾的空格以及多餘的換行符

一個空格占用一個位元組,換行符Window是兩個位元組\r\n, Unix與Mac占用一個位元組

13.1.4. php 標籤

禁止這樣使用

			
<?
...
?>
			
			

正確的使用方法

			
<?php
...

or

<?php
...
?>
			
			

13.1.5. 頭部註釋

			
<?php
/**
 * Project Name
 *
 * @author     $Author: netkiller $
 * @copyright  Copyright (c) 2012 Company
 * @version    $Id: chapter.coding.xml 584 2013-05-15 05:13:17Z netkiller $
 */

<?php
/**
 * Project Name
 *
 * @author     $Author: netkiller $
 * @license    GNU General Public License 2.0
 * @version    $Id: chapter.coding.xml 584 2013-05-15 05:13:17Z netkiller $
 */
			
			
comments powered by Disqus