/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to doc/developers/code-style.txt

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
===============
79
79
 
80
80
Bazaar supports Python from 2.4 through 2.6, and in the future we want to
81
 
support Python 3.0.  Avoid using language features added in 2.5 or 2.6, or
82
 
features deprecated in Python 3.0.  (You can check v3 compatibility using
83
 
the ``-3`` option of Python2.6.)
 
81
support Python 2.7 and 3.0.  Avoid using language features added in 2.5,
 
82
2.6 or 2.7, or features deprecated in Python 3.0.  (You can check v3
 
83
compatibility using the ``-3`` option of Python2.6.)
84
84
 
85
85
Specifically:
86
86
 
87
 
 * Don't use the ``with`` statement.
88
 
 
89
 
 * Don't ``from . import``.
90
 
 
91
 
 * Don't use ``try/except/finally``, which is not supported in Python2.4,
92
 
   use separate nested ``try/except`` and ``try/finally`` blocks.
 
87
* Don't use the ``with`` statement.
 
88
 
 
89
* Don't ``from . import``.
 
90
 
 
91
* Don't use ``try/except/finally``, which is not supported in Python2.4,
 
92
  use separate nested ``try/except`` and ``try/finally`` blocks.
93
93
 
94
94
 
95
95
hasattr and getattr
101
101
  if getattr(thing, 'name', None) is None
102
102
 
103
103
 
 
104
kwargs
 
105
======
 
106
 
 
107
``**kwargs`` in the prototype of a function should be used sparingly.
 
108
It can be good on higher-order functions that decorate other functions,
 
109
such as ``addCleanup`` or ``assertRaises``, or on functions that take only
 
110
(or almost only) kwargs, where any kwargs can be passed.  
 
111
 
 
112
Otherwise, be careful: if the parameters to a function are a bit complex
 
113
and might vary over time (e.g.  the ``commit`` API) then we prefer to pass an
 
114
object rather than a bag of positional and/or keyword args.  If you have
 
115
an arbitrary set of keys and values that are different with each use (e.g.
 
116
string interpolation inputs) then again that should not be mixed in with
 
117
the regular positional/keyword args, it seems like a different category of
 
118
thing.
 
119
 
 
120
 
 
121
Imitating standard objects
 
122
==========================
 
123
 
 
124
Don't provide methods that imitate built-in classes (eg ``__in__``,
 
125
``__call__``, ``__int__``, ``__getitem__``) unless the class you're
 
126
implementing really does act like the builtin class, in semantics and
 
127
performance.
 
128
 
 
129
For example, old code lets you say ``file_id in inv`` but we no longer
 
130
consider this good style.  Instead, say more explicitly
 
131
``inv.has_id(file_id)``.
 
132
 
 
133
``__repr__``, ``__cmp__``, ``__str__`` are usually fine.
 
134
 
 
135
 
104
136
Module Imports
105
137
==============
106
138
 
152
184
later time, or possibly never at all.  Therefore we have restrictions on
153
185
what can be done inside them.
154
186
 
155
 
 0. If you think you need to use a ``__del__`` method ask another
156
 
    developer for alternatives.  If you do need to use one, explain
157
 
    why in a comment.
158
 
 
159
 
 1. Never rely on a ``__del__`` method running.  If there is code that
160
 
    must run, do it from a ``finally`` block instead.
161
 
 
162
 
 2. Never ``import`` from inside a ``__del__`` method, or you may crash the
163
 
    interpreter!!
164
 
 
165
 
 3. In some places we raise a warning from the destructor if the object
166
 
    has not been cleaned up or closed.  This is considered OK: the warning
167
 
    may not catch every case but it's still useful sometimes.
 
187
0. If you think you need to use a ``__del__`` method ask another
 
188
   developer for alternatives.  If you do need to use one, explain
 
189
   why in a comment.
 
190
 
 
191
1. Never rely on a ``__del__`` method running.  If there is code that
 
192
   must run, do it from a ``finally`` block instead.
 
193
 
 
194
2. Never ``import`` from inside a ``__del__`` method, or you may crash the
 
195
   interpreter!!
 
196
 
 
197
3. In some places we raise a warning from the destructor if the object
 
198
   has not been cleaned up or closed.  This is considered OK: the warning
 
199
   may not catch every case but it's still useful sometimes.
168
200
 
169
201
 
170
202
Cleanup methods
171
203
===============
172
204
 
173
 
Often when something has failed later code, including cleanups invoked
174
 
from ``finally`` blocks, will fail too.  These secondary failures are
175
 
generally uninteresting compared to the original exception.  So use the
176
 
``only_raises`` decorator (from ``bzrlib.decorators``) for methods that
177
 
are typically called in ``finally`` blocks, such as ``unlock`` methods.
178
 
For example, ``@only_raises(LockNotHeld, LockBroken)``.  All errors that
179
 
are unlikely to be a knock-on failure from a previous failure should be
180
 
allowed.
 
205
Often when something has failed later code will fail too, including
 
206
cleanups invoked from ``finally`` blocks.  These secondary failures are
 
207
generally uninteresting compared to the original exception.  ``bzrlib``
 
208
has some facilities you can use to mitigate this.
 
209
 
 
210
* In ``Command`` subclasses, prefer the ``add_cleanup`` method to using
 
211
  ``try``/``finally`` blocks.  E.g. to acquire a lock and ensure it will
 
212
  always be released when the command is done::
 
213
 
 
214
    self.add_cleanup(branch.lock_read().unlock)
 
215
 
 
216
  This also avoids heavily indented code. It also makes it easier to notice
 
217
  mismatched lock/unlock pairs (and other kinds of resource
 
218
  acquire/release) because there isn't a large block of code separating
 
219
  them.
 
220
 
 
221
* Use the ``only_raises`` decorator (from ``bzrlib.decorators``) when
 
222
  defining methods that are typically called in ``finally`` blocks, such
 
223
  as ``unlock`` methods.  For example, ``@only_raises(LockNotHeld,
 
224
  LockBroken)``.  All errors that are unlikely to be a knock-on failure
 
225
  from a previous failure should be allowed.
 
226
 
 
227
* Consider using the ``OperationWithCleanups`` helper from
 
228
  ``bzrlib.cleanup`` anywhere else you have a ``finally`` block that
 
229
  might fail.
181
230
 
182
231
 
183
232
Factories
315
364
 
316
365
Because repr methods are often called when something has already gone
317
366
wrong, they should be written somewhat more defensively than most code.
 
367
They shouldn't have side effects like doing network or disk
 
368
IO.
318
369
The object may be half-initialized or in some other way in an illegal
319
370
state.  The repr method shouldn't raise an exception, or it may hide the
320
371
(probably more useful) underlying exception.
336
387
``Exception`` (which excludes system errors in Python2.5 and later) would
337
388
be better.
338
389
 
 
390
The ``__str__`` method on exceptions should be small and have no side
 
391
effects, following the rules given for `Object string representations`_.
 
392
In particular it should not do any network IO, or complicated
 
393
introspection of other objects.  All the state needed to present the
 
394
exception to the user should be gathered before the error is raised.
 
395
In other words, exceptions should basically be value objects.
 
396
 
339
397
 
340
398
Test coverage
341
399
=============
354
412
 
355
413
Rationale:
356
414
 
357
 
 * It makes the behaviour vary depending on whether bzr is run with -O
358
 
   or not, therefore giving a chance for bugs that occur in one case or
359
 
   the other, several of which have already occurred: assertions with
360
 
   side effects, code which can't continue unless the assertion passes,
361
 
   cases where we should give the user a proper message rather than an
362
 
   assertion failure.
363
 
 * It's not that much shorter than an explicit if/raise.
364
 
 * It tends to lead to fuzzy thinking about whether the check is
365
 
   actually needed or not, and whether it's an internal error or not
366
 
 * It tends to cause look-before-you-leap patterns.
367
 
 * It's unsafe if the check is needed to protect the integrity of the
368
 
   user's data.
369
 
 * It tends to give poor messages since the developer can get by with
370
 
   no explanatory text at all.
371
 
 * We can't rely on people always running with -O in normal use, so we
372
 
   can't use it for tests that are actually expensive.
373
 
 * Expensive checks that help developers are better turned on from the
374
 
   test suite or a -D flag.
375
 
 * If used instead of ``self.assert*()`` in tests it makes them falsely pass with -O.
 
415
* It makes the behaviour vary depending on whether bzr is run with -O
 
416
  or not, therefore giving a chance for bugs that occur in one case or
 
417
  the other, several of which have already occurred: assertions with
 
418
  side effects, code which can't continue unless the assertion passes,
 
419
  cases where we should give the user a proper message rather than an
 
420
  assertion failure.
 
421
* It's not that much shorter than an explicit if/raise.
 
422
* It tends to lead to fuzzy thinking about whether the check is
 
423
  actually needed or not, and whether it's an internal error or not
 
424
* It tends to cause look-before-you-leap patterns.
 
425
* It's unsafe if the check is needed to protect the integrity of the
 
426
  user's data.
 
427
* It tends to give poor messages since the developer can get by with
 
428
  no explanatory text at all.
 
429
* We can't rely on people always running with -O in normal use, so we
 
430
  can't use it for tests that are actually expensive.
 
431
* Expensive checks that help developers are better turned on from the
 
432
  test suite or a -D flag.
 
433
* If used instead of ``self.assert*()`` in tests it makes them falsely
 
434
  pass with -O.
376
435
 
377
436
emacs setup
378
437
===========
423
482
    finally:
424
483
        f.close()
425
484
 
 
485
 
 
486
Terminology
 
487
===========
 
488
 
 
489
Bazaar is a GNU project and uses standard GNU terminology, especially:
 
490
 
 
491
 * Use the word "Linux" to refer to the Linux kernel, not as a synechoche
 
492
   for the entire operating system.  (See `bug 528253
 
493
   <https://bugs.launchpad.net/bzr/+bug/528253>`_).
 
494
 
 
495
 * Don't say "open source" when you mean "free software".
 
496
 
 
497
 
 
498
Dynamic imports
 
499
===============
 
500
 
 
501
If you need to import a module (or attribute of a module) named in a
 
502
variable:
 
503
 
 
504
 * If importing a module, not an attribute, and the module is a top-level
 
505
   module (i.e. has no dots in the name), then it's ok to use the builtin
 
506
   ``__import__``, e.g. ``__import__(module_name)``.
 
507
 * In all other cases, prefer ``bzrlib.pyutils.get_named_object`` to the
 
508
   built-in ``__import__``.  ``__import__`` has some subtleties and
 
509
   unintuitive behaviours that make it hard to use correctly.
 
510
 
426
511
..
427
512
   vim: ft=rst tw=74 ai