Places/Coding Style: Difference between revisions
(Created page with 'As a general rule we follow conventions reported in the global coding style page: https://developer.mozilla.org/En/Developer_Guide/Coding_Style With some differences: if/else…') |
mNo edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
As a general rule we follow conventions reported in the | As a general rule we follow conventions reported in the [https://developer.mozilla.org/En/Developer_Guide/Coding_Style Global Coding Style page].<br> | ||
With some differences: | With some differences: | ||
Line 22: | Line 22: | ||
catch(ex) {} | catch(ex) {} | ||
</pre> | </pre> | ||
Oneliners statements inside if/elseif/else conditions can avoid braces, but if any side of the condition uses braces then all sides will take them:<br> | Oneliners statements inside if/elseif/else conditions can avoid braces, but if any side of the condition uses braces then all sides will take them:<br> | ||
<pre>if (condition) | <pre>if (condition) | ||
statement; | statement; | ||
Line 36: | Line 36: | ||
statement2; | statement2; | ||
} | } | ||
</pre> | </pre> | ||
When instantiating services in cpp use this form:<br> | When instantiating services in cpp use this form:<br> | ||
<pre>nsCOMPtr<nsIAnnotationService> annoSvc = | <pre>nsCOMPtr<nsIAnnotationService> annoSvc = | ||
do_GetService(NS_ANNOTATIONSERVICE_CONTRACTID); | do_GetService(NS_ANNOTATIONSERVICE_CONTRACTID); | ||
NS_ENSURE_TRUE(annoSvc, NS_ERROR_OUT_OF_MEMORY); | NS_ENSURE_TRUE(annoSvc, NS_ERROR_OUT_OF_MEMORY); | ||
</pre> | </pre> | ||
To null check other | To null check other pointers use NS_ENSURE_STATE(something) unless it's a memory mapping issue.<br> | ||
When defining cpp methods opening brace | When defining cpp methods opening brace and type are in new lines:<br> | ||
<pre>type* | <pre>type* | ||
Class::NewMethod(type aParam, type* _outParam) | |||
{ | { | ||
impl; | impl; | ||
} | } | ||
</pre> | </pre> | ||
Constructors params are inited like this (notice order of inited params should be the same as they are defined in the class definition, otherwise GCC will warn)<br> | Notice that input params are always starting with "a" letter, while output params should be prefixed with an underscore. | ||
<pre>nsNavHistory:: | |||
: | Constructors params are inited like this (notice order of inited params should be the same as they are defined in the class definition, otherwise GCC will warn)<br> | ||
, | <pre> | ||
nsNavHistory::Method(type1* aParam1, | |||
const type2& aParam2) | |||
: mProperty1(0) | |||
, mPRoperty2(PR_FALSE) | |||
{ | { | ||
impl; | impl; | ||
} | } | ||
</pre> | </pre> | ||
Use namespace mozilla::places where possible, or anonymous namespace for file scope. | Use namespace mozilla::places where possible, or anonymous namespace for file scope. | ||
Add Places or nsPlaces in front of components or modules names, so that they are easily recognizeable in components/modules folders and also to prevent naming conflicts.<br> | |||
<br> | |||
If both [https://developer.mozilla.org/En/Developer_Guide/Coding_Style Globarl Coding Style page] and this page cannot answer your question regarding code style, try checking [http://mxr.mozilla.org/mozilla-central/source/storage/style.txt Storage code style document], we don't follow all Storage code style conventions, but it contains some interesting idea.<br> |
Latest revision as of 11:26, 24 February 2010
As a general rule we follow conventions reported in the Global Coding Style page.
With some differences:
if/else if/else conditions, as well as try/catch should never be inline with braces:
if (condition) { statement1; statement2; } else if (condition) { statement1; statement2; } else { statement1; statement2; } try { statement; } catch(ex) {}
Oneliners statements inside if/elseif/else conditions can avoid braces, but if any side of the condition uses braces then all sides will take them:
if (condition) statement; else statement; if (condition) { statement; } else { statement1; statement2; }
When instantiating services in cpp use this form:
nsCOMPtr<nsIAnnotationService> annoSvc = do_GetService(NS_ANNOTATIONSERVICE_CONTRACTID); NS_ENSURE_TRUE(annoSvc, NS_ERROR_OUT_OF_MEMORY);
To null check other pointers use NS_ENSURE_STATE(something) unless it's a memory mapping issue.
When defining cpp methods opening brace and type are in new lines:
type* Class::NewMethod(type aParam, type* _outParam) { impl; }
Notice that input params are always starting with "a" letter, while output params should be prefixed with an underscore.
Constructors params are inited like this (notice order of inited params should be the same as they are defined in the class definition, otherwise GCC will warn)
nsNavHistory::Method(type1* aParam1, const type2& aParam2) : mProperty1(0) , mPRoperty2(PR_FALSE) { impl; }
Use namespace mozilla::places where possible, or anonymous namespace for file scope.
Add Places or nsPlaces in front of components or modules names, so that they are easily recognizeable in components/modules folders and also to prevent naming conflicts.
If both Globarl Coding Style page and this page cannot answer your question regarding code style, try checking Storage code style document, we don't follow all Storage code style conventions, but it contains some interesting idea.