コーディングスタイル:波括弧の位置
自分の信念となる「コーディングスタイル」を持っていますか?
信念というよりは、両端を揃えた名刺のような心地よさとでも申しましょうか。
日本人ですから、揃えることは美学であり、哲学でもあります。
カッコの位置
■ formatted by Eclipse 2020-02
Eclipse 2020-02のフォーマッターは数種類ありますが、選択肢によって以下のように変わっていきます。
■■ Active profile:K&R [built-in]
波括弧の始まりは、行末(右側)に置く事を良しとするフォーマットスタイル。
Javaは頑として、波括弧は行末スタートだったと記憶しています。
そしてC++の御先祖様になる、Cの標準的なスタイルはこれだったかなと。
/* * A sample source file for the code formatter preview */ #include <math.h> class Point { public: Point(double x, double y) : x(x), y(y) { } double distance(const Point &other) const; double x; double y; }; double Point::distance(const Point &other) const { double dx = x - other.x; double dy = y - other.y; return sqrt(dx * dx + dy * dy); }
■■ Active profile:BSD/Allman [built-in]
波括弧の始まりは、行頭(左側)に置く事を良しとするフォーマットスタイル。
波括弧の開始と終了が同一カラムに存在することになるから、私としては
統一感があっていいような気がしています。
/* * A sample source file for the code formatter preview */ #include <math.h> class Point { public: Point(double x, double y) : x(x), y(y) { } double distance(const Point &other) const; double x; double y; }; double Point::distance(const Point &other) const { double dx = x - other.x; double dy = y - other.y; return sqrt(dx * dx + dy * dy); }
■■ Active profile:GNU [built-in]
これは、上記のインデントが2になったものに見えます。
HTMLやCSSではないので、インデント2は読みづらい。
/* * A sample source file for the code formatter preview */ #include <math.h> class Point { public: Point (double x, double y) : x (x), y (y) { } double distance (const Point &other) const; double x; double y; }; double Point::distance (const Point &other) const { double dx = x - other.x; double dy = y - other.y; return sqrt (dx * dx + dy * dy); }
■■ Active profile:Whitesmiths [built-in]
行頭(左側)に波括弧を配置することになるが、インデントが加わっている。
カッコ=スコープの意味合いが薄くなった気がします。
/* * A sample source file for the code formatter preview */ #include <math.h> class Point { public: Point(double x, double y) : x(x), y(y) { } double distance(const Point &other) const; double x; double y; }; double Point::distance(const Point &other) const { double dx = x - other.x; double dy = y - other.y; return sqrt(dx * dx + dy * dy); }