Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

11.11. 標準與規範

11.11.1. Redis Key

Redis Key 使用“:”分割例如

set SMS:CAPTCHA 1234
		

11.11.2. 錯誤編碼

格式 ABCD

A: 編碼

1 用戶錯誤

2 網絡錯誤

3 系統錯誤

4 應用伺服器錯誤

5 應用程序錯誤

6 緩存錯誤

7 資料庫錯誤

8 搜索引擎

B 編碼

0 成功

1 失敗

剩餘 C D 用戶自行編碼

11.11.3. HTML 標準

11.11.3.1. 校驗

https://html5.validator.nu/?doc=https%3A%2F%2Fwww.netkiller.cn%2Findex.html	
		

11.11.3.2. XHTML/HTML

11.11.3.3. CSS

11.11.3.4. Script

11.11.4. 編碼風格

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/
	

11.11.4.1. java 編程規範

11.11.4.1.1. Spring Data JPA
			
	@Autowired
	private TableRepostitory tableRepostitory;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@PersistenceContext 
	private EntityManager entityManager; 
			
			

11.11.4.2. php 檔案

http://www.php-fig.org

11.11.4.2.1. 格式與 編碼

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

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

源碼檔案使用 UTF-8

有些IDE環境 UTF-8 BOM

11.11.4.2.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)
}			
11.11.4.2.3. 取出行尾的空格以及多餘的換行符

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

11.11.4.2.4. php 標籤

禁止這樣使用

			
<?
...
?>
			
			

正確的使用方法

			
<?php
...

or

<?php
...
?>
			
			
11.11.4.2.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 $
 */
			
			

11.11.4.3. String

雙引號要處理字元串轉義,性能上不如單引號,如果你不需要轉義字元串,或者字元串中不含原轉譯字元,建議你使用單引號

print("string")
		

每次輸出會檢索特殊字元串如: \r, \n, \t, \0xFF 等等

print('string')
		

11.11.4.4. Database

使用pdo_mysql替代mysql

錯誤的寫法,通過字元串連結拼接sql語句極容易出現注入漏洞

$sql = "select * from table where id=".$id;
$sql = "select * from table where id='".$id."'";
$sql = "INSERT INTO fruit(name, colour) VALUES ('".$name."', '".$colour."')";
		

正確的寫法

$sql = "select * from table where id=?";
$sql = "INSERT INTO fruit(name, colour) VALUES (?, ?)";
		
		
$sql = <<<____SQL
     CREATE TABLE IF NOT EXISTS `ticket_hist` (
       `tid` int(11) NOT NULL,
       `trqform` varchar(40) NOT NULL,
       `trsform` varchar(40) NOT NULL,
       `tgen` datetime NOT NULL,
       `tterm` datetime,
       `tstatus` tinyint(1) NOT NULL
     ) ENGINE=ARCHIVE COMMENT='ticket archive';
____SQL;
		
		
11.11.4.4.1. 結果集使用注意事項

返回資料庫查詢結果有幾種形式

數組形式

			
Array
(
    [0] => banana
    [1] => yellow
)

Array
(
    [NAME] => banana
    [COLOUR] => yellow
)
			
			

對象形式

Object
(
	Obj->NAME
	Obj->COLOUR
)
			

正確的使用方式

print($row[name])
print($row->name)
			

錯誤的使用使方式

print($row[0])
			

避免使用 "*"查詢,一會影響性能,二增加頻寬開銷

$sql = "select * from tab where status=0 limit 1";
			

如果程序使用$row[1]讀取結果,有可能當資料庫結構改變,增加欄位,欄位順序發生變化,輸出數據都會出錯

11.11.4.4.2. 索引

下面的例子,不會使用索引

$sql = "select id, name, created from tab where id != 100";
			
EXPLAIN select * from members where id != '1010';			索引失效
EXPLAIN select count(*) from members where id != '1010';	索引有效
			
11.11.4.4.3. 緩存

			

下面的例子,數據不會緩存查詢結果

$sql = "select id, name, created from tab where created=now()";
			

11.11.5. 安全

11.11.5.1. Interface

11.11.5.2. SQL注入