【问题标题】:Unsure tabbing with Pascal [closed]不确定是否使用 Pascal [关闭]
【发布时间】:2014-02-21 17:05:16
【问题描述】:

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

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;

【问题讨论】:

    标签: delphi pascal tabbing


    【解决方案1】:

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

    我会做的稍有不同。 IMO,这清楚地向新程序员展示了 if..elsebegin..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 移动到同一行(它减少了一级缩进,对我来说使代码流更加清晰。我们有三个可能的选项,并且清楚地显示了三个选项(@ 987654326@,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 语句:

    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 循环:

    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;
    

    【讨论】:

    • 谢谢!是的,我是一个初学者,但在这里得到一些帮助后,我想它很容易理解它:-) 我希望如果我在标签上再发布一个小问题也没关系。
    【解决方案2】:

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

    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 语句保持一致

    【讨论】:

    • 谢谢,我正在尝试关注一本书,但它的标签非常不同,所以我不确定。我想你不能再看两个我不确定的小部分吗?
    【解决方案3】:

    就我个人而言,我是一个 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 写入大量嵌套语句,这反过来只会损害可读性而不是提高可读性。

    【讨论】:

      【解决方案4】:

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

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

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

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

          else
        if NewPassword <> Retype
      

      第二个if 语句是第一个if 语句的else 分支的一部分,并且应该相应地缩进,可能将它放在与else 相同的行。

          then showmessage ('Message')
          else
            begin
      

      beginend 不应该有额外的缩进:子语句应该是上面else 右侧的两个空格。

              if Message (yes, No, etc) =yes
      

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

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

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

            end;
      end;
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-26
        • 1970-01-01
        • 2014-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        相关资源
        最近更新 更多