Editing C++ style guide

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 84: Line 84:
   }
   }
</syntaxhighlight>
</syntaxhighlight>
==== #ifdef directives ====
Indent code that follows a conditional processor directive such as {{codeline|#ifdef}} or {{codeline|#else}}.
Example
<syntaxhighlight lang="cpp">
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif
</syntaxhighlight>
The '#' character may also be placed with the directive rather than remaining in column 1 if this looks better.


==== Split long expressions ====
==== Split long expressions ====
Line 169: Line 155:
=== Namespace ===
=== Namespace ===


All code should be in the {{codeline|octave}} namespace or in a namespace below it.
All code should be in the octave namespace.  This is an ongoing project.  We
are still moving existing classes into namespaces but all new classes
should go somewhere into the "octave" namespace.  There is 1 extra level for namespaces
inside octave to be used with care, we don't want too many namespaces.
Ask before creating a new namespace.


Namespaces should start and stop using the special macros {{codeline|OCTAVE_BEGIN_NAMESPACE(XXX)}} and {{codeline|OCTAVE_END_NAMESPACE(XXX)}}. There is no indentation of code that is placed into namespaces using these macros.
* Indent namespaces as any other block.  Emacs and other editors can do this automatically.
* Define namespace on the .cc files;
* Do not use "using X" directives;
* Do not declare anything on the std namespace;


Example
{{Code|namespace style on a .h file|<syntaxhighlight lang="cpp">
 
// Note indentation
{{Code|Use of namespace macros|<syntaxhighlight lang="cpp">
namespace octave
OCTAVE_BEGIN_NAMESPACE(octave)
{
 
  namespace math
OCTAVE_BEGIN_NAMESPACE(math)
  {
 
    class foo
template <typename T>
    {
void
    public:
umfpack_report_control (const double *Control);
      foo (...);
 
    };
OCTAVE_END_NAMESPACE(math)
  }
OCTAVE_END_NAMESPACE(octave)
}
</syntaxhighlight>}}
</syntaxhighlight>}}


If bare namespace directives must be used, as occasionally is required in Qt code, then the code within the namespace should be indented.
{{Code|namespace style on a .cc file|<syntaxhighlight lang="cpp">
 
// Note indentation and that functions are not defined
{{Code|bare namespace usage|<syntaxhighlight lang="cpp">
// as "octave::math::foo:foo"
// Note indentation and that functions are not defined as "octave::math::foo:foo"
namespace octave
namespace octave
{
{
Line 203: Line 195:
}
}
</syntaxhighlight>}}
</syntaxhighlight>}}
==== Other Guidelines ====
* Do not use {{codeline|using XXX;}} directives
* Do not declare anything on the {{codeline|std::}} namespace


== Naming ==
== Naming ==
Line 296: Line 284:
|}
|}


When passing variables that are large, but will not be changed in a subroutine (read-only), use {{codeline|const}} references.  This helps avoid overflowing the finite stack capacity of a program while still ensuring that read-only access is enforced.
When passing variables that are large, but will not be changed in a subroutine (read-only), consider using 'const' references.  This helps avoid overflowing the finite stack capacity of a program while still ensuring that read-only access is enforced.


{| class="wikitable"
{| class="wikitable"
Line 327: Line 315:
</syntaxhighlight>
</syntaxhighlight>
|}
|}
=== new/delete ===
Pointers that will be allocated memory with {{codeline|new}} should be initialized with the C++ literal {{codeline|nullptr}}, not the numerical value 0 or the macro {{codeline|NULL}}.
The {{codeline|delete}} keyword accepts {{codeline|nullptr}} and programmers should not put an {{codeline|if (ptr)}} guard around {{codeline|delete}}.
{| class="wikitable"
! style="color:green;" | good
! style="color:darkred;" | bad
|-
| <syntaxhighlight lang="c++">
delete ptr;
</syntaxhighlight>
| <syntaxhighlight lang="c++">
if (ptr)
  delete ptr;
</syntaxhighlight>
|}
=== lambda expressions ===
When capturing variables from the surrounding function, explicitly list the variables being captured rather than relying on a default capture by value (`[=]`) or by reference (`[&]`).  This more clearly captures the programmer's intent and makes the code more understandable.


=== std::string ===
=== std::string ===
Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Templates used on this page: