Coding Style

This chapter discuss the coding guide lines that should be used in all Planner code. Please notice that if your code doesn't follow these guide lines it won't be included in Planner, therefor please make sure that it does before sending the patches.

Naming conventions

The naming schemes is a little different between libplanner and planner, this is to make it clear by looking at a function name to see where it belongs. The reason for a naming scheme is that it should be easy to find the definition and implementation of a function/enum/class/... by just looking at the name of it.

Class Names
  • Classes should be named with a prefix Mrp for classes in libplanner and Planner for classes in planner.
  • Use upper case letters as word separators, lower case for the rest of the word.
  • No underscores ("_") in the names.

Example of Class Names

class MrpTaskManager
class PlannerResourceModel

File Names

File names should be named as follows:

  • No upper case letters.
  • Use a hyphen ("-") to separate words.
  • Prefix filenames with mrp- for files in libplanner and planner- for files in planner.

Example of Filenames

mrp-task-manager.c
mrp-task-manager.h
planner-resource-model.c
planner-resource-model.h

External Functions Names

External functions should be named as the file names with every hyphen ("-") replaced by an underscore ("_").

Example of External function names

mrp_task_manager_get_root (...)
planner_resource_model_get_type_string (...)

Static Functions Names

This use the same scheme as external functions names but remove the prefix. So for example:

Example of Static Functions Names

/* An external function */
 
mrp_resource_get_name (...)
 
/* would become */
 
resource_get_name (...)
 
/* if it was a static function withing mrp-resource.c. */

Notice that this scheme is currently not used in Planner but we'll change to it whenever we are editing those files, please use this scheme for all new code.

Enum/Struct Names

Enum and structs follow the same naming convention as the classes.

Example of Enum/Struct names
in mrp-resource.h

typedef enum {
   	MRP_RESOURCE_TYPE_NONE,
	MRP_RESOURCE_TYPE_WORK,
	MRP_RESOURCE_TYPE_MATERIAL
} MrpResourceType;

Same goes for structures.
#define and Macro Names

#defines and macros should be named in the same way as functions but with uppercase letters instead of lowercase.

Example of #define and macro names

/* In mrp-resource.h */
 
#define MRP_TYPE_RESOURCE         (mrp_resource_get_type ())
 
/* In planner-resource-model.h */
 
#define PLANNER_IS_RESOURCE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
                                 PLANNER_TYPE_RESOURCE_MODEL))

Code Formatting

In order to make the code more readable we here define a number of formatting rules that has to be followed. If everyone hacking on Planner uses the same code formatting it will be much easier to read other persons code.

Indentation

Indentation should be done using eight spaces.

Example of Indentation

static void
mrp_resource_new (void)
{
        MrpResource *resource;
 
        resource = g_object_new (MRP_TYPE_RESOURCE, NULL);
 
        return resource;
}

This is the same style used by the Linux kernel and by the GNOME project.

Braces policy
  • All if-, while-, do-, for-statements should use braces, even one-liners. This is because it's obvious where the statement starts/ends, it's also good when you want to put another line in the same statement and you don't have to make sure that you put the braces there.

    Example 2.8. Braces used for statements

    if (i >= 0) {
            /* do something */
    }
  • For these types of statements, the open brace should be placed on the same line as the statement with a single space between the statement and the opening brace.

    Example of One line statements

     
    for (i = 0; i < len; ++i) {
            /* my one-liner */
    }
  • For functions the opening brace should be placed below the function

    Example of Braces around function statements

    void static
    my_function (void)
    {
            /* do something */
    }
  • If-elseif-else-statements should use the following formatting.

    Example of If-elseif-else statement

    if (...) {
            /* do something */
    }
    else if (...) {
            /* do something else */
    } else {
            /* do yet another thing */
    }
Parenthesis policy

Function declarations and implementation

A few guidelines on how to write functions.

  • In declaration, always put one argument per row and align the functions, types and arguments for easy reading.
    Example of Function declarations
    void      mrp_resource_assign             (MrpResource     *resource,
          		                           MrpTask         *task,
          					   gint             units);
     
    GList *   mrp_resource_get_assignments    (MrpResource     *resource);
  • When implementing the function, try to fit all arguments on one row, if that doesn't fit in 80 chars width, split them up with one argument per row. Put a single space between the longest type and the arguments.

    Example of Function implementation

    void
    mrp_resource_assign (MrpResource *resource, MrpTask *task, gint units)
    {
            /* ... */
    }
     
    gboolean
    mrp_project_save_as (MrpProject   *project,
                         const gchar  *uri,
                         gboolean      force,
                         GError      **error)
    {
            /* ... */
    }
  • If you have pointer arguments with * or ** they should be placed together with the argument, not with the type. Also they should be aligned as:

    Example of Align pointer arguments

    gboolean
    mrp_project_save_as (MrpProject   *project,
                         const gchar  *uri,
                         gboolean      force,
                         GError      **error)
    {
            /* ... */
    }
Variable definitions

Variables should be aligned in the same way as function arguments. Put one space between longest type and arguments and align with * and ** in the same way as for function arguments.

Structure of source files

All source files should start with a header.

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 Your name <your@email>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

The first row is to make sure that everyone using Emacs gets the correct tab-width and programming mode.

Header Files

Header files are built up as follows.

/* Header as described above */
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2002 Richard Hult &lt;richard@imendio.com&gt;
 * Copyright (C) 2001-2002 Mikael Hallendal &lt;micke@imendio.com&gt;
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
/* 
 * include guards, these are to make sure that the same header only
 * gets included once. 
 */
#ifndef __MRP_ASSIGNMENT_H__
#define __MRP_ASSIGNMENT_H__
 
/* 
 * Includes needed by this header, put as much includes as possible in
 * the C-file rather than the header file. 
 */
#include &lt;planner/mrp-object.h&gt;
#include &lt;planner/mrp-time.h&gt;
#include &lt;planner/mrp-types.h&gt;
 
/*
 * If it is a class you are defining you need this convenience macros,
 * they should come directly after the includes.
 */
#define MRP_TYPE_ASSIGNMENT         (mrp_assignment_get_type ())
#define MRP_ASSIGNMENT(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), 
                                     MRP_TYPE_ASSIGNMENT, MrpAssignment))
#define MRP_ASSIGNMENT_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), 
                                     MRP_TYPE_ASSIGNMENT, MrpAssignmentClass))
#define MRP_IS_ASSIGNMENT(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), 
                                     MRP_TYPE_ASSIGNMENT))
#define MRP_IS_ASSIGNMENT_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), 
                                     MRP_TYPE_ASSIGNMENT))
#define MRP_ASSIGNMENT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), 
                                     MRP_TYPE_ASSIGNMENT, MrpAssignmentClass))
 
/*
 * Defining the class structures
 */
typedef struct _MrpAssignmentClass MrpAssignmentClass;
typedef struct _MrpAssignmentPriv  MrpAssignmentPriv;
 
struct _MrpAssignment {
        MrpObject          parent;
 
        MrpAssignmentPriv *priv;
};
 
struct _MrpAssignmentClass {
        MrpObjectClass parent_class;
};
 
/* Function declarations */
GType              mrp_assignment_get_type     (void) G_GNUC_CONST;
 
MrpAssignment     *mrp_assignment_new          (void);
 
MrpTimeInterval   *mrp_assignment_get_interval (MrpAssignment *assignment);
 
MrpTask           *mrp_assignment_get_task     (MrpAssignment *assignment);
MrpResource       *mrp_assignment_get_resource (MrpAssignment *assignment);
gint               mrp_assignment_get_units    (MrpAssignment *assignemnt);
 
/* End of the include guard */
#endif /* __MRP_ASSIGNMENT_H__ */

C files

/* Same header as in the header file */
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2002 Richard Hult &lt;richard@imendio.com&gt;
 * Copyright (C) 2001-2002 Mikael Hallendal &lt;micke@imendio.com&gt;
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
/*
 * Declarations of private structures and enums, the should come
 * before the internal functions since some of the internal functions
 * might need these structures/enums.
 */
struct _MrpAssignmentPriv {
	MrpTask     *task;
	MrpResource *resource;
 
	gint         units;
};
 
/* Properties */
enum {
        PROP_0,
        PROP_TASK,
        PROP_RESOURCE,
        PROP_UNITS
};
 
/* Declaration of internal functions */
static void mpa_class_init        (MrpAssignmentClass *klass);
static void mpa_init              (MrpAssignment      *assignment);
static void mpa_finalize          (GObject            *object);
static void mpa_set_property      (GObject            *object,
				   guint               prop_id,
				   const GValue       *value,
				   GParamSpec         *pspec);
static void mpa_get_property      (GObject            *object,
				   guint               prop_id,
				   GValue             *value,
				   GParamSpec         *pspec);
 
 
/*
 * Declaration of internal file global variables
 * NOTE! Try to not use file global variables and instead put them in
 *       a privat structure as in this example
 */
static MrpObjectClass *parent_class;
 
/* 
 * Put the get_type function as the first function implementation.
 */
GType
mrp_assignment_get_type (void)
{
        /* ... */
}
 
/* 
 * It should be directly followed by the static variables 
 * (in the same order as they were defined).
 */
static void
mpa_class_init (MrpAssignmentClass *klass)
{
      /* &lt;snip&gt; */
}
 
/* After the internal functions you put the implementations of the
 * external functions.
 * (These too in the same order as they were declared).
 */

The reason I'm nagging about putting the implementations in the same order as the declarations is that it's much easier to find a function if you do.
Use of private structures

We strongly suggest that you use private structures as where done in the code example above. This makes the code better object oriented and makes sure that you are not using the internal variables directly.