Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

units creation 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
My code is starting to get to heavy and bulky. I would like to create units to house some function and procedures to make the main form lighter. Is there some tutorial out there that could show me on how to do that properly or do I just create a new unit and reference my main form inside it?
thanks.
P
 
Take any functions and procedures that aren't directly tied to the user interface and move them to a new unit. Where possible pass the function all of the parameters that it needs to do it's job. Try to avoid global variables. Then you can call those functions from your main form and pass along the parameters.

That allows you to get used to separating your program logic from your UI. This is also where the refactoring tools become handy.
 
Thanks.let me google what refactoring is. I never used that although I saw the name.
 
There are probably more elegant definitions for refactoring. What it means for you is that you can have the IDE help you modify your source code to avoid re-keying or copying/pasting.

A simple example allows you to rename a variable within your application without performing a search/replace.

I've always been tempted to purchase this tool. This will give you some visual examples of what refactoring tools and do for you:

 
In effect, this is a question that I've been trying to come up with a good answer for myself. It's easy to say "don't use global variables" and "move things to another unit" and it's good if it's not code that is interrelated to your form, but if you end up with 1000-2000 lines of nothing but form methods and so on, what is the "accepted" approach to break this up into discrete functions, especially without going to a lot of trouble patching code in the form object, especially since it seems to expect method definitions in the same unit as the class definition?

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I always apply the same rule:

if the project is "big" enough I separate the business and presentation layer. In general, I see a program that has more than 5 forms as "big" enough...

Glenn, think about unit tests...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I'm still careful how often I divide units, even though it does help you keep organized. I have one particular unit called 'JDCtrls.pas' which contains many completely different components I've custom built. These components I consider done and ready and don't require too much more work. It's similar to the 'StdCtrls' and 'ExtCtrls' units, only with my own stuff. But it's only visual controls. Non-visual components I keep elsewhere. Larger projects I still keep separate, such as my ginormous needle gauge component, I still keep it in its own unit, even though it's done enough to be kept in this shared unit. It's separate because it's very, very large, and if I want to use just one small control which is in my JDCtrls unit, I don't want this large component to be compiled with the project as well. Think about the size of the file after you've compiled it.

JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top