Recent

Author Topic: If, Then, Else Statements?  (Read 11848 times)

wp

  • Hero Member
  • *****
  • Posts: 13269
Re: If, Then, Else Statements?
« Reply #15 on: December 27, 2017, 06:06:37 pm »
This is perfect. There are no style guides on this, it's a matter of personal preference. I am mixing both approaches myself: sometimes OKButton, sometimes btnOK. The latter has the advantage that it is easier to group controls by type.

The other most important tip for coding is: Use proper indentation. This way it is much easier for you to see the code blocks. Therefore, I'd recommend to put the final end (which corresponds to the leading "begin") at the same indentation level as the begin

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2. begin
  3.    If NameEdit.Text = ''
  4.      Then OKButton.Enabled := False
  5.      Else OKButton.Enabled := True;
  6. End;

or better

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit2Change(Sender: TObject);
  2. begin
  3.   If NameEdit.Text = '' then
  4.     OKButton.Enabled := False
  5.   else
  6.     OKButton.Enabled := True;
  7. end;

P.S.
Bart was faster...
« Last Edit: December 27, 2017, 06:08:21 pm by wp »

A

  • Guest
Re: If, Then, Else Statements?
« Reply #16 on: December 27, 2017, 06:16:01 pm »
Thanks Bart :)

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: If, Then, Else Statements?
« Reply #17 on: December 28, 2017, 03:49:29 am »
To A: You might need to read these too…

- Object Pascal (Coding) Style Guide (Yes, it's from Delphi, but it should apply to all Object Pascal dialects as well, like Free Pascal.)
- Modern Object Pascal Introduction (Some topics are a bit advance but most of them are very useful informations.)

However, like in other languages, the coding style isn't mandatory. I personally don't follow them all. For example, instead of PascalCase I prefer camelCase. So, I prefer to use getValue() instead of GetValue(). As for component naming, I also prefer to use btnSave (common abbreviation) instead of ButtonSave (full name). But that's me, you may use whichever style you prefer.  :)

So, my version would be like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.edNameChange(Sender: TObject);
  2. begin
  3.   if edName.Text = '' then
  4.     btnOK.Enabled := false
  5.   else
  6.     btnOK.Enabled := true;
  7. end;
  8.  

or the shorter one…

Code: Pascal  [Select][+][-]
  1. procedure TForm1.edNameChange(Sender: TObject);
  2. begin
  3.   btnOK.Enabled := (edName.Text <> '');
  4. end;
  5.  
« Last Edit: December 28, 2017, 04:11:09 am by bee »
-Bee-

A long time pascal lover.

 

TinyPortal © 2005-2018