Codingstyle

Version 4 (Jaime Phillips, 01/03/2012 08:05 pm)

1 2 Jaime Phillips
h1. Coding Style
2 1 Jaime Phillips
3 2 Jaime Phillips
h2. Match braces
4 1 Jaime Phillips
5 2 Jaime Phillips
Align braces in same column.
6 1 Jaime Phillips
7 3 Jaime Phillips
<pre>
8 2 Jaime Phillips
  struct mystruct 
9 2 Jaime Phillips
  {
10 2 Jaime Phillips
    int myvar;
11 2 Jaime Phillips
  };
12 3 Jaime Phillips
</pre>
13 1 Jaime Phillips
14 2 Jaime Phillips
**NOT**
15 1 Jaime Phillips
16 3 Jaime Phillips
<pre>
17 2 Jaime Phillips
  struct mystruct {
18 2 Jaime Phillips
    int myvar;
19 2 Jaime Phillips
  };
20 3 Jaime Phillips
</pre>
21 1 Jaime Phillips
22 2 Jaime Phillips
This is a general rule for braces.  It applies to switch statements, blocks under if statements, function blocks, ...
23 1 Jaime Phillips
24 2 Jaime Phillips
h2. Tabs
25 1 Jaime Phillips
26 2 Jaime Phillips
* Don't insert tabs into the source file.  
27 2 Jaime Phillips
* Set your editor translate a tab into two spaces.
28 1 Jaime Phillips
29 2 Jaime Phillips
h2. Indent Offset
30 1 Jaime Phillips
31 2 Jaime Phillips
Use two spaces as indent offset.
32 1 Jaime Phillips
33 3 Jaime Phillips
<pre>
34 2 Jaime Phillips
    for (i=0; i<bufsize; i++)
35 2 Jaime Phillips
    {
36 2 Jaime Phillips
      buf[i] = tolower(buf[i]);
37 2 Jaime Phillips
      if(buf[i] < 0) 
38 2 Jaime Phillips
      {
39 2 Jaime Phillips
        buf[i] = 127;
40 2 Jaime Phillips
      }
41 2 Jaime Phillips
    }
42 3 Jaime Phillips
</pre>
43 1 Jaime Phillips
44 2 Jaime Phillips
**NOT**
45 1 Jaime Phillips
46 3 Jaime Phillips
<pre>
47 2 Jaime Phillips
    for (i=0; i<bufsize; i++)
48 2 Jaime Phillips
    {
49 2 Jaime Phillips
            buf[i] = tolower(buf[i]);
50 2 Jaime Phillips
            if(buf[i] < 0) 
51 2 Jaime Phillips
            {
52 2 Jaime Phillips
                    buf[i] = 127;
53 2 Jaime Phillips
            }
54 2 Jaime Phillips
    }
55 3 Jaime Phillips
</pre>
56 1 Jaime Phillips
57 2 Jaime Phillips
h2. Local Function Names
58 1 Jaime Phillips
59 2 Jaime Phillips
Don't use an underscore to denote local functions.
60 2 Jaime Phillips
  void  entry_print(void* to_print, FILE* ostr)
61 2 Jaime Phillips
62 2 Jaime Phillips
**NOT**
63 2 Jaime Phillips
64 2 Jaime Phillips
  void  _entry_print(void* to_print, FILE* ostr)
65 2 Jaime Phillips
66 2 Jaime Phillips
h2. Doxygen
67 2 Jaime Phillips
68 2 Jaime Phillips
The source documentation is embedded in the code and parsed by doxygen.  Use "Doxygen commands.":http://www.stack.nl/~dimitri/doxygen/commands.html A typical doc header looks like:
69 2 Jaime Phillips
70 3 Jaime Phillips
<pre>
71 2 Jaime Phillips
  /*!
72 2 Jaime Phillips
   * \brief analyze a file for the copyright information
73 2 Jaime Phillips
   *
74 2 Jaime Phillips
   * This will open and analyze the file provided. It is important to note that
75 2 Jaime Phillips
   * this function will clear any information that was previously stored in copy.
76 2 Jaime Phillips
   * All information that is needed should be extracted from copy after a call to
77 2 Jaime Phillips
   * analyze before calling analyze again.
78 2 Jaime Phillips
   *
79 2 Jaime Phillips
   * \param copy the copyright instance that will be analyzed
80 2 Jaime Phillips
   * \param file_name the name of the file to be openned and analyzed
81 2 Jaime Phillips
   *
82 2 Jaime Phillips
   * \return 0 on OK, -1 on failure.
83 2 Jaime Phillips
   * 
84 2 Jaime Phillips
   * \todo blah blah
85 2 Jaime Phillips
   */
86 2 Jaime Phillips
  void copyright_analyze(copyright copy, FILE* istr)
87 3 Jaime Phillips
</pre>
88 2 Jaime Phillips
89 2 Jaime Phillips
There are many other useful doxygen commands, including:
90 2 Jaime Phillips
  \code ... \endcode
91 2 Jaime Phillips
  \verbatim ... \endverbatim
92 2 Jaime Phillips
  \li
93 2 Jaime Phillips
  \n
94 2 Jaime Phillips
95 1 Jaime Phillips
===== Copyright =====
96 1 Jaime Phillips
Each source file should have a copyright notice.  For HP developers, this looks like
97 3 Jaime Phillips
98 3 Jaime Phillips
<pre>
99 2 Jaime Phillips
  /***************************************************************
100 2 Jaime Phillips
  Copyright (C) 2008-2010 Hewlett-Packard Development Company, L.P.
101 2 Jaime Phillips
102 2 Jaime Phillips
  This program is free software; you can redistribute it and/or
103 2 Jaime Phillips
  modify it under the terms of the GNU General Public License
104 2 Jaime Phillips
  version 2 as published by the Free Software Foundation.
105 2 Jaime Phillips
106 2 Jaime Phillips
  This program is distributed in the hope that it will be useful,
107 2 Jaime Phillips
  but WITHOUT ANY WARRANTY; without even the implied warranty of
108 2 Jaime Phillips
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
109 2 Jaime Phillips
  GNU General Public License for more details.
110 2 Jaime Phillips
111 2 Jaime Phillips
  You should have received a copy of the GNU General Public License along
112 1 Jaime Phillips
  with this program; if not, write to the Free Software Foundation, Inc.,
113 1 Jaime Phillips
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
114 2 Jaime Phillips
  ***************************************************************/
115 3 Jaime Phillips
</pre>
116 3 Jaime Phillips
117 2 Jaime Phillips
And don't forget to update the year when a file is modified.
118 2 Jaime Phillips
119 3 Jaime Phillips
h2. Comments
120 3 Jaime Phillips
121 2 Jaime Phillips
Comments are good.  Use them.
122 2 Jaime Phillips
When commenting a variable, indent the comment starting on the same line
123 2 Jaime Phillips
124 1 Jaime Phillips
<pre>
125 2 Jaime Phillips
struct regex_file_struct
126 2 Jaime Phillips
{
127 2 Jaime Phillips
  int      ftype1;          /* 1=filename, 2=license 
128 2 Jaime Phillips
                             * 3=bucket, 4=tag       */
129 2 Jaime Phillips
  char    *regex1;          /* regex1 string         */
130 2 Jaime Phillips
  regex_t  compRegex1;
131 2 Jaime Phillips
};
132 1 Jaime Phillips
</pre>
133 1 Jaime Phillips
134 2 Jaime Phillips
**NOT**
135 2 Jaime Phillips
136 2 Jaime Phillips
<pre>
137 2 Jaime Phillips
struct regex_file_struct
138 2 Jaime Phillips
{
139 2 Jaime Phillips
  /* 1=filename, 2=license */
140 2 Jaime Phillips
  int      ftype1;
141 2 Jaime Phillips
  /* regex1 string */
142 2 Jaime Phillips
  char    *regex1;
143 2 Jaime Phillips
  regex_t  compRegex1;
144 2 Jaime Phillips
};
145 2 Jaime Phillips
</pre>
146 1 Jaime Phillips
147 2 Jaime Phillips
h2. SQL 
148 2 Jaime Phillips
149 2 Jaime Phillips
The current code base has sql scattered in each file that executes a sql statement.  The problem with this is that we sometimes have multiple statements to accomplish the same task.  Sometimes both versions work correctly and sometimes they don't.  I propose that we start a /ui/common/common-sql.php to define common functions to return results.  For example, one common function is to get all the non-artifact uploadtree records in an upload or subtree.  
150 2 Jaime Phillips
151 3 Jaime Phillips
<pre>
152 2 Jaime Phillips
function GetUploadtreeResult($upload_fk, $lft="", $rgt="")
153 2 Jaime Phillips
{
154 2 Jaime Phillips
  global $PG_CONN;
155 2 Jaime Phillips
156 2 Jaime Phillips
  if (empty($upload_fk)) return NULL;
157 2 Jaime Phillips
158 2 Jaime Phillips
  /* Missing $lft or $rgt means to return results for the
159 2 Jaime Phillips
   * whole upload instead of a subtree 
160 2 Jaime Phillips
   */
161 2 Jaime Phillips
  if (empty($lft) || (empty($rgt)))
162 2 Jaime Phillips
    $LftRgtCondition = "";
163 2 Jaime Phillips
  else
164 2 Jaime Phillips
    $LftRgtCondition = "and lft>'$lft'  and rgt<'$rgt'";
165 2 Jaime Phillips
166 2 Jaime Phillips
  $sql = "select uploadtree_pk, ufile_name, lft, rgt from uploadtree 
167 2 Jaime Phillips
            where upload_fk='$upload_fk' 
168 1 Jaime Phillips
              $LftRgtCondition
169 2 Jaime Phillips
              and ((ufile_mode & (1<<28)) = 0)";
170 2 Jaime Phillips
    $outerresult = pg_query($PG_CONN, $sql);
171 2 Jaime Phillips
    DBCheckResult($outerresult, $sql, __FILE__, __LINE__);
172 2 Jaime Phillips
  return $outerresult;
173 2 Jaime Phillips
}
174 3 Jaime Phillips
</pre>
175 2 Jaime Phillips
176 2 Jaime Phillips
h2. Internationalization
177 4 Jaime Phillips
178 2 Jaime Phillips
[[i18n#step_2_-_separate_message_text_from_code | Internationalization]]