Jump to content
dormky

How can I get this code formatting ?

Recommended Posts

I'd like to have my code formatted like this. I've seen it in multiple places on the internet but can't figure out how to get the right parameters.

 

if condition then
begin
  code;
end
else begin
  more code;
end;

if condition then
begin
  code;
end
else if otherCondition then
begin
  more code;
end;

 

I've tried using GE and the default RAD Studio formatter.

Edited by dormky

Share this post


Link to post

You may have hard time to find something like that, because IMHO your formatting is not consistent: After then the begin is in the next line, but after else it is not.

 

It probably boils down to having to write your own formatter.

  • Like 1

Share this post


Link to post

Hi...:classic_cool:

Quote

I've seen it in multiple places on the internet

...sometimes the internet is not correct. :classic_tongue: The styleguide from Emba is correct! https://docwiki.embarcadero.com/RADStudio/Sydney/en/Delphi’s_Object_Pascal_Style_Guide

Quote

your formatting is not consistent

+1

 

better:

(indent)

if condition then
begin
  code;
end
else 
  if otherCondition then
  begin
    more code;
  end;
  
//or:

if condition then
begin
  code;
end
else 
begin
  if otherCondition then
  begin
    more code;
  end;
end;

 

Edited by haentschman

Share this post


Link to post

I agree with the fact that it's not 100% consistent. That's because having

end
else
begin

Is not that useful. 3 keywords each on one line is really wasteful.

Share this post


Link to post

@haentschmanThe style guide mentions :

// correct
begin
  for var I: Integer := 1 to 10 do

But I can't declare a var like this. What's the compiler setting to allow that ?

Share this post


Link to post

I don't know if that's the intention, but it can't be like that.

  var I: Integer;
  I := 10
or
  var I := 10;

 

Share this post


Link to post
45 minutes ago, dormky said:

3 keywords each on one line is really wasteful.

What is it wasting? Seriously.

Share this post


Link to post

image.thumb.png.a86f7a047e7ad48082bfe0f42d25a0ef.png

 

It was is Rio too, but i remember that were some issuse about that. First the errorinsight was not working, but the compilation were OK. Other issues ,,, may be

 

I think that no one used in that version the inline variables.

Bye

Edited by DelphiUdIT

Share this post


Link to post

I prefer my nested if/then/else code in this block format as this gives me the least ripples=visual noise, and I have done the same with for/loops, since the days of old (Delphi 6 and earlier).

 

procedure
begin
  if condition then begin
    ...
    ... 
  end else begin
    ...
    ... 
  end;
end;

procedure
begin
  if condition then begin
    ...
    ... 
  end else if condition then begin
    ...
    ... 
  end;
end;

procedure
var r,c: integer; 
begin
  for r:=1 to 10 do begin
    ... 
    ... 
    for c:=1 to 100 do begin
    ... 
    ... 
    end;
    ...
    ...
  end;
end;

 

Share this post


Link to post

How about reducing the number of ifs needed. Or Less ifs = less need for else ifs ending with an else 😞! The need for formatting to get "readable" code is reduced.  Debugging is readily done if or when needed. 

I used on jobs in XL on a Mac in the eighties and consider the flyover case copyrighted in the Excel realm.

     

  • Set each Boolean state first with assignments then use these "Uwe's" or Explainer variables in the if assignments.
  • Assignments with a Nil state implementing default short circuit are more visible. 
  • Example case statement repeatedly steps though an enumerated Type setting states in the order of the Enum.
     
 /// <summary> Beep when A and B are true</summary>
 /// <remarks>
 ///  Or allows R to retain true result of each lines test;
 ///  Set R to False start of logic and apply the braces as and/or if needed in the assignments!
 /// </remarks>
procedure latchingLogic(A,B: Boolean);
var
  R: Boolean;
begin 

  R := False;
  R := R or A and B;
  R := R or A and not B;
  R := R or not A and B;
  R := R or not A and not B;

  if R then   // One if and zero elses = readable +++
    Beep; //needs expanded to beep(freq, MS) IDE compiles anyway
end;


Type            // <-- more true precedent inversion to allow fitting to OP example
  TabTrueFalse = (abTrueTrue, abTrueFalse,abFalseTrue, abFalseFalse);

var
  A: Boolean = False;  //example only these become class variables plus typed constants in 11.3
  B: Boolean = True;


procedure SetAThenBtoTrue(var A, B: Boolean);
var
  I: Integer;
  abTest: TabTrueFalse;
begin
  repeat // could be reentrant
    I := ord(not A) * 2 + ord(not B); // not allows present enumtype order
    abTest := TabTrueFalse(I);

    case abTest of
      // abTrueTrue: 'yaa';
      abTrueFalse:
        B := True;  // since A is True Ok to set B to True
      abFalseTrue:
        B := False; // We want A True first
      abFalseFalse:
        A := True;  // set A true first
    end;

  until abTest = abTrueTrue;
  latchingLogic(A,B);  // use for UI output
end;
Edited by Pat Foley
Used in Excel sheets

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

×