This page aims to precise/improve/replace current Coding guidelines from Kohadocs.
Each file (scripts & modules) must include the GPL licence statement, typically at the top.
# 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
use strict; use warnings;
strict
and warnings
should be quelled by fixing the code, not by tacking on a no warnings
directive.prove xt/author/valid-templates.t
will catch unclosed tmpl_if and tmpl_loops and nesting problems.xt/author/test_template.pl file_name path_to_approprate_template_incudes_dir
default '0'
becomes
default 0
When you commit code to the project (whether using CVS or git) please write useful commit messages.
Don't needlessly refactor code, if it ain't broke, don't fix it! Don't waste time on changing style from someone else's style to your's! If you must refactor, make sure that the commit for the refactoring is completely separate from a bugfix, etc.
A good commit message has a few parts:
bug 1234: adding limit to number of items shown on reading history page [9.3.x] Added max_issues_history to borrowers table. Max_issues_history defines how many items will show up in the OPAC Reading History page. So, if max_issues_history is set to 5, the page will only list the last 5 items the borrower has returned. If max_issues_history is set to 0, then history is unlimited, i.e. The same as before max_issues_history was added. The manual should be updated to show how users can set the max_issues_history variable.
*** empty log message ***
Contributors to Koha are encouraged to reference a bug number with every commit. This helps us determine the purpose of the commit and establishes a more searchable and understandable history. If there is no bug open at bugs.koha.org that addresses your contribution, please feel free to open one and describe the bug or enhancement. Then, include the bug number in your commit message and set the “priority” field to “patch-sent”. This helps us keep track of patches that have been contributed but not yet applied.
FF will tolerate a trailing comma in an array literal, but Internet Explorer does not.
This is wrong:
KOHA.MyLib = { GetUgc: function(id) { // foo }, ugcCallBack: function(booksInfo) { // foo }, };
Replacing },
line with }
this is correct:
KOHA.MyLib = { GetUgc: function(id) { // foo }, ugcCallBack: function(booksInfo) { // foo } };
To be translatable by Koha translator system, text must be surrounded by _( ).
For example, this code is not correct:
param1 +="<\/optgroup><option value=\"newlist\">[ New List ]<\/option>"
This code is correct:
param1 +="<\/optgroup><option value=\"newlist\">[ "+_("New List")+" ]<\/option>"
And text mustn't contain html tags. For example, this code is not correct:
var displaytext = _("The Numbering Pattern will look like this: <br \/><ul class=\"numpattern_preview\">");
The correct form is:
var displaytext = _("The Numbering Pattern will look like this: ") + "<ul class=\"numpattern_preview\">";
Instead of writing:
$sth=$dbh->prepare("Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
write:
my $query = ' SELECT borrowers.*, categories.category_type FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE cardnumber = ? '; $sth = $dbh->prepare($query);
What are the rules from this example:
How to write “WHERE xxx IN (23, 45, 67)”?
In order to prevent SQL injection attacks, you must use placeholders (question marks) and pass the variables into the “execute” method for WHERE clauses and all other points in the SQL that pass user input directly to database queries.
my $query = ' SELECT itemnumber, biblionumber FROM items WHERE biblionumber IN (' . join(',', ('?') x @biblionumbers) . ')'; my $sth = $dbh->prepare($query); $sth->execute(@biblionumbers);
We should decide on a convention for command-line argument processing and stick to it:
The problem is… Some people may have already created scripts (for crontab jobs, like you said) already relying on “rebuild_nozebra.pl” running right away. For ”–help” I think we should add “usage” instructions (I don't think that breaks any thing).
Yeah, adding ”–help” to jobs that don't have it shouldn't break crontabs.
As proposal of convention rules, the GNU coding standards, http://www.gnu.org/prep/standards/ . In particular the option table and two standard options http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html#Command_002dLine-Interfaces
All sub names must contains a verb & a name. The verb describing what the sub does and the name the object it works on. Use capital for verb initial & name initial.
For example, AddBiblio will add a biblio in the database. common verbs :
if the sub works on a single object, it's singular. If it works on more than 1, it's plural. For example :
Internal functions should begin with an underscore and contain only lower-case characters with fully-spelled out names.
For example, _koha_delete_biblio will delete a biblio record from the koha biblio table.
In addition to use POD coding guidelines on this page : http://www.kohadocs.org/codingguidelines.html#d0e442 each function has to be “poded” like the example below :
=head2 SearchSuggestion =over 4 my @array = SearchSuggestion($user, $author, $title, $publishercode, $status, $suggestedbyme) searches for a suggestion return : C<\@array> : the suggestions found. Array of hash. Note the status is stored twice : * in the status field * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes. =back =cut sub SearchSuggestion { my ($user,$author,$title,$publishercode,$status,$suggestedbyme) = @_; ... return \@array; }
For 3.4, we plan a large rewrite & strengthening of the code. Here are some questions we should ask ourselves on coding guidelines. If you prefer an option, just put a + in the []