Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 1 of 1 total
Thread Free advice and hints to new developers
Sat, Feb 8 2020 4:56 PMPermanent Link

erickengelke

Avatar

Hi,

Sometimes I see projects written by people and there can be a lot of code.  That's actually a bad sign, you want to use the language and features to do the work for you, and do it in as little code as possible.  More code generally means more bugs.

So here are some helpful tips.

1. Your functions should be short and sweet.  If there is more than a screenful of code in a function, you cannot read it, and you probably won't know what all it does because you cannot follow it.  Break up your code into subroutines that do simple things.

2. Never repeat code.  If you are doing the same thing twice or more times, write a single subroutine that does the action and call it as often as you need it instead.   This shrinks your code and makes it more manageable.

3. Use inheritance to solve general purpose problems.  Suppose you write an OnChange event handler for Edit1  and you want to do the same thing for Edit2 and Edit3.  Just call the same event handler, but use the Sender property to tell you which TEdit you are being called for.  

4. Use the Tag property to hold extra bits of information.  It can hold a 52 bit number, you can use it as an index into a list, or as a flag, or a bitfield of many flags.    Use this with inheritance tip (3).  

Here's an example, suppose you want to only enable the save button if the user updated any fields on a complex page and might need to save.  And suppose you want to make the changed fields more visible.  Write a single event handler which does both and set your TEdit's OnChange handler to that.


TForm1 GeneralEditOnChange( sender : TObject );
var
 ed : TEdit;
begin
 ed := TEdit( sender );
 ed.Font.Color := clRed;    // we set the color of the text to Red
 btSave.Enabled := True;  // enable the save Button
end;
  
Erick
EWB Programming Books and Component Library
http://www.erickengelke.com
Image