LoginSignup
0
0

More than 5 years have passed since last update.

よく見る可読性の低い関数定義のコードフォーマット

Last updated at Posted at 2017-08-24

戻り値の型が、後置きの言語

特にPHPなどは、戻り値の型の有無が選択的なので、より顕著に可読性が下がります。

良い例

基本は、長くないなら1行で書きましょう。

function createUesr(string $name, int $age): User {
    return User::create($name, $age);
}

改行する場合でも、以下のように書けば、可読性を落とさずに済みます。

function createUesr(
    string $name,
    int $age
): User {
    return User::create($name, $age);
}
function createUesr(
    string $name,
    int $age
    ): User {
    return User::create($name, $age);
}

良くない例

function createUesr(
    string $name,
    int $age): User {
    return User::create($name, $age);
}

この書き方の良くないところは、以下のことが一見してすぐに分からない点です。

  • 「シグネチャ定義」と「処理の実装」の境界
    • 戻り値の型(そもそも、あるのかないのかがわからない)
    • 処理がどこから書かれているのか
function createUesr(
        string $name,
        int $age): User {
    return User::create($name, $age);
}

この書き方の良くないところは、戻り値の型がすぐにわからない点です。

戻り値の型が、前置きの言語

戻り値の型が前置きの言語の場合、事情は異なります。

良い例

基本は、長くないなら1行で書きましょう。

public User createUser(String name, int age) {
    return User.create(name, age);
}

改行する場合でも、以下のように書けば、可読性を落とさずに済みます。

public User createUser (
        String name,
        int age) {
    return User.create(name, age);
}
public User createUser (
    String name,
    int age
) {
    return User.create(name, age);
}
public User createUser (
    String name,
    int age
    ) {
    return User.create(name, age);
}

良くない例

public User createUser (
    String name,
    int age) {
    return User.create(name, age);
}

この書き方の良くないところは、以下のことが一見してすぐに分からない点です。

  • 「シグネチャ定義」と「処理の実装」の境界
    • 処理がどこから書かれているのか
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0