SegmentOJ代码风格指南

JavaScript

  1. 表示区块起首的大括号,不要另起一行。
  2. 调用函数的时候,函数名与左括号之间没有空格。
  3. 函数名与参数序列之间,没有空格。
  4. 所有其他语法元素与左括号之间,都有一个空格。
  5. 不要省略句末的分号。
  6. 不要使用with语句。
  7. 不要使用"相等"(==)运算符,只使用"严格相等"(===)运算符。
  8. 不要将不同目的的语句,合并成一行。
  9. 所有变量声明都放在函数的头部。
  10. 避免使用全局变量;如果不得不使用,用大写字母表示变量名,比如UPPER_CASE。
  11. 不要使用自增(++)和自减(--)运算符,用+=-=代替。
  12. 总是使用大括号表示区块。

C/C++

配置文件

使用clang-format,配置文件如下:

---
Language:        Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:   
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:   
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks:   Preserve
IncludeCategories: 
  - Regex:           '^<(c[a-z]+)>'
    Priority:        2
  - Regex:           '^<[a-z/]+\.h>'
    Priority:        3
  - Regex:           '^<[a-z]+>'
    Priority:        1
  - Regex:           '^<[a-z/]+\.[a-z]+>'
    Priority:        4
  - Regex:           '^"[a-z]+\.h"'
    Priority:        5
  - Regex:           '^"[a-z]+\.[a-z]+"'
    Priority:        6
  - Regex:           '^"[a-z/]+\.h"'
    Priority:        7
  - Regex:           '^"[a-z/]+\.[a-z]+"'
    Priority:        8
  - Regex:           '.*'
    Priority:        9
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth:     4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp11
TabWidth:        4
UseTab:          Never
ColumnLimit:     99

示例代码

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <sys/acct.h>
#include <time.h>
#include "a.h"
#include "b.hpp"
#include "path/c.h"
using namespace std;

int a;
int b;
char c;
vector<int*> v;

class example {
public:
    int a, b;
    int* c;
    example(int a, int b) a : a, b : b {
        c = new int[1234];
    }
};

template <typename _T>
void max(_T a, _T b) {
    return a < b ? b : a;
}

void empty() {}

int main() {
    int a = 1;
    int b = 2;
    short c = max(a, b);

    switch (a) {
        case 1:
            cout << 1 << endl;
            break;
        case 2:
            cout << 2 << endl;
        default:
            cout << -1 << endl;
    }

    for (int i = 1; i <= a; ++i)
        cout << i << endl;

    if (a == 1)
        ++a;
    else
        ++b;
    return 0;
}
posted @   方而静  阅读(178)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示