Jump to content
uligerhardt

"Always break line between else and if" vs. comments

Recommended Posts

I have disabled the "Always break line between else and if" option, and I'm happy with the results.

 

However my colleague insists on having nested if-elses without begin/end pairs and on top mixes this with comments. A simplified example:

procedure MyProcedure(a, b: Boolean);
begin
  if a then
    if b then
      Beep
    else
      Beep
  else
    // Kommentar
    if b then
      Beep
    else
      Beep;
end;


This gets formatted to

procedure MyProcedure(a, b: Boolean);
begin
  if a then
    if b then
      Beep
    else
      Beep
  else
    {// Kommentar} if b then
      Beep
    else
      Beep;
end;

Is there an option to keep the lines separate under these circumstances? Do you deem the current behavior desirable?

Share this post


Link to post

/off-topic Preferences of code.  I like begin/end for clarity.
However, I also like unusual breaks before then and strange indentations of single statements.  If only I could convince the formatter to do my bidding.

procedure MyProcedure(a, b: Boolean);
begin
  if a
  then begin
    if b 
     then Beep
      else Beep
  end  
  else begin
    if b // Kommentar
     then Beep
      else Beep;
  end;
end;

 

Share this post


Link to post
1 hour ago, uligerhardt said:

I have disabled the "Always break line between else and if" option, and I'm happy with the results.

 

However my colleague insists on having nested if-elses without begin/end pairs and on top mixes this with comments. A simplified example:


procedure MyProcedure(a, b: Boolean);
begin
  if a then
    if b then
      Beep
    else
      Beep
  else
    // Kommentar
    if b then
      Beep
    else
      Beep;
end;


This gets formatted to


procedure MyProcedure(a, b: Boolean);
begin
  if a then
    if b then
      Beep
    else
      Beep
  else
    {// Kommentar} if b then
      Beep
    else
      Beep;
end;

Is there an option to keep the lines separate under these circumstances? Do you deem the current behavior desirable?

There is no option (as far as I know, I might have missed it). And I remember seeing a comment in the original formatter sources from Egbert van Nes that he implemented this behaviour because he couldn't get the formatter handle these comments in any useful manner without breaking the source code.

 

If anybody really wants to use that kind of comments: I am accepting patches.

Share this post


Link to post
5 minutes ago, Lars Fosdal said:

/off-topic Preferences of code.  I like begin/end for clarity.
However, I also like unusual breaks before then and strange indentations of single statements.  If only I could convince the formatter to do my bidding.


procedure MyProcedure(a, b: Boolean);
begin
  if a
  then begin
    if b 
     then Beep
      else Beep
  end  
  else begin
    if b // Kommentar
     then Beep
      else Beep;
  end;
end;

 

If you really want that kind of formatting: I am accepting patches.

Share this post


Link to post
47 minutes ago, baka0815 said:

I think this could be the same topic as

 

No. It's something totally unrelated.

Share this post


Link to post
5 minutes ago, dummzeuch said:

If you really want that kind of formatting: I am accepting patches.

Well, I am not that consequent, so I'll make do, doing it manually.

 

It did take me 30+ years to develop this style for readability, though.

One of the benefits of breaking before then, is that it becomes easy to comment out parts of the if condition.

One of the benefits of the hanging indent on single line statements, is that it is easy to spot where the full statement ends.

 

Readability is cruical. Maintainability improves with readability.

Share this post


Link to post
23 minutes ago, Lars Fosdal said:

/off-topic Preferences of code.  I like begin/end for clarity.
However, I also like unusual breaks before then and strange indentations of single statements.  If only I could convince the formatter to do my bidding.


procedure MyProcedure(a, b: Boolean);
begin
  if a
  then begin
    if b 
     then Beep
      else Beep
  end  
  else begin
    if b // Kommentar
     then Beep
      else Beep;
  end;
end;

 

I'd just use begin/end here too, but that's difficult because of my charcter counting colleague. 😉

Share this post


Link to post
21 minutes ago, dummzeuch said:

There is no option (as far as I know, I might have missed it). And I remember seeing a comment in the original formatter sources from Egbert van Nes that he implemented this behaviour because he couldn't get the formatter handle these comments in any useful manner without breaking the source code.

 

If anybody really wants to use that kind of comments: I am accepting patches.

OK then, I'll see if I can give it a try.

Share this post


Link to post

Do I remember correctly that the standalone formatter doesn't use the expert DLL anymore? That should make debugging much easier.

Share this post


Link to post
1 hour ago, Lars Fosdal said:

/off-topic Preferences of code.  I like begin/end for clarity.
However, I also like unusual breaks before then and strange indentations of single statements.  If only I could convince the formatter to do my bidding.


procedure MyProcedure(a, b: Boolean);
begin
  if a
  then begin
    if b 
     then Beep
      else Beep
  end  
  else begin
    if b // Kommentar
     then Beep
      else Beep;
  end;
end;

 

I also like begin/end for readability, but that code style gives me the creeps.

 

I would format it the following way (or even use additional begins and ends for the "Beeps").

procedure MyProcedure(a, b: Boolean);
begin
  if a then
  begin
    if b then 
      Beep
    else
      Beep;
  end  
  else
  begin
    // Kommentar
    if b then
      Beep
    else
      Beep;
  end;
end;

 

Share this post


Link to post
1 hour ago, Lars Fosdal said:

/off-topic Preferences of code.  I like begin/end for clarity.
However, I also like unusual breaks before then and strange indentations of single statements.  If only I could convince the formatter to do my bidding.


procedure MyProcedure(a, b: Boolean);
begin
  if a
  then begin
    if b 
     then Beep
      else Beep
  end  
  else begin
    if b // Kommentar
     then Beep
      else Beep;
  end;
end;

 

Personally I think it's much clearer like this:

 

procedure MyProcedure(a, b: Boolean);
  begin
     if a
     then 
     begin
        if b 
        then 
            Beep
        else 
            Beep
     end else     begin
        if b 
        // Kommentar
        then 
           Beep
        else 
           Beep;
     end;
  end;

 

  • Haha 1

Share this post


Link to post

Quite seriously, IMHO, this is something that you have to figure as you go, from case to case that is. Som variable names/expressions are SuperLoooooong.Chains.Of() and some are shrt. That is definitely IMHO something that should be factored in - so just look at the code (preferrably some months after you wrote it - maybe not so manageable, but anyho) and use the style that appears most clear.

  • Like 1

Share this post


Link to post
16 hours ago, uligerhardt said:

Do I remember correctly that the standalone formatter doesn't use the expert DLL anymore? That should make debugging much easier.

No, that's not the case. But the Unit Tests don't use the DLL, so you can create test cases for the unit tests and then implement the code directly using that program.

  • Like 1
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×