代码格式化工具Astyle1- Brace Style Options

       dddd,工作中总会遇到一言难尽、阅读性低的代码,真的能逼疯一个处女座码农,但是自己手动改任务量太重了,这个时候格式化工具就尤为重要。

       代码格式化工具选择astyle主要是感觉比较容易上手,且开放源代码,版本一直在保持更新;

要使用Astyle就得了解它的一些命令参数,今天先浅看下括号格式选择吧~

        Astyle是内置了15种Brace Style Options,个人觉得Allman style就挺好的,如果else后面的return 0能加个大括号就更好了!

        from:Artistic Style

Allman style

命令参数:--style=allman / --style=bsd / --style=break / -A1

int Foo(bool isBar)
{
    if (isBar)
    {
        bar();
        return 1;
    }
    else
        return 0;
}

Java style

命令参数:--style=java / --style=attach / -A2

int Foo(bool isBar) {
    if (isBar) {
        bar();
        return 1;
    } else
        return 0;
}

Kernighan & Ritchie style

  • uses linux braces.
  • namespaces, 类和函数定义的括号另起一行对齐写;
  • 其他所有包括函数中的数组、结构、枚举和语句等,大括号都是紧跟其后。
  • 使用k&r风格可能会由于&而导致一些问题。这可以通过将k&r括在引号中(例如‑style=“k&r”)或使用‑style=kr或‑style=k/r中的一种来解决

命令参数:--style=kr / --style=k&r / --style=k/r / -A3

int Foo(bool isBar)
{
    if (isBar) {
        bar();
        return 1;
    } else
        return 0;
}

Stroustrup style

  • uses linux braces with closing headers broken from closing braces (e.g. ‑‑break‑closing‑headers).
  • 函数定义的括号另起一行对齐写;
  • 其他所有包括namespaces、类、函数中的数组、结构、枚举和语句等,大括号都是紧跟其后。
  • 这种风格通常与“attach-closing-while”、制表符缩进和5个空格的缩进一起使用。

命令参数:--style=stroustrup / -A4

int Foo(bool isBar)
{
    if (isBar) {
        bar();
        return 1;
    }
    else
        return 0;
}

Whitesmith style

  • 全部使用另起一行的、缩进的大括号。
  • Switch 块和class块缩进,以防止使用以下case语句和C++类修饰符(public、private、protected)进行“挂起缩进”。 

命令参数:--style=whitesmith / -A5

int Foo(bool isBar)
    {
    if (isBar)
        {
        bar();
        return 1;
        }
    else
        return 0;
    }

VTK (Visualization Toolkit) style

  • 类、数组、结构、枚举和函数定义,这些的大括号另起一行对齐写,其他的大括号另起一行缩进写。
  • 开关块缩进,以防止以下case语句出现“挂起缩进”。

命令参数:--style=vtk / -A15

int Foo(bool isBar)
{
    if (isBar)
        {
        bar();
        return 1;
        }
    else
        return 0;
}


Ratliff style

  • 左大括号紧跟其后,右大括号另起一行缩进写;
  • 开关块和类块是缩进的,以防止使用以下case语句和C++类修饰符(public、private、protected)进行“挂起缩进”。

命令参数:--style=ratliff / --style=banner / -A6

int Foo(bool isBar) {
    if (isBar) {
        bar();
        return 1;
        }
    else
        return 0;
    }

GNU style

  • 函数的大括号另起一行对齐写,其余的大括号另起一行缩进写;
  • 整个块是缩进的,而不仅仅是大括号。此样式通常用于缩进2个空格。

命令参数:--style=gnu / -A7

int Foo(bool isBar)
{
    if (isBar)
        {
            bar();
            return 1;
        }
    else
        return 0;
}

Linux style

  • uses linux braces.
  • 大括号与命名空间、类和函数定义的大括号另起一行对齐写。
  • 其他所有包括函数中的数组、结构、枚举和语句的大括号紧跟其后。
  • 最小条件缩进为半缩进。如果需要不同的最小条件缩进,请改用K&R样式。这种样式最适合大缩进。它经常与8个空格的缩进一起使用。
  • 也称为内核范式(KNF)样式,这是Linux BSD内核中使用的样式。

命令参数:--style=linux / --style=knf / -A8

int Foo(bool isBar)
{
        if (isFoo) {
                bar();
                return 1;
        } else
                return 0;
}

Horstmann style

  • uses broken braces and run-in statements.
  • 大括号全部另起一行对齐写,
  • 大括号后 缩进写 代码块。此样式通常与3个空格的缩进一起使用。

命令参数:--style=horstmann / --style=run-in / -A9

int Foo(bool isBar)
{   if (isBar)
    {   bar();
        return 1;
    }
    else
        return 0;
}

// 后面的几种风格就不写啦,用的比较少

//具体风格规则可以点击本文最上面的链接查看哦~

One True Brace Style

命令参数:--style=1tbs / --style=otbs / -A10

int Foo(bool isBar)
{
    if (isFoo) {
        bar();
        return 1;
    } else {
        return 0;
    }
}

Google style

命令参数:--style=google / -A14

int Foo(bool isBar) {
    if (isBar) {
        bar();
        return 1;
    } else
        return 0;
}

Mozilla style

命令参数:--style=mozilla / -A16

int Foo(bool isBar)
{
    if (isBar) {
        bar();
        return 1;
    } else
        return 0;
}

Pico style

命令参数:--style=pico / -A11

int Foo(bool isBar)
{   if (isBar)
    {   bar();
        return 1; }
    else
        return 0; }

Lisp style

命令参数:--style=lisp / --style=python / -A12

int Foo(bool isBar) {
    if (isBar) {
        bar()
        return 1; }
    else
        return 0; }

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...