-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchapter.standard.coding.xml
279 lines (264 loc) · 5.77 KB
/
chapter.standard.coding.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: chapter.coding.xml 584 2013-05-15 05:13:17Z netkiller $ -->
<section id="coding">
<title>编码风格</title>
<screen>
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/
</screen>
<section id="java">
<title>java 编程规范</title>
<section>
<title>一个输入多个出口</title>
<programlisting>
<![CDATA[
Object row = input.readLine();
if (row != null) {
if (this.process != null) {
row = this.process.run(row);
}
if (row != null) {
boolean outputStatus = this.output.write(row);
if (this.position != null && outputStatus) {
this.position.set(row);
}
}
}
]]>
</programlisting>
</section>
<section>
<title>Spring Data JPA</title>
<programlisting>
<![CDATA[
@Autowired
private TableRepostitory tableRepostitory;
@Autowired
private JdbcTemplate jdbcTemplate;
@PersistenceContext
private EntityManager entityManager;
]]>
</programlisting>
</section>
</section>
<section id="php">
<title>php 文件</title>
<para>http://www.php-fig.org</para>
<section>
<title>格式与 编码</title>
<para>使用 UNIX 风格换行, 请在你的编辑器内调整</para>
<screen>
UNIX (LF或"\n")
MAC OS (CR 或"\r")
Windows CRLF \r\n
</screen>
<para>源码文件使用 UTF-8</para>
<para>有些IDE环境 UTF-8 BOM</para>
</section>
<section>
<title>循环嵌套</title>
<para>if, while, for, foreach, do ... loop, switch... 等的嵌套必须小于等于3层</para>
<para>如下面的例子,可读性极差。</para>
<screen>
if (xxx){
if (xxx){
if(xxx){
if(xxx){
if(xxx){
}
}
}
if(xxx){
if(xxx){
}
}
}
if (xxx){
if(xxx){
if(xxx){
}
}
if(xxx){
if(xxx){
}
}
}
}
</screen>
<para>加以改造</para>
<screen>
func aaa(p){
if(p){
if(xxx){
}
}
}
func bbb(b){
if(b){
if(xxx){
if(xxx){
}
}
}
}
if(xxx){
aaa(xxx)
}
if(b){
bbb(b)
} </screen>
</section>
<section>
<title>取出行尾的空格以及多余的换行符</title>
<para>一个空格占用一个字节,换行符Window是两个字节\r\n, Unix与Mac占用一个字节</para>
</section>
<section>
<title>php 标签</title>
<para>禁止这样使用</para>
<screen>
<![CDATA[
<?
...
?>
]]>
</screen>
<para>正确的使用方法</para>
<screen>
<![CDATA[
<?php
...
or
<?php
...
?>
]]>
</screen>
</section>
<section>
<title>头部注释</title>
<screen>
<![CDATA[
<?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 $
*/
]]>
</screen>
</section>
</section>
<section id="coding.string">
<title>String</title>
<para>双引号要处理字符串转义,性能上不如单引号,如果你不需要转义字符串,或者字符串中不含原转译字符,建议你使用单引号</para>
<screen>
print("string")
</screen>
<para>每次输出会检索特殊字符串如: \r, \n, \t, \0xFF 等等</para>
<screen>
print('string')
</screen>
</section>
<section id="coding.db">
<title>Database</title>
<para>使用pdo_mysql替代mysql</para>
<para>错误的写法,通过字符串链接拼接sql语句极容易出现注入漏洞</para>
<screen>
$sql = "select * from table where id=".$id;
$sql = "select * from table where id='".$id."'";
$sql = "INSERT INTO fruit(name, colour) VALUES ('".$name."', '".$colour."')";
</screen>
<para>正确的写法</para>
<screen>
$sql = "select * from table where id=?";
$sql = "INSERT INTO fruit(name, colour) VALUES (?, ?)";
</screen>
<screen>
<![CDATA[
$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;
]]>
</screen>
<section>
<title>结果集使用注意事项</title>
<para>返回数据库查询结果有几种形式</para>
<para>数组形式</para>
<screen>
<![CDATA[
Array
(
[0] => banana
[1] => yellow
)
Array
(
[NAME] => banana
[COLOUR] => yellow
)
]]>
</screen>
<para>对象形式</para>
<screen>
Object
(
Obj->NAME
Obj->COLOUR
)
</screen>
<para>正确的使用方式</para>
<screen>
print($row[name])
print($row->name)
</screen>
<para>错误的使用使方式</para>
<screen>
print($row[0])
</screen>
<para>避免使用 "*"查询,一会影响性能,二增加带宽开销</para>
<screen>
$sql = "select * from tab where status=0 limit 1";
</screen>
<para>如果程序使用$row[1]读取结果,有可能当数据库结构改变,增加字段,字段顺序发生变化,输出数据都会出错</para>
</section>
<section>
<title>索引</title>
<para>下面的例子,不会使用索引</para>
<screen>
$sql = "select id, name, created from tab where id != 100";
</screen>
<screen>
EXPLAIN select * from members where id != '1010'; 索引失效
EXPLAIN select count(*) from members where id != '1010'; 索引有效
</screen>
</section>
<section>
<title>缓存</title>
<screen>
</screen>
<para>下面的例子,数据不会缓存查询结果</para>
<screen>
$sql = "select id, name, created from tab where created=now()";
</screen>
</section>
</section>
</section>