Recent

Author Topic: Lenghthy Procedures  (Read 3533 times)

guest48180

  • Guest
Lenghthy Procedures
« on: November 27, 2013, 01:51:09 am »
Before I start whacking away at Unit1, is it possible to take my long procedures and turn them into usable units that Unit1 can use? Like SysUtils and so on that are in the USES portion of Unit1. Just wondering because I have over a thousand lines of code for this one program, and sorting through it is becoming a difficult tangle to work with.

Any help on this will be appreciated.

Landslyde

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Lenghthy Procedures
« Reply #1 on: November 27, 2013, 02:55:51 am »
Quote
...is it possible to take my long procedures and turn them into usable units...

Yes, sure! You should do that, and also you should turn your long procedures into more but shorter procedures.




guest48180

  • Guest
Re: Lenghthy Procedures
« Reply #2 on: November 27, 2013, 03:29:30 am »
Quote
Yes, sure! You should do that, and also you should turn your long procedures into more but shorter procedures.

I took a long procedure called 'DisplayData' and put it into a new unit called unit1. In my main unit I put that unit shld be used in the USES section, but it's giving me several errors:

identifier not found "TForm1"
class identifier expected
identifier not found "sg1"

etc.

How do I make this new unit and the one procedure in it available to the main unit?

Landslyde


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Lenghthy Procedures
« Reply #3 on: November 27, 2013, 03:58:06 am »
If DisplayData is a member of TForm1, then keep it. and take what's inside it to another unit. For instance, take FillGrid to another unit and make enough modifications to make it work.

Right now FillGrid is declared as:

Code: [Select]
procedure FillGrid(aRowIndex: integer);

If you move it to another unit, you should provide sg1, sg2, and CustRec as parameters:

Code: [Select]
procedure FillGrid(aRowIndex: integer; sg1, sg2: TStringGrid; CustRec: TCustRec);
I don't know the actual type for CustRec, I just assumed TCustRec, you should correct that.
« Last Edit: November 27, 2013, 04:22:14 am by engkin »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Lenghthy Procedures
« Reply #4 on: November 27, 2013, 04:17:30 am »
As a side note. To make it easier for you (and others), use better names for your procedures and controls. I would change some names like this:

sg1 :
After reading the code (It should give some idea BEFORE reading the code), I realized that sg1 is dealing with customer details, so maybe a name like CustomerDetailGrid (or at least CustGrid, if I don't like long names).

sg2 :
After reading the code, OrderGrid.

FillGrid :
Fill...Grids, and it should call other procedures like FillCustGrid and FillOrderGrid
« Last Edit: November 27, 2013, 04:23:04 am by engkin »

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Lenghthy Procedures
« Reply #5 on: November 27, 2013, 05:36:52 am »
A rule of thumb that has been used compares files to a page in a book.  A paragraph (2 or 3) generally describe a specific train of thought.  There is white space on the page to improve readability, too.

FORTRAN programmers were taught to make their units 50 lines long. 

Personally?  I make mine units several pages long.  Especially my custom library file...it seems to grow.  (I did break it into two files.)

When you start to add different functionality (or it gets convoluted and difficult to remember/manage), put it in a new unit.  At the top of my main (MyUnit1) file, I list ALL units and a brief description of what they do.  I list them in the order they appear in the flow of the program.

Hope some of this helps.  It WILL get easier!

Have fun!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018