Project

General

Profile

Coding standards and hints for Virtuemart » History » Version 1

Max Milbers, 02/25/2011 04:48 PM

1 1 Max Milbers
h1. Coding standards and hints for Virtuemart
2
3
h2. Code Formatting:
4
5
We follow the joomla standard in most cases. We use camelCase for methods. For faster copypasting and working with database fields. Both variable and database fields are lowercase and seperated with "_". You may use for internal variables camelCase also.
6
7
Example:
8
9
10
<pre>
11
function exampleMethod($data = 0) {
12
    if (!$product_id) $product_id = JRequest::getInt('product_id', 0);
13
    if ($product_id > 0) {
14
        ...;
15
    }
16
}
17
</pre>
18
19
h2. SQL Formatting:
20
21
The queries should follow this format:
22
23
@$q  = 'SELECT `example_id` FROM  `#__vm_table` WHERE `user_id`=' . (int)$user_id;@
24
25
or if the
26
27
$user_id is an array, use $user_id["myId"]
28
29
Placing of the ` improves speed, because the sql-engine can parse faster ( and do not have to reparse).
30
31
h2. Variable Formatting:
32
33
Due abstract logic many people name their variables or database fields just "id", "key", "value" and so on.
34
35
In our case we want to rewrite the structure of an old code, therefore it is very important to find the variable in the code rather fields in the database.
36
37
So we want a kind of this: <classname><tablename>variable like here
38
39
$vendor_id or $payment_method_id
40
41
Exception from this rule is the use of ordering and published. Ordering and published with 0 and 1 as values are useable with the joomla standard methods.
42
43
h2. Filename Formatting:
44
45
Filenames should be lowercase.
46
47
Models filenames (mymodel.php), view direcorynames (myview), all helper files and probably all other files loaded by the Joomla Framework, must be lowercase, no matter how you load the file:
48
49
 @$this->loadHelper('adminMenu'); // looks for the file helpers/adminmenu.php@
50
51
More complex filenames that are specific to your task and loaded by your own code (using require_once()), e.g. mySpecificClass.php, can be camelcase.
52
53
Read this article for more info http://docs.joomla.org/Talk:Developing_a_Model-View-Controller_Component_-_Part_1#Use_lowercase_file_and_folder_names_in_your_components.21 
54
55
h2. Table names
56
57
Depending on the way mysqld is configured, the MySQL server might be case sensitive on linux systems too, so _always_ use the correct case, preferably lowercase only.
58
59
h2. URL Requests:
60
61
As written in the section __Filename Formatting__ above, Joomla requires lowercase filenames. However, URL requests are not lowercased by Joomla. In order to find the proper views and controllers, make sure you use lowercase only in requests, e.g.:
62
63
  $this->setRedirect('index.php?option=com_virtuemart&view=updatesMigration', $msg); // Fails
64
$this->setRedirect('index.php?option=com_virtuemart&view=updatesmigration', $msg); // Works
65
66
67
h2. Character Encoding:
68
69
Like in joomla we use UTF-8. Please ensure the right character set in your IDE.