今回もTechネタ.
今回は単なるグチ、というか何のオチもない話ですので軽く読み飛ばしてくださいw
今までず~~と続けていた自分のスタイルが全否定(笑)された話です.
Table of Contents
ワイのC/C++コーディングスタイル

CやC++でのプログラミングは30年くらいやってますけど, コーディングスタイルはずっとGNU Coding Standards に従ってます.
#include <iostream>
int
main (int argc, char **argv)
{
  int i;
  for (i = 0; i < 10; i++)
    {
      if (i % 2)
        {
          std::cout << "hello odd " << i << std::endl;
        }
      else
        {
          std::cout << "hello even " << i << std::endl;
        }
    }
  return 0;
}上は GNU Coding Standards の 5.1 Formatting Your Source Code に従って書いたコード.
特徴として,
- 関数定義で関数名が行の一番左にある.
 - 関数本体が始まる大括弧が行の一番左にくる.
 - それ以外の大括弧はインデントを入れた行の最初にくる.
 - 開き丸括弧の前とコンマの後にスペースを一つ入れる.
 
最近はあまり流行らないのか, 職場の若い人たち曰く、このスタイルで「ワイが書いたコードが一目で分かる」そうです(笑)
人から引き継いだコードは GNU indent をかけて GNU Coding Standards フォーマットに成形してから読んでます.
理由はその方が読むのが楽だから、です.
このスタイルでずーっと30数年CやC++コードを書いてます.
ところがです…
Google C++ Styele Guide ややってきた

プロジェクトでコーディングスタイルを Google C++ Style Guide に従う、との指示がありました.
ざっと目を通してみましたが、自分のコードがどれくらい Style Guide から逸脱してるものか、ツールをかけて調べてみることにします.
cpplint – static code checker for C++

Google C++ Style Guideに準拠しているコードかどうかをチェックする cpplint というツールがあります.
cpplint のインストールは以下の方法で行いました.
$ sudo apt-get install python3-pip $ pip install --user cpplint
~/.local/bin/ の下に cpplint がインストールされるので、そこに PATH を通すようにすると良いでしょう.
先のコードを cpplintにかけた結果が以下の通り.
$ cpplint sample00.cpp
 sample00.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] "  [legal/copyright] [5]
 sample00.cpp:4:  Extra space before ( in function call  [whitespace/parens] [4]
 sample00.cpp:5:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
 sample00.cpp:9:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
 sample00.cpp:11:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
 sample00.cpp:14:  An else should appear on the same line as the preceding }  [whitespace/newline] [4]
 sample00.cpp:15:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
 Done processing sample00.cpp
 Total errors found: 7
コピーライト表示が無い、とかは置いといて、大括弧が行の最後に置け、とか余計なスペースがあるとか、GNU Coding Standards に従ったスタイルがまるっきり
全否定
されてます(涙)
Function Declarations and Definitions

Google C++ Style Guide の Function Declarations and Definitions を調べてみました.
すると以下の様なことが書かれてました…
- Choose good parameter names.
 - A parameter name may be omitted only if the parameter is not used in the function’s definition.
 - If you cannot fit the return type and the function name on a single line, break between them.
 - If you break after the return type of a function declaration or definition, do not indent.
 - The open parenthesis is always on the same line as the function name.
 - There is never a space between the function name and the open parenthesis.
 - There is never a space between the parentheses and the parameters.
 - The open curly brace is always on the end of the last line of the function declaration, not the start of the next line.
 - The close curly brace is either on the last line by itself or on the same line as the open curly brace.
 - There should be a space between the close parenthesis and the open curly brace.
 - All parameters should be aligned if possible.
 - Default indentation is 2 spaces.
 - Wrapped parameters have a 4 space indent.
 
ということで、すなわち重要なところだけ列挙すると
- 関数名丸括弧の間にスペース入れるな.
 - 開いた波括弧(大括弧) は行の先頭に書くな、行の最後に書け.
 
…とあってやっぱり GNU Coding Standards はここに来て全否定されてます…ハイ.
あと、やりがちなのが TAB の代わりにスペースを使え、というのは大賛成です、今後気をつけます.
cpplint の指摘に従って書きなおしたコード

cpplint から指摘に対応して修正したコードは以下の通りです.
// Copyright [2022] <kawauso55>
#include <iostream>
int
main(int argc, char **argv) {
  int i;
  for (i = 0; i < 10; i++) {
      if (i % 2) {
          std::cout << "hello odd " << i << std::endl;
      } else {
          std::cout << "hello even " << i << std::endl;
      }
  }
  return 0;
}やっぱり長年親しんだスタイルと違うので違和感はありますが、時代の流れなんでしょうね.
プロジェクトの方針であれば仕方ない、今後はこのスタイルに慣れるようにします.
GNU indent のようなコーディングスタイルを変更してくれるツールは無いものか、この機会に調べてみようかと.
あと, C++の機能の使い方, 例えば例外は使わない等の記載もあるので継続してGoogle C++ Style Guide読んでみます.
追申:Amazon で調べたらこんな本があるんですね、値段が安いので手元にあっても良いかも
では





