<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[ROS C++代码风格说明]]></title><description><![CDATA[<ol>
<li>动机<br />
代码风格是重要的。整洁一致的代码风格能够让代码更加容易阅读，更容易测试和维护。我们努力编写程序不仅让它现在能够正常工作，同时也要保证能够在许多年后被其他开发者继续使用。</li>
</ol>
<p dir="auto">为了这个目的，我们规定了一系列规则。我们的目标是鼓励敏捷且合理的能够让其他开发者容易理解的代码。</p>
<p dir="auto">这些是指导准则并不是规则。这篇文章并不完全禁止一些C++的模式或者功能，只是介绍更好的实现方式。当偏离这个准则的时候你要在代码中说明原因。</p>
<p dir="auto">最重要的是保持一致性。尽可能的遵守此准则。但是如果你是在更改别人的已经存在的软件包。那就要遵循别人已经存在的代码风格。</p>
<ol start="2">
<li>ROS代码自动格式规范化</li>
</ol>
<p dir="auto">为什么在我们忙着创造机器人的时候要话大量时间去手动修改代码格式。用机器人来自动格式规范化你的代码吧。<a href="https://github.com/davetcoleman/roscpp_code_format" target="_blank" rel="noopener noreferrer">这些指令</a>能够自动规范你的代码。</p>
<ol start="3">
<li>有很多C++的代码是在这个标准发布之前写的。所有在代码库中有很多代码是不符合这个标准的。下面是在使用这些代码是的建议</li>
</ol>
<ul>
<li>所有新开发的软件包都应该符合这个标准</li>
<li>除非你有很多空闲时间，否则不要去尝试把代码修改成符合此规则。</li>
<li>如果你是一个软件包的作者，请花时间把软件包更新为符合这个规则。</li>
<li>如果你在小范围的修改一个软件包，请遵循这个软件包自己的代码风格。不要混合不同风格的代码。</li>
<li>如果你在大范围修改一个软件包，那么请花时间把这个软件包修改为符合此代码风格。</li>
</ul>
<ol start="4">
<li>命名规则<br />
下面的词用来代指各种命名规则
<ul>
<li>CamelCased: 第一个字母大写，以后每个词的首字母大写</li>
<li>camelCased: 第一个字母小写，以后每个词的首字母大写</li>
<li>under_scored: 只使用小写字母，不同词之间用下划线隔开</li>
<li>ALL_CAPITALS: 只使用大写字母，不同的词间用下划线隔开。</li>
</ul>
</li>
</ol>
<p dir="auto">4.1 软件包<br />
ROS软件包的命名规则为under_scored<br />
这个不止适用于C++的代码。查看<a href="http://wiki.ros.org/DevelopersGuide" target="_blank" rel="noopener noreferrer">开发指南</a>了解更多信息</p>
<p dir="auto">4.2 话题和服务<br />
ROS话题和服务的名称命名规则为under_scored<br />
同样这也不止适用于C++代码。查看<a href="http://wiki.ros.org/DevelopersGuide" target="_blank" rel="noopener noreferrer">开发指南</a>了解更多信息</p>
<p dir="auto">4.3 文件<br />
所有的文件名都是 under_scored<br />
源文件的扩展名为.cpp<br />
头文件的扩展名为.h<br />
要描述清楚，比如不要使用laster.cpp使用hokuyo_topurg_laser.cpp.<br />
如果一个文件主要是实现一个class.那么就根据这个类去命名文件。比如 ActionServer 的文件为action_server.h</p>
<p dir="auto">4.3.1 库文件<br />
库文件命名规则为under_scored<br />
不要在lib前缀之后直接添加库名称<br />
比如</p>
<pre><code>lib_my_great_thing ## Bad
libmy_great_thing ## Good
</code></pre>
<p dir="auto">4.4 类和类型<br />
类的命名规则为CamelCased<br />
比如</p>
<pre><code>class ExampleClass;
</code></pre>
<p dir="auto">例外情况，如果这个类名字本身包含缩写那么缩写名称要大写，比如</p>
<pre><code>class HokuyoURGLaser;
</code></pre>
<p dir="auto">根据这个类是什么来命名这个类。如果你无法描述这个类是什么也许你还没有设计好。如果一个类的名称包含了三个词以上，那么这可能是由于你的设计不够清晰。</p>
<p dir="auto">请参考 <a href="https://google.github.io/styleguide/cppguide.html#Type_Names" target="_blank" rel="noopener noreferrer">Google: Type names</a></p>
<p dir="auto">4.5 函数和方法<br />
通常情况下函数和方法的命名规则是camelCased，其参数的命名规则是under_scored，比如</p>
<pre><code class="language-cpp">int exampleMethod(int example_arg);
</code></pre>
<p dir="auto">函数和方法通常是为了实现否个行为，所以他们的名称要体现出它们要做什么。比如使用checkForErrors()而不要使用errorCheck()，使用dumpDataToFile() 而不要使用dataFile()。类通常都是名词。通过把函数名称设计为动词可以让其他的命名更加自然。</p>
<p dir="auto">4.6 变量<br />
通常情况下变量名是under_scored<br />
变量命名要尽可能的具有描述性。更长的变量名并不会占用更多的内存。当然整型迭代变量可以非常的短。比如i,j,k.但是变量使用一定要保持一致性。比如i在外部循环，j在内部循环。</p>
<p dir="auto">STL迭代变量要能看出来他们是迭代器</p>
<pre><code>std::list&lt;int&gt; pid_list;
std::list&lt;int&gt;::iterator pid_it;
</code></pre>
<p dir="auto">或者从变量名能够看出迭代器的类型</p>
<pre><code>std::list&lt;int&gt; pid_list;
std::list&lt;int&gt;::iterator int_it;
</code></pre>
<p dir="auto">4.6.1 常量<br />
常量使用ALL_CAPITALS<br />
4.6.2 成员变量<br />
类的成员变量使用 under_scored。同时尾部添加一个下划线</p>
<pre><code>int example_int_;
</code></pre>
<p dir="auto">4.6.3 全局变量<br />
尽可能避免使用全局变量。当使用时，全局变量要under_scored 同时在前面加上g_</p>
<pre><code>// I tried everything else, but I really need this global variable
int g_shutdown;
</code></pre>
<p dir="auto">4.7 命名空间<br />
命名空间要under_scored<br />
5. 协议声明<br />
所有的源文件和头文件头部都必须包含协议和版权声明。<br />
在ros-pkg和wg-ros-pkg源LICENSE 文件夹中包含了协议的模板。<br />
查看<a href="http://wiki.ros.org/DevelopersGuide" target="_blank" rel="noopener noreferrer">ROS开发指南</a>了解更多关于协议和版权的信息。</p>
<ol start="6">
<li>格式<br />
你的编辑器应该能够自动设置格式。<br />
每一个块级结构要缩进两个空格，不要使用tab<br />
命名空间的内容并不需要缩进。<br />
大括号无论是左括号还是右括号都要独占一行。</li>
</ol>
<pre><code class="language-cpp">if(a &lt; b)
{
  // do stuff
}
else
{
  // do other stuff
}
</code></pre>
<p dir="auto">如果只有一行代码，那么括号可以省略</p>
<pre><code class="language-cpp">if(a &lt; b)
  x = 2*a;
</code></pre>
<p dir="auto">当执行的代码比较复杂的时候不要省略大括号</p>
<pre><code>if(a &lt; b)
{
  for(int i=0; i&lt;10; i++)
    PrintItem(i);
}
</code></pre>
<p dir="auto">下面是一个更完整的例子</p>
<pre><code>/*
 * A block comment looks like this...
 */
#include &lt;math.h&gt;
class Point
{
public:
  Point(double xc, double yc) :
    x_(xc), y_(yc)
  {
  }
  double distance(const Point&amp; other) const;
  int compareX(const Point&amp; other) const;
  double x_;
  double y_;
};
double Point::distance(const Point&amp; other) const
{
  double dx = x_ - other.x_;
  double dy = y_ - other.y_;
  return sqrt(dx * dx + dy * dy);
}
int Point::compareX(const Point&amp; other) const
{
  if (x_ &lt; other.x_)
  {
    return -1;
  }
  else if (x_ &gt; other.x_)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}
namespace foo
{
int foo(int bar) const
{
  switch (bar)
  {
    case 0:
      ++bar;
      break;
    case 1:
      --bar;
    default:
    {
      bar += bar;
      break;
    }
  }
}
} // end namespace foo
</code></pre>
<p dir="auto">6.1 行的长度<br />
一行最多包含120个字符</p>
<p dir="auto">6.2 #ifndef 保护<br />
所有的头文件都必须用#ifndef保护起来</p>
<pre><code>#ifndef PACKAGE_PATH_FILE_H
#define PACKAGE_PATH_FILE_H
...
#endif
</code></pre>
<p dir="auto">这部分保护代码要立即添加在协议声明之后。同时#endif添加在文件尾部。</p>
<ol start="7">
<li>文档<br />
代码必须要有文档。没有文档的代码也许能够工作但是没有办法维护。<br />
我们使用<a href="http://www.doxygen.org/" target="_blank" rel="noopener noreferrer">doxygen</a>来自动生成文档。Doxygen 会解析你的代码从代码中提取出对应的文档。</li>
</ol>
<p dir="auto">查看<a href="http://wiki.ros.org/rosdoc" target="_blank" rel="noopener noreferrer">rosdoc</a>页面了解更多的doxygen文档信息。</p>
<p dir="auto">所有的方法，函数，类，类成员变量，枚举类型，和常量都需要文档说明。</p>
<ol start="8">
<li>终端输出<br />
避免使用printf 和cout。使用<a href="http://wiki.ros.org/rosconsole" target="_blank" rel="noopener noreferrer">rosconsole</a>来输入信息。其具有下面的优点</li>
</ol>
<ul>
<li>有颜色</li>
<li>能够通过显示级别来控制输出</li>
<li>通过/rosout发布，所以网络中的其他用户也能查看</li>
<li>可以选择输出到文件中。</li>
</ul>
<ol start="9">
<li>宏定义</li>
</ol>
<p dir="auto">尽量避免使用宏定义。和常量变量和内联函数不同，宏定义既没有类型也没有作用域。</p>
<ol start="10">
<li>预处理指令(#if #ifdef)<br />
对于条件编译的情况，总是使用#if 不要使用#ifdef<br />
有些人会这样写代码</li>
</ol>
<pre><code class="language-cpp">#ifdef DEBUG
        temporary_debugger_break();
#endif
</code></pre>
<p dir="auto">其他人可能会这样编译</p>
<pre><code>cc -c lurker.cpp -DDEBUG=0
</code></pre>
<p dir="auto">总是使用#if,。这样能够保证程序正常工作。即使DEBUG没有定义。</p>
<pre><code class="language-cpp">#if DEBUG
        temporary_debugger_break();
#endif
</code></pre>
<ol start="11">
<li>输出参数<br />
函数和方法中的输出参数通过传指针进行而不是传引用。</li>
</ol>
<pre><code>int exampleMethod(FooThing input, BarThing* output);
</code></pre>
<p dir="auto">通过传引用的方式，调用者无法判断参数是否能被修改。</p>
<ol start="12">
<li>命名空间<br />
鼓励使用命名空间来给代码增加作用域。根据软件包的功能选择一个具有描述性的名称。不要在头文件中使用using-directives . 否则所有使用这个头文件的文件的命名空间都会被污染.使用 using-declarations。这样只有想要使用的名称才会被影响。<br />
比如不要使用</li>
</ol>
<pre><code class="language-cpp">using namespace std; // Bad, because it imports all names from std::
</code></pre>
<p dir="auto">使用</p>
<pre><code>using std::list;  // I want to refer to std::list as list
using std::vector;  // I want to refer to std::vector as vector
</code></pre>
<ol start="13">
<li>继承<br />
继承是经常用来定义接口的方式。基础类定义了接口，然后子类实现对应接口。<br />
继承也通常被用来给子类提供通用的代码。这种继承的使用方法是不建议的。因为子类中包含了基类的实例，所以可以实现同样的目的，同时也有更少的迷惑性。</li>
</ol>
<p dir="auto">当在子类中覆盖一个虚拟方法时，一定要声明称virtual，这样读者才知道发生了什么。</p>
<p dir="auto">13.1 多重继承<br />
尽量避免多重继承，它会产生各种各样的问题。</p>
<ol start="14">
<li>异常<br />
异常是相对于返回整型错误码更好的一种错误报告方式。<br />
一定要把你的软件包中每个函数和方法可能抛出的异常在文档中说明。<br />
不要在destructors中抛出异常。<br />
不要在你不直接调用的回调函数中抛出异常<br />
如果你在代码中使用错误码而不使用异常，那么就一直使用错误码。一定要保持一致性。</li>
</ol>
<p dir="auto">14.1 编写异常安全的代码<br />
当你的代码能过够被异常中断时，一定要确认所有的资源都被释放。比如互斥锁要释放，动态内存要释放。</p>
<ol start="15">
<li>枚举类型<br />
把你的枚举类型放在命名空间中</li>
</ol>
<pre><code class="language-cpp">namespace Choices
{
  enum Choice
  {
     Choice1,
     Choice2,
     Choice3
  };
}
typedef Choices::Choice Choice;
</code></pre>
<p dir="auto">这样方式枚举类型污染命名空间。枚举类型中的元素可以Choices::Choice1这样访问。但是typedef 仍然允许在命名空间外使用去声明Choice枚举类型。</p>
<ol start="16">
<li>Globals<br />
全局的无论是变量还是方法都要尽量避免使用。他们会污染命名空间，减少代码的重用性。</li>
</ol>
<p dir="auto">全局变量是最最应该避免的。它阻碍了多实例，同时使得多线程编程成为噩梦。</p>
<p dir="auto">大部分的变量和函数应当在类里面声明。剩下的要在命名空间中声明。<br />
例外情况: 一个文件可能包含main()函数和其他一些小的全局的工具函数。但是注意这些有用的函数可能以后也对其他人很有用。</p>
<ol start="17">
<li>
<p dir="auto">静态类变量<br />
尽量避免使用静态类变量。它同样也会使得无法多次实例化代码，同时也使得多线程编程变成一个噩梦</p>
</li>
<li>
<p dir="auto">调用exit()<br />
只在设计好的一个退出程序的地方调用exit()<br />
不要在库文件中调用exit</p>
</li>
<li>
<p dir="auto">Assertions<br />
使用assertions 来检查条件，数据结构，和内存管理器的返回值。使用Assertions 要比使用条件声明更好。<br />
不要直接使用assert()。使用在<strong>ros/assert.h</strong>里面声明的函数</p>
</li>
</ol>
<pre><code>/** ROS_ASSERT asserts that the provided expression evaluates to
 * true.  If it is false, program execution will abort, with an informative
 * statement about which assertion failed, in what file.  Use ROS_ASSERT
 * instead of assert() itself.
 * Example usage:
 */
   ROS_ASSERT(x &gt; y);
</code></pre>
<pre><code>/** ROS_ASSERT_MSG(cond, "format string", ...) asserts that the provided
 * condition evaluates to true.
 * If it is false, program execution will abort, with an informative
 * statement about which assertion failed, in what file, and it will print out
 * a printf-style message you define.  Example usage:
 */
   ROS_ASSERT_MSG(x &gt; 0, "Uh oh, x went negative.  Value = %d", x);
</code></pre>
<pre><code>/** ROS_ASSERT_CMD(cond, function())
 * Runs a function if the condition is false. Usage example:
 */
   ROS_ASSERT_CMD(x &gt; 0, handleError(...));
</code></pre>
<pre><code>/** ROS_BREAK aborts program execution, with an informative
 * statement about which assertion failed, in what file. Use ROS_BREAK
 * instead of calling assert(0) or ROS_ASSERT(0). You can step over the assert
 * in a debugger.
 * Example usage:
 */
   ROS_BREADK();
</code></pre>
<p dir="auto">不要在assertion里面运行程序。只用它来检查逻辑条件。根据编译的设置assertion可能不会被执行。</p>
<p dir="auto">在软件开发的时候开启assertion检查时很必要的。当软件接近完成，在深入的测试代码中assertion一直成立。那么你可以添加一个编译flag，把assertion代码从编译中移除。这样这部分代码就不会在占用空间和CPU资源。下面的catkin_make选项会为你的所有的软件包定义NDEBUG宏，从而移除assertion检查。</p>
<pre><code>catkin_make -DCMAKE_CXX_FLAGS:STRING="-DNDEBUG"
</code></pre>
<p dir="auto">注意: 当你执行这个命令后cmake会编译你的所有软件。同时会把设置记录下来。除非你删除build和devel文件夹。</p>
]]></description><link>http://community.bwbot.org/topic/468/ros-c-代码风格说明</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 19:18:03 GMT</lastBuildDate><atom:link href="http://community.bwbot.org/topic/468.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 May 2018 06:52:40 GMT</pubDate><ttl>60</ttl></channel></rss>