如何在Delphi和C#中格式化复合语句?

如何在Delphi和C#中格式化复合语句?,c#,delphi,coding-style,code-formatting,C#,Delphi,Coding Style,Code Formatting,作为一名长期的Pascal和Delphi开发人员,我总是以这样的方式排列我的开始和结束: begin if x = y then begin ... ... end else for i := 0 to 20 do begin ... ... end; end; 让我抓狂的是这样的代码格式: begin if x = y then begin ... ... end else

作为一名长期的Pascal和Delphi开发人员,我总是以这样的方式排列我的开始和结束:

begin
  if x = y then
  begin
     ...
     ...
  end
  else
    for i := 0 to 20 do
    begin
      ...
      ...
    end;
end;
让我抓狂的是这样的代码格式:

begin
  if x = y then begin
     ...
     ...
  end
  else
    for i := 0 to 20 do begin
      ...
      ...
    end;
end;
当有几个层次的复合语句时,我发现这很难理解。上面的代码是可以的,因为它没有那么复杂,但为了一致性,我希望所有的开始和结束都对齐

当我开始使用c#时,我发现自己也在对齐花括号。C#世界的标准是什么

编辑:


有人指出,这是一种不应该这样问的问题。我不明白为什么不。我正在建立一个编码指南文档。我知道我会对某些事情产生一些阻力,我希望在这里得到一些答案,这样我就可以准备好面对阻力。

每个人都有不同的偏好。在我的例子中,我在学习Pascal之前学习了Modula-2。Modula-2没有BEGIN关键字,每个块都需要一个END。因此代码可能如下所示(Modula-2碰巧对大写关键字区分大小写):

当我开始用Pascal编写代码时,它变成了:

if x = y then begin
    ....
end;
这样,代码看起来更像我以前看到的,同时仍然在可接受的Pascal代码范围内

对我来说,这些早期的印象影响了我对几乎所有其他语言的大括号和缩进风格的偏好。C代码实际上没有任何特定的“规范”,就像C或Pascal没有一样


唯一需要遵循的真正规则是:在处理现有代码时,使用已经存在的样式。在编写新代码时,请使用您喜欢的样式。

我绝不会以这种方式编写代码(您的第二个示例)。要么(首选)

结束

有时我使用这种折叠(如第二种情况)来组合if和try..finally

x := GetMeTheObject;
if assigned(x) then try
  ...
finally FreeAndNil(x); end;
我曾经在Delphi中使用“悬挂”开头:

if (SomeCondition) then begin
  ...
end;
if a=b then begin
  c;
end else begin
  d;
end;

if x=y then z;
奇怪的是,我没有使用C,因为我发现它更可读:

if (SomeCondition)
{
  ...
}
过了一段时间,为了便于阅读,我不再到处保存一行:

if (SomeCondition) then 
begin
  ...
end;
我还使用显式的开始/结束块,我认为这可以提高可读性。我不需要“聪明”。我需要能够一目了然地遵循代码的意图。更重要的是,每个可能阅读/维护它的人都是如此

if x = y then 
begin
  ...
  ...
end
else
begin
  for i := 0 to 20 do 
  begin
    ...
    ...
  end;
end;
我通常不介意是否有一个明显的单一声明

if (SomeCondition) then
  ...
我个人使用:

if Condition then
begin
  DoThis;
end else
begin
  DoThat;
end;

在复合if语句中,将每个 在一个新的 行:示例:


就个人而言,我更喜欢将连接语句压缩到一行。例如,
在一行上结束else
。更清楚地表明下一个语句块与当前语句块相关。

我倾向于在Delphi中这样做:

if (SomeCondition) then begin
  ...
end;
if a=b then begin
  c;
end else begin
  d;
end;

if x=y then z;

只是因为我发现它比额外的换行符更可读。显然,如果我与其他人一起工作,我会使用我们同意的任何标准(或当前代码库使用的任何标准),但当我是唯一一个从事这项工作的人(如个人项目)时,我会这样做。

C世界中的标准是将右大括号与开始语句对齐:

if (I am nuts) {
    Psychiatry
}
甚至可以将开口撑杆放在自己的线上:

if (I am nuts)
{
    Psychiatry
}
在某些样式中,大括号具有不同的缩进:

if (I am nuts)
  {
    Psychiatry
  }
甚至

if (I am nuts)
    {
    Psychiatry
    }
我在Perl中很长一段时间使用了第一种样式,这是我继续使用else的方式:

if (I am nuts) {
    Psychiatry
  } else {
    I am free
}
但是,在接触Lisp之后,当我已经正确缩进时,我看不到将大括号放在它们自己的行上有什么额外的价值:

if (I am completely nuts) {
    Psychiatry }
  else {
    I am free } 
不过,我不希望用这些想法改变传统的C方式


顺便说一句,Python完全抛弃了大括号,只依赖缩进,但我认为这太过分了,因为它导致了一些荒谬的事情,比如lambda只能有一条语句。

有人发帖说,他们会写以下内容:

if x=y then z;
我真的不喜欢这个,它和美学无关。假设x、y和z是函数,如果我单步执行代码,上面的意思是我不能跨过x和y,然后跨入z

1   if x=y then
2     Z;

现在,我可以进入第2行,而不必进入第1行。

我总是按照您的示例排列开始/结束。(除了For语句周围还有一个Begin/End,以防万一以后再添加代码。)

无论如何,如果您的代码是多层次的,那么将开始/结束放在哪里并不重要,因为它太复杂了。如果你发现自己超过了,比如说,3个层次的深度,停下来,简化。创建子例程来清理问题

CodeComplete是关于这类事情的最好的参考书。如果你读那本书还学不到什么,那我就不干了

不久前我用过

if <cond> then
  begin
  ShowMessage('balablala');
  exit;
  end;
if-then
开始
ShowMessage(“balabala”);
出口
结束;
但现在我遵循的标准是

if <cond> then
begin
  ShowMessage('balablala');
  exit;
end;
if-then
开始
ShowMessage(“balabala”);
出口
结束;

正如在

中一样,我倾向于总是将IF和ELSE对齐,并缩进BEGIN/END块。如果我有一个多重条件,如果,我把它分成多行。如果我发现自己的想法越来越深刻,那么我会重新思考我正在编写的代码或重构成多种方法。因此,我的代码如下所示:

if condition1 then
  begin
    // do something
  end
else // not condition1
  begin
    // do something else
  end;
或者更复杂的if条件句

if condition1 or
  condition2 or
  condition3 and
  ( condition4 or
    condition5 )
then
  begin
    // do something
  end
else // not conditions
  begin
    // do something else
  end;

这一切都很有趣。我有自己独特的对齐方式,奇怪的是,我在几种语言中都使用这种方式,包括Pascal、C甚至COBOL(尽管这种语言的使用时间不长)

我想我第一次看到它是在肯·奥尔的课堂上。我的版本也非常类似于有一个越位规则(类似于Python和F#),您可以使用缩进来显示嵌套

无论如何,我是这样做的:

begin  if x = y 
       then begin (* stack to avoid getting too much indentation *)
            ...     
            ...  
            end  
       else for i := 0 to 20 
             do begin      
                ...      
                ...    
                end;
       end;
是的,C/C++/Java/C的风格类似于

  {  if (x == y)
          {  ...   /* Space as if 'then' is there */  
             ...  
             }
     else for (int i = 0; i<21; i++)
              {  ... /* space as if 'do' is there */
                 ...  
                 }
     }
{if(x==y)
{…/*空格,好像“then”在那里*/
...
if condition1 or
  condition2 or
  condition3 and
  ( condition4 or
    condition5 )
then
  begin
    // do something
  end
else // not conditions
  begin
    // do something else
  end;
begin  if x = y 
       then begin (* stack to avoid getting too much indentation *)
            ...     
            ...  
            end  
       else for i := 0 to 20 
             do begin      
                ...      
                ...    
                end;
       end;
  {  if (x == y)
          {  ...   /* Space as if 'then' is there */  
             ...  
             }
     else for (int i = 0; i<21; i++)
              {  ... /* space as if 'do' is there */
                 ...  
                 }
     }
if A < B then 
begin
  DoSomething; 
end; // if
with qryTemp do
begin
  First;

  while not Eof do
  begin
    if (A < B)
      or (C < D)
    begin
      DoSomething;
    end else
    begin
      DoSomethingElse;
    end; // if-else        

    Next;
  end; // while
end; // with
if (condition) {
    statements;
} // if
if ((condition1 && condition2)
        || (condition3 && condition4)
        ||!(condition5 && condition6)) {
    doSomethingAboutIt();
} else {
    doSomethingElse();
} // if-else
try
  if Condition1
  or (    Condition2
      and Condition3) then
    begin
      DoSomething
      DoSomeMore;
    end

  else if Condition4 then // I only do this if the nested "if" has "case"-like characteristics
    begin                 // otherwise the if would obviously be indented and on its own line
      DoSomethingElse;

      if Condition5 then
        begin
          DoThisToo;
          DoFoo;
        end;
    end

  else
    DoTheWholeShebang;

  case Whatever of
    A: DoIt;

    B, C, D:
      begin
        DoSomethingSlightly;
        MoreComplicated;
      end;

  else
    begin
      DoWhatever;
      DoLastResort; 
    end;
  end;
except
  on ESomeException do
    begin
      HandleIt;
      raise;
    end;
  on ESomeOtherException do
    DealWithIt;

  else
    DoWhateverItTakes;
end;
if (condition) {
  doSomething;
} else {
  doSomethingElse;
}
if (condition) then begin
  statement;
  ...
  statement; end
else begin
  ...
end;
if (some_long_conditional_expression) or
   (some_other_long_conditional_expression) or
   (some_even_longer_conditional_expression)
then
  Exit;