gnu-c++-standards
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ code indentation for GNU


From: Bruno Haible
Subject: Re: C++ code indentation for GNU
Date: Sun, 11 Aug 2019 14:04:02 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-157-generic; KDE/5.18.0; x86_64; ; )

Ian Lance Taylor wrote:
> As far as the actual formatting goes, I don't see any reason not to
> follow the format used by the GCC source code
> (https://gcc.gnu.org/codingconventions.html#Cxx_Formatting).  To me it
> seems like a straightforward extension of the GNU C formatting
> conventions.

Two objections:

1) Formatting is about how to distribute whitespace. The text you point to
also contains other coding style rules (e.g. the section "Names", or
"Define all members outside the class definition."). IMO this is out of
scope when we talk about formatting.

2) The rule "Do not indent protection labels." makes it harder to work with
git diff.

Git is the de-facto standard for source code management, and developers
spend a lot of times working with git diff:
  1. to review patches sent by mail,
  2. to produce GNU-style ChangeLog entries.
Therefore the coding style and the 'git diff' implementation ought to
work well together.

Now look at this file:
========================= foo.cc ======================
class a_rather_long_class_name
: with_a_very_long_base_name, and_another_just_to_make_life_hard
{
public:
  int member1;
  int member2;
  int member3;
  int member4;
  int member5;
  int member6;
  int member7;
private:
  int member8;
  int member9;
  int member10;
  int member11;
  int member12;
  int member13;
  int member14;
};
=========================================================
and make an edit. The resulting diff ought to mention the class name.

With 0 indentation of the protection labels you get this:

$ git diff foo.cc
diff --git a/foo.cc b/foo.cc
index 34f91eb..6196bed 100644
--- a/foo.cc
+++ b/foo.cc
@@ -16,5 +16,4 @@ private:
   int member11;
   int member12;
   int member13;
-  int member14;
 };

With a 1-space indentation of the protection labels you get this:

$ git diff foo.cc
diff --git a/foo.cc b/foo.cc
index c2a051d..1d78ffa 100644
--- a/foo.cc
+++ b/foo.cc
@@ -16,5 +16,4 @@ class a_rather_long_class_name
   int member11;
   int member12;
   int member13;
-  int member14;
 };

The latter is clearly more useful, as it allows more efficient review of
patches.

Tested with the most recent git release (git 2.22.0).

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]