Jos*_*ons 6 delphi coding-style code-formatting
Code Complete表示,为了清晰起见和作为防御措施,始终使用块标识符是一种好习惯.
自读这本书以来,我一直在虔诚地做这件事.有时看起来似乎过多,如下面的情况.
Steve McConnell是否有权坚持始终使用块标识符?您会使用以下哪些?
//naughty and brief
with myGrid do
  for currRow := FixedRows to RowCount - 1 do
    if RowChanged(currRow) then
      if not(RecordExists(currRow)) then
        InsertNewRecord(currRow)
      else
        UpdateExistingRecord(currRow);
//well behaved and verbose
with myGrid do begin
  for currRow := FixedRows to RowCount - 1 do begin
    if RowChanged(currRow) then begin
      if not(RecordExists(currRow)) then begin
        InsertNewRecord(currRow);
      end  //if it didn't exist, so insert it
      else begin
        UpdateExistingRecord(currRow);
      end;  //else it existed, so update it
    end;  //if any change
  end;  //for each row in the grid
end;  //with myGrid 
Vin*_*Vin 10
我一直遵循' 乖巧和冗长 '的风格,除了那些不必要的额外注释.
不知何故,能够更快地看到代码并使其更有意义,比在解密哪个块结束之前必须花费至少几秒钟更有意义.
提示:C#的Visual Studio KB快捷方式跳转开始和结束:Ctrl +]
如果您使用Visual Studio,那么在块的开头和结尾处使用C#的大括号也有助于您有一个KB快捷方式可以跳转到开始和结束
就个人而言,我更喜欢第一个,因为恕我直言"结束"; 不要告诉我太多,一旦一切都接近,我可以通过身份来判断什么时候会发生什么.
我相信在使用大型语句时,块更有用.您可以进行混合方法,在其中插入一些"开始...结束;"并注释它们结束的内容(例如将其用于with和第一个if).
恕我直言,您也可以将其分解为更多方法,例如,该部分
  if not(RecordExists(currRow)) then begin
    InsertNewRecord(currRow);
  end  //if it didn't exist, so insert it
  else begin
    UpdateExistingRecord(currRow);
  end;  //else it existed, so update it
可以采用单独的方法.
我认为这在某种程度上取决于具体情况.有时你只是有一个这样的方法:
void Foo(bool state)
{
    if (state)
        TakeActionA();
    else
        TakeActionB();
}
我不知道如何使它看起来像这样:
void Foo(bool state)
{
    if (state)
    {
        TakeActionA();
    }
    else
    {
        TakeActionB();
    }
}
完全改善了可读性.
| 归档时间: | 
 | 
| 查看次数: | 459 次 | 
| 最近记录: |