rabbit-slide-kou-schoo-readable-code-2015-04 2015.4.3.1 → 2015.4.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/chapter-7.rab +103 -61
- data/config.yaml +1 -1
- data/pdf/schoo-readable-code-2015-04-chapter-7.pdf +0 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6d3ad0c9ee6f98d0020259259c4648843cc5c2b
|
4
|
+
data.tar.gz: 2741d1c843db3c8570c7b299150e8d12483b497c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31cb8f4afd0157ac5bc93950865d36321da556b1c22b7b2e202de84a6fde0f1ce1b44fe16f32f19156a9c0de45bb7b00b4e00ef6a1874ac94dea31d8ec482cbc
|
7
|
+
data.tar.gz: a391a026bbcc32bcc4a4519fa2be58f82366f9e8a7235b1901e529ecd684ef8f52203a9bdc3258bca1f43de532327a403e4ff94896f5082278d8ee9c7b31f2cd
|
data/chapter-7.rab
CHANGED
@@ -142,17 +142,15 @@
|
|
142
142
|
|
143
143
|
// どちらがリーダブルコード?どうして?
|
144
144
|
// リーダブルコード:変更できる・バグを見つけられるコード
|
145
|
-
// A
|
146
|
-
Node* node = list->head;
|
147
|
-
if (node == NULL) return;
|
148
|
-
while (node->next != NULL) {
|
149
|
-
Print(node->data);
|
150
|
-
node = node->next;
|
151
|
-
}
|
152
|
-
if (node != NULL)
|
153
|
-
|
154
|
-
for (Node* node = list->head; node != NULL; node = node->next)
|
155
|
-
Print(node->data);
|
145
|
+
// A // B
|
146
|
+
Node* node = list->head; | for (Node* node = list->head;
|
147
|
+
if (node == NULL) return; | node != NULL;
|
148
|
+
while (node->next != NULL) { | node = node->next)
|
149
|
+
Print(node->data); | Print(node->data);
|
150
|
+
node = node->next; |
|
151
|
+
} |
|
152
|
+
if (node != NULL) |
|
153
|
+
Print(node->data); |
|
156
154
|
|
157
155
|
(('tag:center'))(('note:「優れた」コードって何? p. 2より'))
|
158
156
|
|
@@ -251,33 +249,59 @@
|
|
251
249
|
* 左側:変化する式
|
252
250
|
* 右側:変化しにくい式
|
253
251
|
|
254
|
-
= 7.2
|
252
|
+
= 7.2 例:処理の順番(1)
|
255
253
|
|
256
254
|
# coderay c
|
257
255
|
|
258
|
-
/*
|
256
|
+
/* どちらがリーダブル? */
|
257
|
+
/* A */ | /* B */
|
259
258
|
if (a == b) { | if (a != b) {
|
260
259
|
/* ケース1 */ | /* ケース2 */
|
261
260
|
} else { | } else {
|
262
261
|
/* ケース2 */ | /* ケース1 */
|
263
262
|
} | }
|
264
263
|
|
265
|
-
|
266
|
-
どちらがリーダブル?
|
267
|
-
|
268
|
-
= 7.2 例:処理の順番
|
264
|
+
= 7.2 例:処理の順番(1)
|
269
265
|
|
270
266
|
# coderay c
|
271
267
|
|
272
|
-
/*
|
268
|
+
/* A:条件が肯定形だから */
|
269
|
+
/* A */ | /* B */
|
273
270
|
if (a == b) { | if (a != b) {
|
274
271
|
/* ケース1 */ | /* ケース2 */
|
275
272
|
} else { | } else {
|
276
273
|
/* ケース2 */ | /* ケース1 */
|
277
274
|
} | }
|
278
275
|
|
279
|
-
|
280
|
-
|
276
|
+
= 7.2 例:処理の順番(2)
|
277
|
+
|
278
|
+
# coderay c
|
279
|
+
|
280
|
+
/* どちらがリーダブル? */
|
281
|
+
/* (ケース1の方が処理が長い) */
|
282
|
+
/* A */ | /* B */
|
283
|
+
if (a == b) { | if (a != b) {
|
284
|
+
/* ケース1 */ | /* ケース2 */
|
285
|
+
/* ... */ | } else {
|
286
|
+
/* ... */ | /* ケース1 */
|
287
|
+
} else { | /* ... */
|
288
|
+
/* ケース2 */ | /* ... */
|
289
|
+
} | }
|
290
|
+
|
291
|
+
= 7.2 例:処理の順番(2)
|
292
|
+
|
293
|
+
# coderay c
|
294
|
+
|
295
|
+
/* B:単純な処理の条件が先 */
|
296
|
+
/* if/elseを一望しやすい */
|
297
|
+
/* A */ | /* B */
|
298
|
+
if (a == b) { | if (a != b) {
|
299
|
+
/* ケース1 */ | /* ケース2 */
|
300
|
+
/* ... */ | } else {
|
301
|
+
/* ... */ | /* ケース1 */
|
302
|
+
} else { | /* ... */
|
303
|
+
/* ケース2 */ | /* ... */
|
304
|
+
} | }
|
281
305
|
|
282
306
|
= 7.2 指針
|
283
307
|
|
@@ -341,11 +365,32 @@ A(左)の方がリーダブル
|
|
341
365
|
# coderay java
|
342
366
|
|
343
367
|
public boolean Contains(String str, String substr) {
|
344
|
-
if (str == null || substr == null)
|
345
|
-
|
368
|
+
if (str == null || substr == null) // ガード節
|
369
|
+
return false;
|
370
|
+
if (substr.equals("")) // ガード節
|
371
|
+
return true;
|
372
|
+
// 大事な処理だけに興味があるなら↑までは無視可能。
|
373
|
+
// 大事な処理は↓から。
|
346
374
|
// ...
|
347
375
|
}
|
348
376
|
|
377
|
+
= 7.5 ガード節なし
|
378
|
+
|
379
|
+
# coderay java
|
380
|
+
|
381
|
+
public boolean Contains(String str, String substr) {
|
382
|
+
if (str == null || substr == null) {
|
383
|
+
return false; // 異常値
|
384
|
+
} else {
|
385
|
+
if (substr.equals("")) {
|
386
|
+
return true; // 特別値
|
387
|
+
} else {
|
388
|
+
// 大事な処理はここ。ネストが深い。
|
389
|
+
// ...
|
390
|
+
}
|
391
|
+
}
|
392
|
+
}
|
393
|
+
|
349
394
|
= 7.5 ガード節の効果
|
350
395
|
|
351
396
|
* 以降の処理が単純になる
|
@@ -373,7 +418,7 @@ ifとreturnの使い方\n
|
|
373
418
|
|
374
419
|
# coderay ruby
|
375
420
|
|
376
|
-
if 条件 | return if
|
421
|
+
if 条件 | return if !条件
|
377
422
|
サブ処理 |
|
378
423
|
end | サブ処理
|
379
424
|
|
@@ -404,16 +449,15 @@ ifとreturnの使い方\n
|
|
404
449
|
|
405
450
|
# コメントを追加するメソッドだな!
|
406
451
|
def add_comment(post, user, text)
|
407
|
-
if post.hidden_from?(user)
|
408
|
-
# コメント追加しないの!?
|
452
|
+
if post.hidden_from?(user) # ユーザーに見えない投稿なら…
|
409
453
|
report_access_error
|
454
|
+
# エラー処理か。ifの後にもなにか処理はあるのかな?
|
410
455
|
else
|
411
|
-
# else(その他)の方の処理だけど
|
412
|
-
# このメソッドにとって大事な処理か
|
413
|
-
# ifの方と同列の大事さの処理かと思った
|
414
456
|
comment = Comment.create(text)
|
415
457
|
post.add(comment)
|
416
|
-
|
458
|
+
# ユーザーに見えるときだけコメント追加か。
|
459
|
+
# こうやってコメントを追加するんだな。
|
460
|
+
end # ifの後にはなにも処理はないんだな。
|
417
461
|
end
|
418
462
|
|
419
463
|
= 番外:例(1)return付き
|
@@ -422,22 +466,23 @@ ifとreturnの使い方\n
|
|
422
466
|
|
423
467
|
# コメントを追加するメソッドだな!
|
424
468
|
def add_comment(post, user, text)
|
425
|
-
if post.hidden_from?(user)
|
426
|
-
#
|
427
|
-
|
428
|
-
return
|
469
|
+
if post.hidden_from?(user) # ユーザーに見えない投稿なら…
|
470
|
+
report_access_error # エラー処理をして、
|
471
|
+
return # それで終了か。
|
429
472
|
end
|
430
473
|
|
431
|
-
#
|
474
|
+
# エラー処理が終わったからここからは通常の処理だな。
|
432
475
|
comment = Comment.create(text)
|
433
|
-
post.add(comment)
|
476
|
+
post.add(comment) # こうやってコメントを追加するんだな!
|
434
477
|
end
|
435
478
|
|
436
479
|
= 番外:例(1)まとめ
|
437
480
|
|
438
|
-
*
|
439
|
-
|
481
|
+
* 異常系と正常系の扱いを変える
|
482
|
+
* 異常系はすぐにreturn
|
483
|
+
* エラー処理だけというのをアピール
|
440
484
|
* 正常系はネストしない
|
485
|
+
* 正常系の方が重要なコード
|
441
486
|
* 重要なコードは見やすい場所に
|
442
487
|
|
443
488
|
= 番外:例(2)
|
@@ -458,14 +503,12 @@ ifとreturnの使い方\n
|
|
458
503
|
|
459
504
|
# データベースを準備するんだな
|
460
505
|
def prepare_database(path)
|
461
|
-
if not File.exist?(path)
|
462
|
-
#
|
463
|
-
# あれ、作るのも普通の「準備」なような…
|
506
|
+
if not File.exist?(path) # なかったら…
|
507
|
+
# 作ってすぐに終了か。あれ、作るのも普通の「準備」なような…
|
464
508
|
return Database.create(path)
|
465
509
|
end
|
466
510
|
|
467
|
-
#
|
468
|
-
# やっぱりどっちも「準備」だよなぁ
|
511
|
+
# あったら開くのか。これも「準備」だよなぁ。
|
469
512
|
# なにか特別な意図があるんだろうか…
|
470
513
|
Database.open(path)
|
471
514
|
end
|
@@ -476,12 +519,10 @@ ifとreturnの使い方\n
|
|
476
519
|
|
477
520
|
# データベースを準備するんだな
|
478
521
|
def prepare_database(path)
|
479
|
-
if File.exist?(path)
|
480
|
-
#
|
481
|
-
|
482
|
-
|
483
|
-
# なかったら作るのか
|
484
|
-
Database.create(path)
|
522
|
+
if File.exist?(path) # あったら…
|
523
|
+
Database.open(path) # 開いて
|
524
|
+
else # なかったら…
|
525
|
+
Database.create(path) # 作るのか
|
485
526
|
end
|
486
527
|
end
|
487
528
|
|
@@ -507,16 +548,16 @@ ifとreturnの使い方\n
|
|
507
548
|
// 読みやすい制御フローにして投稿・よい投稿に「いいね!」して応援
|
508
549
|
|
509
550
|
function positiveIntegerSum(n) { // n以下の正の整数の合計を返す
|
510
|
-
if (0 >= n) {
|
511
|
-
return -1; // エラー
|
512
|
-
} else {
|
513
|
-
var total = 0;
|
514
|
-
for (; n > 0; n--) {
|
515
|
-
total += n;
|
516
|
-
}
|
517
|
-
return total;
|
518
|
-
}
|
519
|
-
}
|
551
|
+
if (0 >= n) { // ヒント:
|
552
|
+
return -1; // エラー // 1. 条件式の並び順
|
553
|
+
} else { // * 左に変化するもの
|
554
|
+
var total = 0; // * 右に変化しにくいもの
|
555
|
+
for (; n > 0; n--) { // 2. 処理する条件の順番
|
556
|
+
total += n; // * 肯定形で書く
|
557
|
+
} // * 単純な条件を先に書く
|
558
|
+
return total; // * 関心がある条件を先に書く
|
559
|
+
} // * 目立つ条件を先に書く
|
560
|
+
} // 3. ガード節
|
520
561
|
|
521
562
|
= まとめ(1)
|
522
563
|
|
@@ -531,7 +572,7 @@ ifとreturnの使い方\n
|
|
531
572
|
考えた
|
532
573
|
* 条件式内の並び順
|
533
574
|
* →左に変化するもの・右に変化しにくいもの
|
534
|
-
*
|
575
|
+
* 処理する条件の順番
|
535
576
|
* →肯定形で書く・単純な条件を先に書くなど
|
536
577
|
* ガード節
|
537
578
|
* →特別なケースはすぐにreturn
|
@@ -561,8 +602,9 @@ ifとreturnの使い方\n
|
|
561
602
|
* コードから学ぶ気持ちで読む
|
562
603
|
* ×悪いこと探し
|
563
604
|
* ○いいこと探し
|
564
|
-
|
565
|
-
|
605
|
+
* 読むコード
|
606
|
+
* オススメは自分が使っているOSS
|
607
|
+
* ↑動作を知っているから読みやすい
|
566
608
|
|
567
609
|
= 悪いコード
|
568
610
|
|
data/config.yaml
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabbit-slide-kou-schoo-readable-code-2015-04
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2015.4.3.
|
4
|
+
version: 2015.4.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kouhei Sutou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03
|
11
|
+
date: 2015-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rabbit
|