Codingstyle

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