-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadable code.html
More file actions
211 lines (181 loc) · 6.74 KB
/
readable code.html
File metadata and controls
211 lines (181 loc) · 6.74 KB
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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>readable code — 一瞬上がった賢さを永続化</title>
<meta name="author" content="cadenacchi">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/favicon.png" rel="icon">
<link href="/theme/css/main.css" media="screen, projection"
rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic"
rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic"
rel="stylesheet" type="text/css">
</head>
<body>
<header role="banner"><hgroup>
<h1><a href="/">一瞬上がった賢さを永続化</a></h1>
</hgroup></header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
</ul>
<ul class="main-navigation">
<li >
<a href="/category/pelican.html">Pelican</a>
</li>
<li class="active">
<a href="/category/programming.html">Programming</a>
</li>
</ul></nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">readable code</h1>
<p class="meta">
<time datetime="2016-05-15T18:00:00+09:00" pubdate>日 15 5月 2016</time> </p>
</header>
<div class="entry-content"><h2>名前に情報を詰め込む</h2>
<h4>抽象的ではなく具体的な名前を使う</h4>
<div class="highlight"><pre><span></span># getだと、どこからpageをとってくるかわからない
def get_page():
pass
# インターネットからとってくるならdownloadにすべき
def download_page():
pass
</pre></div>
<h4>名前に情報を追加する</h4>
<p>変数の意味を間違ってしまったときにバグが起こる場合は情報を追加したほうがよい</p>
<p>htmlのコードをutf-8に変えた
html ⇒ html_utf8</p>
<h4>名前の長さを決める</h4>
<ul>
<li>スコープが小さければ短い名前でもよい</li>
<li>プロジェクト固有の省略形はやめる</li>
<li>常識的な省略形はOK(例: stringのかわりにstrはOK)</li>
<li>不要な単語を捨てる(例: ConvertToString() ⇒ ToString())</li>
</ul>
<h4>名前のフォーマットで情報を伝える</h4>
<p>エンティティごとにことなるフォーマットを用意することで、名前から理解を深められる</p>
<ul>
<li>クラス名はCamelCase</li>
<li>変数名はlower_separated</li>
</ul>
<h2>誤解されない名前</h2>
<h4>限界値を含めるときは<code>min_*</code>と<code>max_*</code>を使う</h4>
<h4>範囲を指定するときは<code>first</code>と<code>last</code>を使う</h4>
<p><code>start</code>,<code>stop</code>では終わりが曖昧</p>
<h4>包含/排他的範囲には<code>begin</code>と<code>end</code>を使う</h4>
<h4>ブール値の変数名には、<code>is</code>・<code>has</code>・<code>can</code>・<code>should</code>をつける</h4>
<h4>プログラマの期待に合わせる</h4>
<p>例えば<code>get*()</code>は軽量アクセサであるという期待がある</p>
<h2>美しさ</h2>
<p>TODO</p>
<h2>コメントすべきことを知る</h2>
<p>TODO</p>
<h2>コメントは正確に</h2>
<p>TODO</p>
<h2>制御フローは読みやすく</h2>
<h4>if/else ブロックの並び順</h4>
<ul>
<li>条件は否定形よりも肯定形を使う</li>
<li>関心を引く条件は先にかく</li>
</ul>
<h4>do/whileループを避ける</h4>
<h4>ネストは浅くする</h4>
<ul>
<li>早期リターン</li>
<li>ループ内部のネストを削除する</li>
</ul>
<div class="highlight"><pre><span></span># 変更前
for i in range(n):
if results[i]:
non_null_count += 1
if results[i]['name'] == ""
pass
# 変更後
for i in range(n):
if not results[i]:
continue
non_null_count += 1
if results[i]['name'] == ""
pass
</pre></div>
<h2>巨大な式を分割する</h2>
<h4>説明変数</h4>
<p>可読性のために式の意味を表す変数を用意しておく</p>
<div class="highlight"><pre><span></span># 変更前
if line.split(':')[0].strip() == "root":
pass
# 変更後
user = line.split(':')[0].strip()
if user == "root":
pass
</pre></div>
<h4>要約変数</h4>
<p>何度も使う式は要約しておく</p>
<h2>変数と読みやすさ</h2>
<p>TODO</p>
<h2>無関係の下位問題を抽出する</h2>
<ul>
<li>関数を見て、「このコードの高レベルの目標は何か」と自問する</li>
<li>コードの各行に対して「高レベルの目標に直接的に効果があるのか」と自問する</li>
<li>無関係の下位問題を解決しているコードが相当量あれば、それらを抽出して別の関数にする</li>
</ul>
<h2>一度に1つのことを</h2>
<p>TODO</p></div>
<footer>
<p class="meta">
<span class="byline author vcard">
Posted by <span class="fn">
Cadenacchi
</span>
</span>
<time datetime="2016-05-15T18:00:00+09:00" pubdate>日 15 5月 2016</time> <span class="categories">
<a class='category' href='/category/programming.html'>programming</a>
</span>
<span class="categories">
<a class="category" href="/tag/programming.html">programming</a> </span>
</p><div class="sharing">
</div> </footer>
</article>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/readable code.html">readable code</a>
</li>
<li class="post">
<a href="/first-post.html">最初のPelicanブログ</a>
</li>
</ul>
</section>
<section>
<h1>Categories</h1>
<ul id="recent_posts">
<li><a href="/category/pelican.html">Pelican</a></li>
<li><a href="/category/programming.html">programming</a></li>
</ul>
</section>
<section>
<h1>Tags</h1>
<a href="/tag/pelican.html">pelican</a>, <a href="/tag/programming.html">programming</a> </section>
</aside> </div>
</div>
<footer role="contentinfo"><p>
Copyright © 2014–2016 cadenacchi —
<span class="credit">Powered by <a href="http://getpelican.com">Pelican</a></span>
</p></footer>
<script src="/theme/js/modernizr-2.0.js"></script>
<script src="/theme/js/ender.js"></script>
<script src="/theme/js/octopress.js" type="text/javascript"></script>
</body>
</html>