Places/Coding Style

< Places
Revision as of 13:52, 15 December 2009 by Mak77 (talk | contribs) (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…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 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 objects use NS_ENSURE_STATE(obj);

When defining cpp methods opening brace goes in new line:

type* newMethod(type param)
{
  impl;
}

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::nsNavHistory()
: mBatchLevel(0)
, mBatchHasTransaction(PR_FALSE)
, mCachedNow(0)
, mExpireNowTimer(nsnull)
, mExpireDaysMin(0)
, mExpireDaysMax(0)
, mExpireSites(0)
, mNumVisitsForFrecency(10)
, mTagsFolder(-1)
, mInPrivateBrowsing(PRIVATEBROWSING_NOTINITED)
, mDatabaseStatus(DATABASE_STATUS_OK)
, mCanNotify(true)
, mCacheObservers("history-observers")
{
  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.