dormky 2 Posted October 10, 2023 (edited) 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 October 10, 2023 by dormky Share this post Link to post
Uwe Raabe 2057 Posted October 10, 2023 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. 1 Share this post Link to post
haentschman 92 Posted October 10, 2023 (edited) Hi... Quote I've seen it in multiple places on the internet ...sometimes the internet is not correct. 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 October 10, 2023 by haentschman Share this post Link to post
dormky 2 Posted October 10, 2023 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
dormky 2 Posted October 10, 2023 @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
Lajos Juhász 293 Posted October 10, 2023 1 minute ago, dormky said: But I can't declare a var like this. What's the compiler setting to allow that ? Delphi version this requires Delphi 10.3 or newer version (https://docwiki.embarcadero.com/RADStudio/Sydney/en/Inline_Variable_Declaration) Share this post Link to post
dormky 2 Posted October 10, 2023 (edited) @Lajos Juhász I am on Delphi Rio. The error is "Expected end but received var". Edited October 10, 2023 by dormky Share this post Link to post
Stano 143 Posted October 10, 2023 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
Sherlock 663 Posted October 10, 2023 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
Uwe Raabe 2057 Posted October 10, 2023 1 hour ago, Lajos Juhász said: Delphi version this requires Delphi 10.3 or newer version (https://docwiki.embarcadero.com/RADStudio/Sydney/en/Inline_Variable_Declaration) Sydney is 10.4! Share this post Link to post
DelphiUdIT 176 Posted October 10, 2023 (edited) 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 October 10, 2023 by DelphiUdIT Share this post Link to post
JohnLM 14 Posted October 10, 2023 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
Pat Foley 51 Posted October 10, 2023 (edited) 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 October 10, 2023 by Pat Foley Used in Excel sheets Share this post Link to post