Skip to content

Instantly share code, notes, and snippets.

@ommadawn46
Last active June 22, 2018 01:42
Show Gist options
  • Save ommadawn46/6fbc3fc44d5990bebea2565f0a2f4ac7 to your computer and use it in GitHub Desktop.
Save ommadawn46/6fbc3fc44d5990bebea2565f0a2f4ac7 to your computer and use it in GitHub Desktop.

リーダブルコード 4章: 美しさ

美しさが必要な理由

  • 読みやすいから。 プログラミングをしている時間の大半はコードを読む時間だし、こだわる意味がある
  • コードを、一貫性あるやり方で、意味のある形に「整形」すれば、設計に手を入れなくても、すばやく簡単に読むことができるコードにすることができる。

コードの美しさの3つの原則

  1. 読み手が慣れているパターンと 一貫性のある レイアウトを使う。
  2. 似ているコードは似ているように見せる。
  3. 関連するコードをまとめてブロックにする。

一貫性のあるコード・見た目を似せる

public class PerformanceTester {
    public static final TcpConnectionSimulator wifi = new TcpConnectionSimulator(
           500, /* Kbps */
           80, /* millisecs latency */
           200, /* jiter */
           1 /* packet loss % */);

    public static final TcpConnectionSimulator t3_fiber =
       new TcpConnectionSimulator(
           45000, /* Kbps */
           10, /* millisecs latency */
           0, /* jiter */
           0 /* packet loss % */);

    public static final TcpConnectionSimulator cell = new TcpConnectionSimulator(
           100, /* Kbps */
           400, /* millisecs latency */
           250, /* jiter */
           5 /* packet loss % */);
}
  • イケてないところ
    1. 改行の位置に一貫性がない
    2. 縦の列が揃っていない
public class PerformanceTester {
    public static final TcpConnectionSimulator wifi =
        new TcpConnectionSimulator(
            500,   /* Kbps */
            80,    /* millisecs latency */
            200,   /* jitter */
            1      /* packet loss % */);

    public static final TcpConnectionSimulator t3_fiber =
        new TcpConnectionSimulator(
            45000, /* Kbps */
            10,    /* millisecs latency */
            0,     /* jitter */
            0      /* packet loss % */);

    public static final TcpConnectionSimulator cell =
        new TcpConnectionSimulator(
            100,   /* Kbps */
            400,   /* millisecs latency */
            250,   /* jitter */
            5      /* packet loss % */);
}
  1. コードの「改行」と「列」を整形したので、概要が把握しやすくなった
  • 良くなったが、同じコメントが繰り返されていて冗長
public class PerformanceTester {
    // TcpConnectionSimulator(throughput, latency, jitter, packet_loss)
    //                            [Kbps]   [ms]    [ms]    [percent]

    public static final TcpConnectionSimulator wifi =
        new TcpConnectionSimulator(500,    80,     200,    1);

    public static final TcpConnectionSimulator t3_fiber =
        new TcpConnectionSimulator(45000,  10,     0,      0);

    public static final TcpConnectionSimulator cell =
        new TcpConnectionSimulator(100,    400,    250,    5);
}

関連のあるコードをブロックにまとめる

# Import the user's email contacts, and match them to users in our system.
# Then display a list of those users that he/she isn't already friends with.
def suggest_new_friends(user, email_password):
    friends = user.friends()
    friend_emails = set(f.email for f in friends)
    contacts = import_contacts(user.email, email_password)
    contact_emails = set(c.email for c in contacts)
    non_friend_emails = contact_emails - friend_emails
    suggested_friends = User.objects.select(email__in=non_friend_emails)
    display['user'] = user
    display['friends'] = friends
    display['suggested_friends'] = suggested_friends
    return render("suggested_friends.html", display)
  • イケてないところ
    1. コードが一塊になっていて読む気がしない
    2. コメントが対象とするスコープが広すぎる
def suggest_new_friends(user, email_password):
    # Get the user's friends' email addresses.
    friends = user.friends()
    friend_emails = set(f.email for f in friends)

    # Import all email addresses from this user's email account.
    contacts = import_contacts(user.email, email_password)
    contact_emails = set(c.email for c in contacts)

    # Find matching users that they aren't already friends with.
    non_friend_emails = contact_emails - friend_emails
    suggested_friends = User.objects.select(email__in=non_friend_emails)

    # Display these lists on the page.
    display['user'] = user
    display['friends'] = friends
    display['suggested_friends'] = suggested_friends

    return render("suggested_friends.html", display)
  1. 処理のブロックごとに「段落分け」することで処理の流れを追うためのナビゲーションになる。
  • 上手く「空行」を活用する。
  1. コメントが説明するスコープを細かく分けることで、読む人が コードを見逃す のを防ぐことができる。

プロジェクト内のコードの美しさ

  • プロジェクト開発においては、正しいスタイルで整形することよりも、 プロジェクト全体で一貫性のあるコードに整形することが重要。
    • コード規約に従うべき。

感想

  • コードフォーマッタを使おう
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment