Difference between revisions of "CodingStandard"

From GnuCash
Jump to: navigation, search
(New page, work in progress, time for supper.)
 
(Finish C format, add sections on the GLib/GObject/Gtk framework and Guile)
Line 1: Line 1:
Gnucash is fairly relaxed about coding style, but we will periodically run a reformatter called astyle over the code to clean it up. To reduce the need for this (it messes up VCS history, particularly "blame"), please format your code as follows.
+
= Coding Style =
 +
== C Format ==
 +
Gnucash is fairly relaxed about code format, but we will periodically run a reformatter called [http://astyle.sourceforge.net/astyle.html Artistic Style] (astyle) over the code to clean it up. To reduce the need for this (it messes up VCS history, particularly "blame"), please format your code as follows.
  
In general, follow the [http://www.gnu.org/prep/standards/standards.html#Formatting GNU format] '''except''' use four spaces instead of two for the indents. To summarize,
+
In general, follow the [http://www.gnu.org/prep/standards/standards.html#Formatting GNU format] '''except''' use four spaces instead of two for the indents, and ''don't'' indent the braces. To summarize, a properly formatted function will look like this:
 +
 
 +
  guint
 +
  gnc_account_foo (Account *account, gpointer bar)
 +
  {
 +
      Split *baz;
 +
      guint salt;
 +
      if (gnc_split_waldo (baz) > 0)
 +
      {
 +
            salt = gnc_split_pepper (baz);
 +
      }
 +
      return salt;
 +
  }
 +
 
 +
Please keep lines under 80 characters. If you need to wrap, line up the function arguments like this:
 +
 
 +
  gnc_account_function_with_a_lot_of_paramters (LongTypeName foo, LongerTypeName *bar,
 +
                                                TypeName baz)
 +
  {
 +
  }
 +
We don't do the wide separation of names and aligned parameters, so ''don't'' do this:
 +
  void gnc_account_foo              (Account    *bar,
 +
                                      Split      *baz,
 +
                                      gpointer    waldo);
 +
  Split *gnc_account_pepper          (Account    *salt,
 +
                                      Transaction *sausage);
 +
 
 +
''Instead, do it this way:''
 +
  void gnc_account_foo (Account *bar, Split *baz, gpointer waldo);
 +
  Split *gnc_account_pepper (Account *salt, Transaction *sausage);
 +
 
 +
For the record, the astyle commandline we use is
 +
  astyle --indent=spaces=4 --brackets=break --pad-oper  *.[hc]
 +
The rationale for the arguments is contained in [http://lists.gnucash.org/pipermail/gnucash-devel/2009-August/026121.html this email].
 +
 
 +
== Framework ==
 +
Gnucash is a [http://www.gtk.org Gtk+] project. It's design is object-oriented using Gnome's [http://developer.gnome.org/gobject/stable/ GObject] C-language framework.
 +
 
 +
Remember that even though it's in C, it's supposed to be object-oriented code, so decompose your problem with objects, not procedurally. Try to keep functions short and well named.
 +
 
 +
Gnucash is also based upon [http://developer.gnome.org/glib/stable/ GLib], a very rich foundation library. Familiarize yourself with GLib's many utilities and data types, ''and use them''. In particular ''do not write a utility for something which GLib already does.''
 +
 
 +
(Yes, in spite of those fine words, there's a lot of code in Gnucash that doesn't use GLib types, duplicates GLib or GObject functionality, or is procedural instead of object oriented. We'll eventually get that refactored out, but in the meantime, don't make it any worse.)
 +
 
 +
== Guile and Scheme ==
 +
Gnucash is partly implemented in a [http://groups.csail.mit.edu/mac/projects/scheme/ Scheme] dialect called [http://www.gnu.org/s/guile/ Guile]. (It was originally written mostly in Guile, but that implementation was largely replaced with C several years ago.) In particular, the reports system and part of the business system are written in Guile. To support that, most of the core "engine" API is wrapped and accessible from Guile.

Revision as of 06:06, 23 November 2011

Coding Style

C Format

Gnucash is fairly relaxed about code format, but we will periodically run a reformatter called Artistic Style (astyle) over the code to clean it up. To reduce the need for this (it messes up VCS history, particularly "blame"), please format your code as follows.

In general, follow the GNU format except use four spaces instead of two for the indents, and don't indent the braces. To summarize, a properly formatted function will look like this:

 guint
 gnc_account_foo (Account *account, gpointer bar)
 {
      Split *baz;
      guint salt;
      if (gnc_split_waldo (baz) > 0)
      {
           salt = gnc_split_pepper (baz);
      }
      return salt;
 }

Please keep lines under 80 characters. If you need to wrap, line up the function arguments like this:

 gnc_account_function_with_a_lot_of_paramters (LongTypeName foo, LongerTypeName *bar,
                                               TypeName baz)
 {
 }

We don't do the wide separation of names and aligned parameters, so don't do this:

 void gnc_account_foo               (Account     *bar,
                                     Split       *baz,
                                     gpointer     waldo);
 Split *gnc_account_pepper          (Account     *salt,
                                     Transaction *sausage);

Instead, do it this way:

 void gnc_account_foo (Account *bar, Split *baz, gpointer waldo);
 Split *gnc_account_pepper (Account *salt, Transaction *sausage);

For the record, the astyle commandline we use is

 astyle --indent=spaces=4 --brackets=break --pad-oper  *.[hc]

The rationale for the arguments is contained in this email.

Framework

Gnucash is a Gtk+ project. It's design is object-oriented using Gnome's GObject C-language framework.

Remember that even though it's in C, it's supposed to be object-oriented code, so decompose your problem with objects, not procedurally. Try to keep functions short and well named.

Gnucash is also based upon GLib, a very rich foundation library. Familiarize yourself with GLib's many utilities and data types, and use them. In particular do not write a utility for something which GLib already does.

(Yes, in spite of those fine words, there's a lot of code in Gnucash that doesn't use GLib types, duplicates GLib or GObject functionality, or is procedural instead of object oriented. We'll eventually get that refactored out, but in the meantime, don't make it any worse.)

Guile and Scheme

Gnucash is partly implemented in a Scheme dialect called Guile. (It was originally written mostly in Guile, but that implementation was largely replaced with C several years ago.) In particular, the reports system and part of the business system are written in Guile. To support that, most of the core "engine" API is wrapped and accessible from Guile.