Topic: Advice on cleaning up code

Hi All,
.
I could use some advice/opinions please.  I have a form with an On-Show event that has a bunch of script I've written to address issues when I open and process the form.  Over the past few months I have been adding a number of If statements to the script.  These do all kinds of things that I felt I needed.  On the plus side, the code works as written.  On the negative side, in my opinion, it's messy and hard to work with.  Even though I wrote it, with the excellent help from folks on the forum, I'm finding it's hard to understand, especially when I want to add or change some functionality.  Please see the attached code.
.
My thought is maybe I could break it up into sup-procedures that I could call from within the script.  I found this code from Dmitry that seems to suggest what I think I could do.  However I can't seem to get it to work.  And I'm not sure if this is even the best way to clean up my messy code?
// Call a procedure from within a prodedure
procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    procedure1;
end;

procedure procedure1;
begin
  procedure2;
end;

procedure procedure2;
begin
  ShowMessage('Hello from proc 2');
end;

.
Maybe I shouldn't be using so many If statements?
.
Right now, as I mentioned, the code is working so I'm hesitant to mess with it.  But I would like to make it more user friendly (for me) if possible.
.
Thanks for your help.
Frank

Post's attachments

Attachment icon If Statements.pdf 54.2 kb, 206 downloads since 2021-11-22 

2 (edited by joshuA 2021-11-23 01:27:54)

Re: Advice on cleaning up code

Hi papafrankc,
.
I wish I could offer you some useful help here.  But I run into this same thing all the time (it seems).  I'm always tempted to write sub-procedures for repeated code, but then I start redesigning my forms to help compensate instead.
.
I can't see your whole project, but for me I try to separate forms (which derek has suggested, in some of my cases) instead of trying to capture all kinds of variations.  Again, this may not apply for you though.
.
Commenting is your best friend, which you are doing, so I think you have the bases covered.  I start commenting almost every line when it starts getting too cluttered.  Because when I'm going back (months, or years) later it's easier for me to read my notes instead of converting what the code is doing in my head.  lol
.
I will be curious if others have better suggestions for you with this too.

"Energy and persistence conquer all things."

Re: Advice on cleaning up code

JoshuA,
.
Thanks for your thoughts.  Since I started adding more comments, the code is a little easier to read.  But I still think it's a mess.
.
When you mentioned Derek's suggestion to separate the forms instead of trying to program in a bunch of variations, I think that makes sense.
.
My many IF statements, with some nested inside each other, are just too confusing to keep straight.  I'm going to look into  more forms and less IF statements to see if I can improve things.  It'll take some re-design, but probably not any more time than I'm spending now to figure things out.
.
Thanks
Frank

Re: Advice on cleaning up code

no matter how hard you try, the code will still be confusing, because you need to comment a lot ..

Re: Advice on cleaning up code

Your source code is total messy and a perfect example who not to write source code.


If you would give me this quality of source code your would be fired.


- To many unneeded comments.
- No termination/exit point if action has been closed
- depended if then clauses where it is not needed
- unneeded repeats of if then


This is slow, full of possible malfunctions, crappy and against all common rules for writing source codes in Delphi or C++.


Simple rule: If you can not read it without comments, it is shit.
Read about "Clean Code" or read the MISRA Rules.


There are only two reasons for comments in a source code
- procedure/function header to inform about input and output parameters and there possible values
- a Line of //----------------------------  for optical separation of logical function parts in a procedure/function.


My Version:

pocedure frmEquip_OnShow (Sender: TObject; Action: string);
 
begin 
  if action = 'NewRecord' then 
  begin 
    frmEquip.EqSvcOverDue.Visible := false ; 
    frmEquip.GrpSvcBox.Visible := false ; 
  end;

  //---------------------------------------------------------
 
  if frmEquip.cboSpare.Text = 'Installed' then 
    frmEquip.GrpSvcBox.Visible := true 
  else 
    frmEquip.GrpSvcBox.Visible := false ;

  //---------------------------------------------------------

  if action = 'ShowRecord' then 
  begin 
    frmEquip.EqSvcOverDue.Visible := false ; 
    // showmessage('Equip OnShow, ShowRecord, EqSvcOverDue.Visible = false') ; // test 

    if frmEquip.cboSpare.Text <> 'Spare' then 
    begin 
      // showmessage('ShowRecord is NOT a SPARE') ; 
      // CHECK TO MAKE SURE THE DATE IS NOT BLANK 
  
     if frmEquip.NextSvcDate.Text <> '' then 
      begin 
        // showmessage('Equip OnShow, ShowRecord, NextSvDate not BLANK') ; 
        // CHECK FOR OVERDUE DATE 
      
        if StrToDate(frmEquip.NextSvcDate.Text) < Date then 
        begin 
          frmEquip.EqSvcOverDue.Visible := true ;
          frmEquip.EqSvcOverDue.Text := 'Overdue' ; 
          // showmessage('Overdue should be SET') ; 
          TableID := frmEquip.btnSave.dbGeneralTableId; 
        end; 
      
      end;

  //---------------------------------------------------------

      if frmEquip.cboSpare.Text = 'Installed' then 
      begin 
        frmEquip.btnPrintInstalled.Visible := true; 
        frmEquip.btnPrintSpare.Visible := false; 
      end; 
    
    end;

  //---------------------------------------------------------

    if frmEquip.cboSpare.Text = 'Spare' then 
    begin 
      frmEquip.btnPrintInstalled.Visible := false; 
      frmEquip.btnPrintSpare.Visible := true; 
      frmEquip.GrpSvcBox.Visible := false ; 
    end; 
  end;

  //---------------------------------------------------------

  TableID := frmEquip.btnSave.dbGeneralTableId; 
  if action = 'NewRecord' then 
    vNew := 'y' ; 
    
end;

Re: Advice on cleaning up code

Hi Teco049,
Please bear in mind that not everyone has either a programming background or actually wants to become a programmer.
Also remember that we ALL had to start somewhere;  I know my first attempts at writing some code were pretty awful and would embarrass me now. 
Your version of Frank's script is certainly a lot more readable, so thank you for that and I'm sure we can all learn from it.
Derek.

Re: Advice on cleaning up code

Hi Frank, Joshua, Sibprogsistem, Teco,
A couple of general observations to take on board or ignore - LOL!
1.  Comments
I didn't find it too easy to distinguish your comments from your code and I think this might add to the confusion.
Perhaps you could put your comments on discrete lines (or even double-line spaced).  Typing them in uppercase can also help to make them stand out.
Teco049's suggestion is a good example of how you can make them much easier to differentiate from the code itself.
2.  Consistent indentation
It makes your code a lot more readable if you consistently indent.
I notice that you indent (some of the time) by 1 and, for me, it doesn't make it stand out enough (but it's just my preference).
I personally indent by 2 spaces but to make it stand out more, try 3 or 4 even.
3.  Line Separators
Perhaps use Line separators to put 'if statements', sub clauses etc into discrete visual 'blocks' that make it much it much easier to  distinguish different segments of code..
4.  Scratchpad (for want of a different term!).
It's possible to rather 'overdo' the comments (and end up with more comment that code!) so, if I'm needing to be more descriptive, I use the space below the final begin.... end. at the bottom of the script as a 'scratchpad'.  Any long explanations etc as to what I'm doing and why get put in there so I can refer back to them at some later stage to refresh my memory!

Derek.

Re: Advice on cleaning up code

Hi everyone,
.
Thanks so much for all your comments/suggestions.  This is exactly what I was looking for.  Different perspectives.
.
Comments
The replies overall seem to suggest to me that well placed comments are preferred over minimal comments.  I'm afraid I don't agree with Teco about not needing hardly any comments.  For someone with a photographic memory, they may be able to remember everything they created six months ago.  But unfortunately that's not me.  I do like Teco's idea about a separate comment line // --------------  It does look like it will make it easier to differentiate between different groups of code.
.
When I stuck some comments at the end of some of the statements I see now that it didn't really help me much.
.
Back in another lifetime I used to program in dBase II (before windows).  Crazy, but there were computers before windows LOL.
dBase used to have a DO command which you would use to call a procedure or code module.  I always thought that made a lot of sense.  It was logical and you didn't have to go through a bunch of code that wasn't relative to the current task. 
.
I was able to figure out the earlier suggestion by Dmitry, to call a procedure from within another procedure.  I'm going to play around with that & see if it might make sense for me.  It might not help, but it doesn't hurt to check out new approaches.
.
Over the last couple of years I have been able to progress to the level I'm at now, because of the excellent help/advice from the folks on the forum, along with my trial and error approach.
.
Thank you all for your feedback.  Please keep the suggestions coming - positive or negative smile
Frank

Re: Advice on cleaning up code

Well, I'm reconsidering my suggestion for commenting now neutral
.
I've been enrolled in a few programming classes over the past year or so.  And I realize these are entry level courses, but they emphasize heavy commenting on our code.  We get penalized otherwise.
.
I guess there is a fine line here because Teco would fire me lol
.
I appreciate the feedback and also for the references.  And thanks to derek for being sypathetic on us wink

"Energy and persistence conquer all things."

Re: Advice on cleaning up code

Hi Joshua,
If commenting your code works for you, then go with it;  it's one of those things where I don't think there is a right or wrong.
For me, the important thing is that it clearly stands out from the code itself so that it clarifies rather than confuses;  some code editors highlight comments in different colours etc which is really nice.
Derek.