Attached to this message is a draft of a document called "Koha Coding
Standards and Guidelines for Contributors."  The release managers
(past and present) and the Kaitiaki have had a chance to look this
over and make comments -- now it's your turn!


The xml source of this document is on Sourceforge under the koha-doc
directory (http://cvs.sourceforge.net/viewcvs.py/koha/koha-doc/), so
you can make minor changes there, if you want.  Major changes should
be discussed on the mailing list, however.


Once we agree on the content of the document, we should get it
translated into French, Spanish, and German.  I think the final
version(s) should be posted in CVS, on kohadocs.org, and in the
Sourceforge Koha Document Manager
(http://cvs.sourceforge.net/viewcvs.py/koha/koha-doc/) -- anywhere
else?


Stephen Hedges


<bold><bigger><bigger><bigger>Koha Coding Standards and Guidelines for
Contributors</bigger></bigger></bigger></bold>

<color><param>0000,0000,FFFF</param>1. Introduction</color>

<color><param>0000,0000,FFFF</param>2. Code style</color>

<color><param>0000,0000,FFFF</param>2.1. HTML</color>

<color><param>0000,0000,FFFF</param>2.1.1. Templates location</color>

<color><param>0000,0000,FFFF</param>2.1.2. Online help</color>

<color><param>0000,0000,FFFF</param>2.1.3. Template content</color>

<color><param>0000,0000,FFFF</param>2.2. DB access</color>

<color><param>0000,0000,FFFF</param>2.3. Perl scripts</color>

<color><param>0000,0000,FFFF</param>2.4. Naming conventions</color>

<color><param>0000,0000,FFFF</param>2.4.1. Script names</color>

<color><param>0000,0000,FFFF</param>2.4.2. Variable names</color>

<color><param>0000,0000,FFFF</param>3. Commenting style</color>

<color><param>0000,0000,FFFF</param>4. POD style (in modules)</color>


<bold><bigger><bigger><bigger>1. Introduction</bigger></bigger></bigger></bold>


As in other free and open source software projects, Koha contains code
that has been contributed to the project by many different developers.
Koha is now a complex piece of software, and we'd like continue to
improve the overall code standards to make it easier for new
developers to join the project, and to make existing code easier to
maintain. These standards and guidelines are intended to give Koha a
reasonably consistent style throughout the code and to ensure
maintainability.


When you commit new code to Koha, please do your best to comply with
these guidelines -- it will increase the chances of your code become
part of a stable Koha release. And if you find code in Koha that
doesn't comply with these guidelines, please feel free to fix it, just
as you would correct any bug.


<bold><bigger><bigger><bigger>2. Code style</bigger></bigger></bigger></bold>


Koha code should conform to the guidelines defined in the
<color><param>0000,0000,FFFF</param>perlstyle</color> man page as they
apply to code details (indenting, etc.). As for the general structure
of the code, all Koha code must be separated into three types:

	• 	

the Perl code;

	• 	

the database (DB) access code; and

	• 	

the HTML code.


These three code types, as well as the Koha naming conventions, are
explained in the following sections.


<bold><bigger>2.1. HTML</bigger></bold>


All code controlling the "appearance layer" is contained in
<fixed>HTML::Template</fixed> files. These files are located in the
<fixed>koha-tmpl</fixed> directory in CVS (currently
<color><param>0000,0000,FFFF</param>http://sourceforge.net/cvs/?group_id=16466</color>).
They must contain all the code controlling the presentation layer.
There are at least two good reasons for this:

	• 	

the look of Koha can be changed by rewriting only the templates,
changing nothing in the Perl code itself; and

	• 	

there is a tool to translate templates, so it's the only way to
localise our preferred ILS.


Almost all the actual Koha presentation pages use templates. Only a
few exceptions (that can be considered bugs) remain at the time of
writing.


<bold>2.1.1. Templates location</bold>


The <fixed>koha-tmpl</fixed> directory contains two different
sub-directories: <fixed>opac-tmpl</fixed> and
<fixed>intranet-tmpl</fixed>. Under those two are the themes
directories, and one directory for each language -- for example,
<fixed>intranet-tmpl/default/en</fixed> for English default templates.
The templates should be stored in a directory that parallels the
location of the Perl scripts. For example, the Perl scripts located in
the <fixed>$KOHA/acqui.simple</fixed> directory should use templates
located in
<fixed>$KOHA/koha-tmpl/intranet-tmpl/<<theme>/<<language>/acqui.simple</fixed>.


<italic>Exceptions:</italic>

	• 	

Scripts located in <fixed>$KOHA/admin/</fixed> use templates located
in <fixed>$KOHA/intranet-tmpl/<<theme>/<<language>/parameters/</fixed>

	• 	

Some scripts in <fixed>$KOHA/members/</fixed> use templates located in
<fixed>$KOHA/intranet-tmpl/<<theme>/<<language>/</fixed>

	• 	

Simple HTML "includes" files for placing standard headers and footers
on Koha pages are located in
<fixed>$KOHA/intranet-tmpl/<<theme>/<<language>/includes/</fixed>


When modifying templates, you should only modify the English one
manually. All other languages are automatically generated from the
English templates through a <fixed>.po</fixed> file and the script
located in <fixed>misc/translator/tmpl_process3.pl</fixed>.


<bold>2.1.2. Online help</bold>


The <fixed>$KOHA/koha-tmpl/<<theme>/<<language>/help</fixed> directory
contains online help. Under the <fixed>help</fixed> directory is a
complete copy of the Koha directory structure, with templates using
the same name as the "true" templates. These templates must contain
online help. The <fixed>$KOHA/help.pl</fixed> script is called when
the user clicks on "Help," which opens/shows the templates in this
directory.


PLEASE write online help when you add features.


<bold>2.1.3. Template content</bold>


There is usually only one template for a given <fixed>.pl</fixed>
file. For example, the template related to
<fixed>circulation/returns.pl</fixed> should be
<fixed>circulation/returns.tmpl</fixed>.


If there are different possible behaviours, they should be separated
by a <fixed>TMPL_IF</fixed>. For example,
<fixed>$KOHA/koha-tmpl/intranet-tmpl/<<theme>/<<language>/parameters</fixed>
contains:

<fixed><<!-- TMPL_IF name="add_form" --></fixed>


that controls what is shown when the user wants to add something (as
determined by the Perl script):

<fixed>if ($op) {

$template->param(script_name => $script_name,

$op              => 1);  # We show only the TMPL_VAR names $op.

} else {

$template->param(script_name => $script_name,

else              => 1); # We show only the TMPL_VAR names $op.

}</fixed>


Note that some scripts use two different templates, depending on what
the user wants to do. This is sometimes a mistake in Koha design,
sometimes not. For example, in
<fixed>$KOHA/opac/opac-search.pl</fixed>, we can use the
<fixed>opac-search</fixed> or <fixed>opac-searchresults</fixed>
template, depending on the step of the search. Both templates are big,
so merging them would not be a good idea for maintenance and
readability of Koha code.


<bold><bigger>2.2. DB access</bigger></bold>


No database (DB) access routines should ever be included in
<fixed>.pl</fixed> files. All DB access routines should be in the
<fixed>C4</fixed> directory, in <fixed>.pm</fixed> modules. There is
one <fixed>.pm</fixed> module for each logical "librarian activity."
For example, everything related to circulation must be in
<fixed>C4/Circulation/Circ2.pm</fixed>.


All inclusion of Perl variables in SQL commands should be done through
<bold><smaller>prepare()</smaller></bold> statements with
<bold><smaller>?</smaller></bold> placeholders, and then using
<bold><smaller>bind()</smaller></bold> and/or
<bold><smaller>execute()</smaller></bold> calls. This helps with
security and other testing.


The <fixed>C4/Context.pm</fixed> module contains everything related to
technical variables (creating a DB handler, getting a
systempreference, etc.).


The <fixed>C4/Koha.pm</fixed> module contains everything related to
things that are useful everywhere in Koha, such as getting a branch
list or a branch detail. (At the time of writing, this module probably
should be improved. Some subroutines, for instance, are located in
other modules but would be better here.)


<italic>Exception:</italic> parameters management scripts. For
historic reasons, and because they don't do anything complex in the
database, all scripts in <fixed>$KOHA/admin/</fixed> deal directly
with the database. This is not really a bug, even if it does seem to
break the rule. (Think of it as "the exception that proves the rule.")


<bold><bigger>2.3. Perl scripts</bigger></bold>


Perl scripts are the scripts that are run from <fixed>cgi-bin</fixed>
(or <fixed>mod_perl</fixed>). They all have (almost) the same
behaviour:

	• 	

read parameters;

	• 	

open an <fixed>HTML::Template</fixed> file (that checks the user
rights to access the page if needed -- if the user doesn't have the
correct permissions, show a "sorry no access" page and end);

	• 	

depending on the parameters (often, in a <fixed>$op</fixed> variable,
but not always), run subroutines from a <fixed>C4/*.pm</fixed> package
to do something in the database;

	• 	

using the parameters and the results of the previous operations, fill
the template; and

	• 	

send the template to the user.


Some scripts are run on the command line. They all are located in the
<fixed>misc</fixed> directory. The <fixed>misc</fixed> directory does
not contain any scripts that can be run as <fixed>cgi-bin</fixed>.


Command line scripts executed without any parameters should return a
"help" listing of possible parameters and exit without doing anything.


<bold><bigger>2.4. Naming conventions</bigger></bold>


<bold>2.4.1. Script names</bold>


The script names must be related to what they do, and they must be
located in a directory that is related to a logical "librarian
activity". For example, all scripts related to circulation are in
<fixed>$KOHA/circ/</fixed>.


<italic>Exceptions:</italic>

	• 	

The <fixed>acqui.simple</fixed> directory contains all the code for
MARC cataloguing. The directory name obviously is not related to the
"librarian activity," but it's historic.

	• 	

The <fixed>bull</fixed> directory is related to serials management.
Most of the code in this directory was developed in France, and
"serials management" is "bulletinage" in French -- but in general,
script and directory names should be in English.

	• 	

Some scripts related to borrowers are still in the
<fixed>$KOHA/</fixed> directory (<fixed>pay.pl</fixed> for example).


<bold>2.4.2. Variable names</bold>


Variables can be of three types:

	• 	

Perl variables;

	• 	

template variables; or

	• 	

hash entries from a SQL request.


All three variables should have the same name. For example, if you get
the field <fixed>issuelength</fixed> from the
<fixed>issuingrules</fixed> table, then you should have a
<bold><smaller><<!-- TMPL_VAR name="issuelength" --></smaller></bold>
in the template, and either a <fixed>$issuelength</fixed> or
<fixed>$issuingrules->{'issuelength'}</fixed> variable in the Perl
code.


Sometimes you must use the same field/variable from two different
points of view. In this case, use two variable names, with something
distinctive before the "simple" name. For example if you need
"librarian name" and "borrower name" from
<fixed>borrowers.surname</fixed>, use <fixed>librariansurname</fixed>
and <fixed>borrowersurname</fixed>.


<bold><bigger><bigger><bigger>3. Commenting style</bigger></bigger></bigger></bold>


Scripts and modules in Koha are expected to contain clear and helpful
comments to assist other developers who want to work on the code.
Despite the availability of external documentation, source code
listings should be able to stand on their own, because external
documentation may not adequately describe the details and intent of
the developer(s). Please make sure you include enough comments to keep
your coding intentions understandable, and try to follow these
guidelines:

	1 	

Please write your comments in English or request translation.


Write in complete sentences, capitalize the first word, and put two
spaces after the end of each sentence (so that the Emacs sentence
commands will work).


Comments should start with a # and a single space.

	2 	

Comment your code when you do something that someone else may not
think is "trivial." In other words, comment anything that is not
readily obvious in the code.

	3 	

Each Koha file should begin with a block comment containing:

	a 	

basic information about the file, such as file name, version, date,
author:

<fixed># addbiblio.pl, v 1.52.2.6, 2005/05/19, tipaul</fixed>

	b 	

the standard Koha copyright message:

<fixed># Copyright 2000-2005 Katipo Communications

# Copyright 2005 <<author or employer>

#

# This file is part of Koha.

#

# Koha 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.

#

# Koha 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

# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple
Place,

# Suite 330, Boston, MA  02111-1307 USA</fixed>

	4 	

All block comments should consist of one or more paragraphs built out
of complete sentences, and each sentence should end in a period.


Block comments generally apply to some (or all) code that follows
them, and are indented to the same level as that code. Each line of a
block comment starts with a # and a single space (unless it is
indented text inside the comment). Paragraphs inside a block comment
are separated by a line containing a single #. Block comments are best
surrounded by a blank line above and below them.

	5 	

Provide comments at the beginning of every function (unless the
function is very short and its purpose is obvious) indicating the
function's purpose, assumptions, and limitations.


The comments should be a brief introduction to understand why the
function exists and what it can do. Document how it does it, too, if
that is not obvious.


Place a blank line between a function and its description. Include
information about the sorts of arguments it gets, and what the
possible values of arguments mean and are used for. Also explain the
significance of the return value, if there is one.

	6 	

Use comments on code that consists of loops and logic branches. These
are key areas that will assist the reader when reading source code.

	7 	

Try to avoid adding comments at the end of a line of code; end-line
comments make code more difficult to read.


End-line comments are appropriate, however, when annotating variable
declarations. In this case, try to align all end-line comments at a
common tab stop. (End-line comments should be separated by at least
two spaces from the statement.)

	8 	

When modifying code, always keep the commenting around it up to date.


Keeping the comments up-to-date when the code changes is a priority --
comments that contradict the code are worse than no comments! Identify
the change with the date and your user name.

	9 	

Avoid using clutter comments, such as an entire line of asterisks.
Instead, use white space to separate comments from code.


Avoid the use of superfluous or inappropriate comments, such as
humorous sidebar remarks.

	10 	

To prevent recurring problems, always use comments on bug fixes and
work-around code, especially in this team environment.


When you want to leave notes about functionalities to be added, put
<bold><smaller>TODO:</smaller></bold> in front of the actual comments
so developers can easily search for them.


When you want to leave notes about bugs to be fixed, put
<bold><smaller>FIXME:</smaller></bold> in front of the actual comments
so developers can easily search for them.


One other type of comment that developers may overlook is the cvs log
message added when committing a file, but these comments are also very
important. The messages should never be omitted and should give enough
description to let other developers know what you are doing without
having to read the code itself. In the case of bug fixes, it is also
recommended that the messages include the bug number and the bug
title, as well as a brief description of the fix.


<bold><bigger><bigger><bigger>4. POD style (in modules)</bigger></bigger></bigger></bold>


Koha modules should be documented with extensive comments written in
POD ("Plain Old Documentation") format. Instructions for the POD
markup language can be found in the
<color><param>0000,0000,FFFF</param>perlpod</color> man page.


POD comments should include as least the following headings:

NAME


The name of the module and a very brief description of the type of
functions contained in the module.

SYNOPSIS


This may give an example of how to access the module from a script,
but it may also contain any general discussion of the purpose or
behavior of the module's functions, and discussion of changes made to
the module.

DESCRIPTION


If not included in the SYNOPSIS, a general description of the module's
purpose should appear here.

FUNCTIONS


Descriptions of each function in the module, beginning with an example
of how the function is called and continuing with a description of the
behavior of the function.

AUTHOR


This will generally be the "Koha Development Team," but may also be an
individual developer who has made significant contributions to the
module. Give a contact e-mail address.


For two examples, see the POD comments in the <fixed>Search.pm</fixed>
module (<bold><smaller>perldoc Search.pm</smaller></bold>) and the
<fixed>Biblio.pm</fixed> module.