LoginSignup
0
0

More than 5 years have passed since last update.

インデントをしてほしい叫び

Posted at

インデントをしてくださいm(__)m

${\boldsymbol インデント}$、インデント、${\large インデント}$、${\rm\Large インデント!!!!!}$

インデントをしていない人は先生怒らないから正直に言いなさい(#^ω^)

インデントをしていないクソなコード例

func youMustIndent(_ code: String) -> String {
var sentences: [String] = code.components(separatedBy: "\n")
var tab: String = ""
for n in (0 ..< sentences.count - 1) {
if let first = sentences[n].first, first == "}", tab.count > 1 {
tab = String(tab.prefix(tab.count - 1))
}
sentences[n] = tab + sentences[n]
if let last = sentences[n].last, last == "{" {
tab += "\t"
}
sentences[n] += "\n"
}
return sentences.joined()
}

インデントしてある良いコード例

func youMustIndent(_ code: String) -> String {
    var sentences: [String] = code.components(separatedBy: "\n")
    var tab: String = ""
    for n in (0 ..< sentences.count - 1) {
        if let first = sentences[n].first, first == "}", tab.count > 1 {
            tab = String(tab.prefix(tab.count - 1))
        }
        sentences[n] = tab + sentences[n]
        if let last = sentences[n].last, last == "{" {
            tab += "\t"
        }
        sentences[n] += "\n"
    }
    return sentences.joined()
}

ちなみに、こちらは引数にとったワンラインコードをインデントをさせる関数

インデントのメリット

他人が見ても、ブロックやスコープが解りやすい。全体の構造が把握しやすい。

インデントはどうやってやる?

最近の大抵のエディタやIDEは改行すると勝手にいい感じにインデントしてくれる。
そういうのがない環境の場合は、改行後tabを押してください。
もっと言えば、作業の区切りごとに全体を選択してオートインデントをしてください。

${\boldsymbol インデント}$、インデント、${\large インデント}$、${\rm\Large インデント!!!!!}$

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