-1

嗨,我正在尝试标记一些代码,请有人检查以确保我已正确完成此操作(为简单起见更改了一些代码):

begin
  if Password <> Database['Database']
    then showmessage ('Message')
    else
  if NewPassword <> Retype
    then showmessage ('Message')
    else
      begin
        if Message (yes, No, etc) =yes
          then
            begin
              List
              List
              List.post;
              showmessage ('Message')
            end
          else close;
      end;
end;
4

4 回答 4

3

这是一个编码风格问题,可能不会在这里存在很长时间。:-) (编码风格在很大程度上是个人意见的问题,那里有很多不同的。)无论如何,我会试一试。:-)

我会做的稍微不同。if..elseIMO,这清楚地显示begin..end了新程序员的正确配对:

begin
  if Password <> Database['Database'] then
    showmessage ('Message')
  else 
    if NewPassword <> Retype then
      showmessage ('Message')
    else
    begin
      if Message (yes, No, etc) = yes then
      begin
        List;
        List;
        List.post;
        showmessage ('Message');
      end
      else
        close;
    end;
end;

在我自己的代码中,我仍然会做一些不同的事情(但只有很小的区别)。我会将 移动else if password到同一行(它减少了一级缩进,对我来说使代码流更加清晰。我们有三个可能的选项,并且清楚地显示了三个选项(if this, else if this, else this):

begin
  if Password <> Database['Database'] then    // option 1
    showmessage ('Message')
  else if NewPassword <> Retype then          // option 2
    showmessage ('Message')
  else                                        // option 3
  begin
    if Message (yes, No, etc) = yes then
    begin
      List;
      List;
      List.post;
      showmessage ('Message');
    end
    else
      close;
  end;
end;

只有几个其他代码区域的格式有时会有所不同。我会尝试尽可能多地快速触摸它们。

案例陈述:

case i of
  0: DoThingForZero;            // Only one line to execute for 0
  1: begin                      // Two things to do for 1
       DoSetupForOne;
       DoThingForOne;
     end;
  2: DoThingForTwo;
else                            // Handle anything other than 0, 1, 2
  DoThingsForOtherValues;
end;

虽然语句:

while not Query1.Eof do
begin
  // Process each field in current record of table
  Query1.Next;  // Move to next row (easy to forget, infinite loop happens. :-)
end;

重复语句:

i := 1;
repeat
  i := i + SomeFunctionResultReturningVariousValues();
until (i >  50)

对于循环:

for i := 0 to List.Count - 1 do
begin
  ProcessItem(List[i]);
end;

for i := List.Count - 1 downto 0 do
  List[i].Delete;

For..in 循环:

for ch in SomeString do           // For each character in a string,
  WriteLn(ch, ' = ', Ord(ch));    // write the ordinal (numeric) value 
ReadLn;

尝试..最后:

SL := TStringList.Create;        // Create object/open file/whatever (resource)
try
  // Code using resource 
finally
  SL.Free;                       // Free the resource
end;

尝试..除了:

try
  // Do something that might raise an exception
except
  on E: ESomeVerySpecificException do
  begin
     // Handle very specific exception 
  end;
  on E: ESomeLessSpecificException do
  begin
    // Handle less specific exception
  end;
  else
    raise;
end;

Try..finally 与 try..except:

SL := TStringList.Create;         // Allocate resource
try
  try
    // Do something that might raise exception
  except
    // Handle exception as above
  end;
finally
  SL.Free;                       // Free resource
end;
于 2014-02-21T17:56:06.730 回答
0

就个人而言,我是一个else if男人,我也反对绞刑then

procedure outer;
begin {outer's}
    if 2 * 2 = 4 then
    begin {this belongs to if...then level}
        Writeln('make bools');
    end
    else if Sin(X) = 3 then
    begin {and this too}
        Writeln('not war');
    end;
    else if True and SoOn then
        {...}
end; {/outer's}

但在你的情况下,我最好避免在else分支中使用大型复合语句和大量嵌套:

begin
    if not CurrentPasswordIsCorrect then
    begin
        Notify('bad pass');
        Exit;
     end;

     if NewPasswordEntry1 <> NewPasswordEntry2 then
     begin
        Notify('you forgot your password already');
        Exit;
     end;

     { and so on }
end;

有时,将正常流代码编写到then分支和异常到else分支的规则会成为错误的朋友,并邀请编写大量嵌套语句,else这反过来只会损害可读性而不是提高可读性。

于 2014-02-21T17:57:56.150 回答
0

正如其他答案中所指出的,风格是高度主观的,因此很难给出客观的答案。但是,可以说的是您的代码应该如何根据 Embarcadero 的Object Pascal Style Guide显示,这就是我将尝试回答的问题(比您要求的更详细)。当同一个问题在多个地方发生时,我只提一次。

begin
  if Password <> Database['Database']
    then showmessage ('Message')

在这里,大小写showmessage应该是ShowMessage,这也是Dialogs单元中过程的名称。ShowMessage和之间不应有空格(

then在下一行的使用是不寻常的,但没关系,在这种情况下你可以缩进它。

    else
  if NewPassword <> Retype

这第二if条语句是else第一条语句分支的一部分if,应该相应地缩进,可能通过将它放在与else.

    then showmessage ('Message')
    else
      begin

begin并且end不应该有额外的缩进:子语句应该是else上面右边的两个空格。

        if Message (yes, No, etc) =yes

之前有一个空格=,但之后没有空格,这不是我以前见过的,但这不是样式指南表达任何偏好的情况,所以没关系。

          then
            begin
              List
              List
              List.post;
              showmessage ('Message')
            end
          else close;

这值得一提:else在风格指南中,后面的声明被明确指出已经失宠,但仍然可以。

      end;
end;

值得一提的是,您使用两个空格进行缩进正是样式指南所说的使用。

于 2014-02-21T18:15:41.700 回答
0

这是我将如何格式化该代码:

begin
  if Password <> Database['Database'] then
    showmessage ('Message')
  else
  if NewPassword <> Retype then
    showmessage ('Message')
  else
  begin
    if Message (yes, No, etc) =yes then
    begin
      List
      List
      List.post;
      showmessage ('Message')
    end
    else
      close;
  end;
end;

主要区别在于:

  • 保持then在同一行的末尾if(除非有多个条件)。
  • else使陈述与if陈述保持一致
于 2014-02-21T17:27:39.933 回答