Thunderbird:CodeCleanup: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 20: Line 20:
  <span class="highlightblue">+ LossyCopyUTF16toASCII(uniValue, charValue);</span>
  <span class="highlightblue">+ LossyCopyUTF16toASCII(uniValue, charValue);</span>


Bad:
  nsCString charStr;
  nsCString charStr;
  <span class="highlightred">-PL_strchr(uri.get(), ' ')</span>
  <span class="highlightred">- PL_strchr(uri.get(), ' ')</span>
  <span class="highlightblue">+ uri.FindChar(' '); (kNotFound)</span>
  <span class="highlightblue">+ uri.FindChar(' '); (kNotFound)</span>


Bad:
nsCString charStr;
  nsCString uri;
<span class="highlightred">- PL_strcasecmp(charStr.get(), "nocopy://")</span>
  PL_strcasecmp(uri.get(), "nocopy://")
<span class="highlightblue">+ charStr.LowerCaseEqualsLiteral("nocopy://")</span>
 
Good:
  uri.LowerCaseEqualsLiteral("nocopy://")
    
    
== nsCOMPtr ==
== nsCOMPtr ==


Use swap to assign an object wrapped by a nsCOMPtr into a return variable
Use swap to assign an object wrapped by a nsCOMPtr into a return variable. This saves the cost of a reference count. Don't do this when the nsCOMPtr object is a member variable of a class or you want to use
 
Bad:
  nsCOMPtr<nsIMsgIdentity> identity;
  NS_IF_ADDREF(*aIdentity= identity);
  return NS_OK;
 
Good:
  identity.swap(*aIdentity)
  return NS_OK;


Bad:
nsCOMPtr<nsIMsgIdentity> identity;
  *aIdentity = m_identity;
<span class="highlightred">- NS_IF_ADDREF(*aIdentity = identity);</span>
  NS_IF_ADDREF(*aIdentity);
<span class="highlightblue">+ identity.swap(*aIdentity);</span>
return NS_OK;


Good:
<span class="highlightred">- *aIdentity = m_identity;
  NS_IF_ADDREF(*aIdentity = m_identity);
- NS_IF_ADDREF(*aIdentity);</span>
 
<span class="highlightblue">+ NS_IF_ADDREF(*aIdentity = m_identity);</span>
This saves the cost of a reference count. Don't do this when the nsCOMPtr object is a member variable of a class or you want to use


== Validating Input Arguments ==
== Validating Input Arguments ==
Line 61: Line 48:
Use do_QueryElementAt to get an element from an nsISupportsArray.
Use do_QueryElementAt to get an element from an nsISupportsArray.


Bad:
nsCOMPtr<nsISupportsArray> identityArray;
  nsCOMPtr<nsISupportsArray> identityArray;
<span class="highlightred">- nsCOMPtr<nsIMsgIdentity> identity;
  nsCOMPtr<nsIMsgIdentity> identity;
- rv = identityArray->QueryElementAt(index, NS_GET_IID(nsIMsgIdentity),
  rv = identityArray->QueryElementAt(index, NS_GET_IID(nsIMsgIdentity),
                                    (void **)getter_AddRefs(identity));</span>
                              (void **)getter_AddRefs(identity));
<span class="highlightblue">+ nsCOMPtr<nsIMsgIdentity> identity( do_QueryElementAt(identityArray, i, &rv));</span>
or
 
  nsCOMPtr<nsISupports> thisElement;
  identityArray->GetElementAt(index, getter_AddRefs(thisElement));
  nsCOMPtr<nsIMsgIdentity> identity = do_QueryInterface(thisElement, &rv);


Good:
nsCOMPtr<nsISupportsArray> identityArray;
  nsCOMPtr<nsIMsgIdentity> identity( do_QueryElementAt(identityArray, i, &rv));
<span class="highlightred">-  nsCOMPtr<nsISupports> thisElement;
- identityArray->GetElementAt(index, getter_AddRefs(thisElement));
- nsCOMPtr<nsIMsgIdentity> identity = do_QueryInterface(thisElement, &rv);</span>
<span class="highlightblue">+ nsCOMPtr<nsIMsgIdentity> identity( do_QueryElementAt(identityArray, i, &rv));</span>
272

edits