Debian Codegraph @

STYLE

raw
1 ===========================
2 Coding style guidelines
3 ===========================
4
5
6 While there is no one ideal style in which to code, there are some
7 advantages to adopting a common coding style. This guide helps
8 outline the style for this project.
9
10
11
12 -----------------------------
13 Tabs, spaces, and columns
14 -----------------------------
15
16 Avoid tabs. Instead, use exactly 2 spaces to indent code. Lines
17 should break at 80 columns. Whitespace greatly increases readability,
18 and should be used between adjacent statements. The exceptions to the
19 newline between statements rule include blocks of "use" statements
20 and blocks of variable assignments.
21
22 An example subroutine:
23
24 sub build_something_else
25 {
26 my ( $self, $p_param ) = @_;
27
28 Assert_Defined( $p_param );
29
30 return $self->calculate_something( $p_param + 1 );
31 }
32
33
34 -----------------
35 Return Values
36 -----------------
37
38 [Initially I advocated using a temporary variable to store the
39 return value to increase code readability, but since reading and
40 rereading (and re-rereading) Fowler's _Refactoring_, I am inclined
41 to agree with him and now support the Remove Temp with Query
42 refactoring, which also implies returning the result of a calculation
43 directly]
44
45 ----------
46 Naming
47 ----------
48
49
50 The following naming conventions should be used:
51
52
53 * Variables
54
55 Inside routines, variables should be named "variable_name", all
56 lowercase alphanumeric characters, with underscores separating the
57 words.
58
59 my $variable_name;
60
61
62 Additionally, it is often a good idea to describe the type of the
63 variable (other than scalars and object instances) in the name.
64 Some examples follow:
65
66 my $foo = 'foo';
67
68 my @number_list = ( 1, 2, 3 );
69
70 my $number_list_ref = [ 1, 2, 3 ];
71
72 my $number_list_ref = \@number_list;
73
74 my %alphabet_hash = ( 'a' => 1, 'b' => 2 );
75
76 my $alphabet_hash_ref = { 'a' => 1, 'b' => 2 };
77
78 my $alphabet_hash_ref = \%alphabet_hash;
79
80 my $file_cache = new Cache::FileCache( );
81
82
83 * Parameters
84
85 Parameters should be prefixed with 'p_' to assist in readability. Note
86 that I typically leave off the p_ for the $self parameter, and the
87 $proto parameter in the constructor. For example:
88
89 sub add
90 {
91 my ( $self, $p_value_one, $p_value_two ) = @_;
92
93 return $p_value_one + $p_value_two;
94 }
95
96
97 * Public static class variables
98
99 This type of variable is written "Public_Static_Class_Variable" and
100 is accessible publicly, and can be updated at runtime. These
101 variables are global for the scope of the module. Note that there
102 are very few cases in which this type of variable should not be
103 exposed via a class property (see below).
104
105 my $Public_Static_Class_Variable;
106
107
108
109 * Private static class variables
110
111 This type of variable is written "_Private_Static_Class_Variable"
112 and is accessible only to the module itself, and can be updated
113 at runtime. Note that there are very few cases in which this type
114 of variable should not be exposed via a class property (see below).
115
116 my $_Private_Static_Class_Variable
117
118
119
120 * Public static class constants
121
122 This type of "variable" is written "PUBLIC_STATIC_CONSTANT" and it
123 accessible publicly, and can not be modified at runtime. These
124 constants are global for the scope of the module. It is often a good
125 idea to make these constants available via Exporter as @EXPORT_OK.
126
127 my $PUBLIC_STATIC_CONSTANT = 'foo';
128
129
130
131 * Private static class constants
132
133 This type of "variable" is written "_PRIVATE_STATIC_CONSTANT" and is
134 accessible only to the module itself, and can not be modified at
135 runtime. These constants are global for the scope of the module.
136
137
138 $_PRIVATE_STATIC_CONSTANT = 'foo';
139
140
141
142 * Public instance methods
143
144 This type of subroutine is written as "public_instance_method" and
145 takes $self as the first parameter. This is part of the public
146 interface for a module, and should be documented with pod (see below
147 for details about documentation).
148
149 sub public_instance_method
150 {
151 my ( $self, $p_parameter ) = @_;
152 }
153
154
155
156 * Private instance methods
157
158 This type of subroutine is written as "_private_instance_method" and
159 takes $self as the first parameter. This is not part of the public
160 interface, and can be documented inline with perl comments.
161
162 sub _private_instance_method
163 {
164 my ( $self, $p_parameter ) = @_;
165 }
166
167
168
169 * Public class methods
170
171 This type of subroutine is written "Public_Class_Method" and does
172 not take $self as a parameter. This is part of the public interface
173 for a module, and should be documented with pod (see below for
174 details about documentation). You can optionally make use of
175 the Static_Params method in CacheUtils to automatically shift the
176 parameters if it is called via the class dereference operator rather
177 that explicitly via the package's namespace. (i.e. it is called via
178 Class->Public_Class_Method( ) vs. Class::Public_Class_Method( ) ).
179
180 sub Public_Class_Method
181 {
182 my ( $p_parameter ) = Static_Params( @_ );
183 }
184
185
186 * Private class methods
187
188 This type of subroutine is written "_Private_Class_Method" and does
189 not take $self as a parameter. This is not part of the public
190 interface, and can be documented inline with perl comments. You can
191 optionally make use of the Static_Params method in CacheUtils to
192 automatically shift the parameters if it is called via the class
193 dereference operator rather that explicitly via the package's
194 namespace. (i.e. it is called via Class->Public_Class_Method( )
195 vs. Class::Public_Class_Method( ) ).
196
197
198 sub _Private_Class_Method
199 {
200 my ( $p_parameter ) = Static_Params( @_ );
201 }
202
203
204
205
206 --------------
207 Properties
208 --------------
209
210 Properties and property accessor methods should be used to provide a
211 consistent abstracted interface to certain types of data. In general,
212 properties accessors are methods that take the form of get_property
213 and set_property. Private getters or setters are written
214 _get_property and _set_property respectively. Additionally, static
215 class properties are written Get_Property and Set_Property as a method
216 on the class itself. The properties are stored in the $self hash
217 (assuming the object is a blessed hash) with a key that corresponds to
218 the first letter uppercase name of the property, preceded by an
219 underscore.
220
221 An example of writing a scalar instance property "foo" with a public
222 getter and a private setter:
223
224 sub get_foo
225 {
226 my ( $self ) = @_;
227
228 return $self->{_Foo};
229 }
230
231 sub _set_foo
232 {
233 my ( $self, $foo ) = @_;
234
235 $self->{_Foo} = $foo;
236 }
237
238
239 An example of writing a list instance property "bar_list" with a
240 public getter and a public setter.
241
242
243 sub get_bar_list
244 {
245 my ( $self ) = @_;
246
247 return @{$self->{_Bar_List}};
248 }
249
250 sub set_bar_list
251 {
252 my ( $self, @bar_list ) = @_;
253
254 @{$self->{_Bar_List}} = @bar_list;
255 }
256
257
258
259
260 -----------------------------
261 Statement and brace style
262 -----------------------------
263
264
265 * if/elsif/else statements
266
267 if ( $condition )
268 {
269 # ...
270 }
271 elsif ( $condition )
272 {
273 # ...
274 }
275 else
276 {
277 # ...
278 }
279
280
281 * for loops
282
283 for ( my $i; $i < 1000; $i++ )
284 {
285 # ...
286 }
287
288
289 * foreach loops
290
291 foreach my $foo ( @foo_list )
292 {
293 # ...
294 }
295
296
297 * subroutines
298
299 sub do_something
300 {
301 my ( $self ) = @_;
302
303 #...
304
305 return $foo;
306 }
307
308
309 * error handling
310
311 my $foo = $self->get_foo( ) or
312 throw Error( "Couldn't get foo" );
313
314
315
316 --------------------
317 Module structure
318 --------------------
319
320 A the top of each module there should be a copyright disclaimer as
321 follows:
322
323 ######################################################################
324 # $ Id: $
325 # Copyright (C) 2002 John Smith All Rights Reserved
326 #
327 # Software distributed under the License is distributed on an "AS
328 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
329 # implied. See the License for the specific language governing
330 # rights and limitations under the License.
331 ######################################################################
332
333 The "Id" keyword is optional, though highly recommended if the code is
334 being managed with CVS.
335
336
337 After the copyright disclaimer, there should be a blank line, and then
338 the package declaration:
339
340 package Cache::FileCache;
341
342
343 The "use" statements should be listed in alphabetical order, with the
344 lowercase modules listed first. (This is so that "use strict" is the
345 next line after the package declaration)
346
347 use strict;
348 use vars qw( @ISA @EXPORT_OK $PUBLIC_STATIC_CONSTANT );
349 use Cache::Cache qw( $EXPIRES_NEVER );
350 use Error;
351 use Exporter;
352
353
354 Next, list the @ISA and @EXPORT or @EXPORT_OK lists:
355
356 @ISA = qw( Cache::Cache Exporter );
357 @EXPORT_OK = qw( $PUBLIC_STATIC_CONSTANT );
358
359
360 Followed by any variables in the "use vars" section:
361
362 $PUBLIC_STATIC_CONSTANT = 'foo';
363
364
365 And any non exported, non "use vars" variables:
366
367 my $_PRIVATE_STATIC_CONSTANT = 'bar';
368 my $_Private_Static_Class_Variable;
369
370
371 Write the methods in the following order:
372
373 * public class methods
374 * private class methods
375 * class properties ( first Get, then Set )
376 * new ( if the class is to be instantiated )
377 * public instance methods
378 * private instance methods
379 * instance properties ( first get, then set )
380
381
382 Finally, the class should be documented according to this template:
383
384 __END__
385
386 =pod
387
388 =head1 NAME
389
390 Class -- short description with no line breaks
391
392 =head1 DESCRIPTION
393
394 Verbose description and overview of the class
395
396 =head1 SYNOPSIS
397
398 Example of the usage of the class
399
400 =head1 METHODS
401
402 =over 4
403
404 =item B<method_name( $p_parameter )>
405
406 Description of the method
407
408 =over 4
409
410 =item $p_parameter
411
412 Description of the $p_parameter
413
414 =item Returns
415
416 Description of the return value
417
418 =back
419
420 =back
421
422 =head1 PROPERTIES
423
424 =over 4
425
426 =item B<(get|set)_foo>
427
428 Description of foo
429
430 =back
431
432 =head1 SEE ALSO
433
434 Related modules or documentation
435
436 =head1 AUTHOR
437
438 Original author: John Smith <[email protected]>
439
440 Last author: $ Author: $
441
442 Copyright (C) 2002 John Smith
443
444 =cut
445
446
447
448
449