Project

General

Profile

Coding standards and hints for Virtuemart » History » Version 5

Max Milbers, 02/25/2011 11:37 PM

1 1 Max Milbers
h1. Coding standards and hints for Virtuemart
2 1 Max Milbers
3 3 Max Milbers
In general we avoid the use of globals. Globals are hard to maintain and they are unsure variables. 
4 3 Max Milbers
5 1 Max Milbers
h2. Code Formatting:
6 1 Max Milbers
7 4 Max Milbers
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.
8 1 Max Milbers
9 1 Max Milbers
Example:
10 1 Max Milbers
11 1 Max Milbers
12 1 Max Milbers
<pre>
13 1 Max Milbers
function exampleMethod($data = 0) {
14 1 Max Milbers
    if (!$product_id) $product_id = JRequest::getInt('product_id', 0);
15 1 Max Milbers
    if ($product_id > 0) {
16 1 Max Milbers
        ...;
17 1 Max Milbers
    }
18 1 Max Milbers
}
19 1 Max Milbers
</pre>
20 1 Max Milbers
21 1 Max Milbers
h2. SQL Formatting:
22 1 Max Milbers
23 1 Max Milbers
The queries should follow this format:
24 1 Max Milbers
25 1 Max Milbers
@$q  = 'SELECT `example_id` FROM  `#__vm_table` WHERE `user_id`=' . (int)$user_id;@
26 1 Max Milbers
27 1 Max Milbers
or if the
28 1 Max Milbers
29 1 Max Milbers
$user_id is an array, use $user_id["myId"]
30 1 Max Milbers
31 1 Max Milbers
Placing of the ` improves speed, because the sql-engine can parse faster ( and do not have to reparse).
32 1 Max Milbers
33 1 Max Milbers
h2. Variable Formatting:
34 1 Max Milbers
35 1 Max Milbers
Due abstract logic many people name their variables or database fields just "id", "key", "value" and so on.
36 1 Max Milbers
37 1 Max Milbers
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.
38 1 Max Milbers
39 1 Max Milbers
So we want a kind of this: <classname><tablename>variable like here
40 1 Max Milbers
41 1 Max Milbers
$vendor_id or $payment_method_id
42 1 Max Milbers
43 1 Max Milbers
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.
44 1 Max Milbers
45 1 Max Milbers
h2. Filename Formatting:
46 1 Max Milbers
47 1 Max Milbers
Filenames should be lowercase.
48 1 Max Milbers
49 1 Max Milbers
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:
50 1 Max Milbers
51 1 Max Milbers
 @$this->loadHelper('adminMenu'); // looks for the file helpers/adminmenu.php@
52 1 Max Milbers
53 1 Max Milbers
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.
54 1 Max Milbers
55 1 Max Milbers
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 
56 1 Max Milbers
57 1 Max Milbers
h2. Table names
58 1 Max Milbers
59 1 Max Milbers
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.
60 1 Max Milbers
61 1 Max Milbers
h2. URL Requests:
62 1 Max Milbers
63 1 Max Milbers
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.:
64 1 Max Milbers
65 1 Max Milbers
  $this->setRedirect('index.php?option=com_virtuemart&view=updatesMigration', $msg); // Fails
66 1 Max Milbers
$this->setRedirect('index.php?option=com_virtuemart&view=updatesmigration', $msg); // Works
67 1 Max Milbers
68 1 Max Milbers
69 1 Max Milbers
h2. Character Encoding:
70 1 Max Milbers
71 1 Max Milbers
Like in joomla we use UTF-8. Please ensure the right character set in your IDE.
72 2 Max Milbers
73 2 Max Milbers
h1. Some coding standards
74 2 Max Milbers
75 2 Max Milbers
h2. Standard publishing fields use:
76 2 Max Milbers
77 2 Max Milbers
- published with 0 and 1 formatted in the db as tinyInt(1) with standard 0
78 2 Max Milbers
- publish_up and publish_down formatted in the db as datetime with standard 0000-00-00 00:00:00
79 2 Max Milbers
80 2 Max Milbers
The standard query is then 
81 2 Max Milbers
82 2 Max Milbers
	@ $db = &JFactory::getDBO();
83 2 Max Milbers
	$this->_nullDate = $db->getNullDate();
84 2 Max Milbers
	$this->_now        = JFactory::getDate()->toMySQL();
85 2 Max Milbers
86 2 Max Milbers
 $query  = 'SELECT * FROM `#__sometable` WHERE yourexampel ="myexampelvalue" AND ';
87 2 Max Milbers
 $query .= ' ( publish_up = '.$this->_db->Quote($this->_nullDate).' OR publish_up <= '.$this->_db->Quote($this->_now).' )' .
88 2 Max Milbers
 ' AND ( publish_down = '.$this->_db->Quote($this->_nullDate).' OR publish_down >= '.$this->_db->Quote($this->_now).' ) '; @
89 5 Max Milbers
90 5 Max Milbers
h2. Path constants
91 5 Max Milbers
92 5 Max Milbers
Always use:
93 5 Max Milbers
94 5 Max Milbers
JPATH_VM_SITE for the frontend
95 5 Max Milbers
JPATH_VM_ADMINISTRATOR for the backend