Solved

Organize my code

Posted on 2002-04-12
11
351 Views
Last Modified: 2010-08-05
Hello All,

My question is kind of weird. I would like to have information on how to organize my code in a Delphi project.

I've been progamming in delphi since 5 years and I always find my code very difficult to clarify. So, I would like to have hint and tricks on how I can organize functions, procedures, properties in my code so other programmers (even me) would have no problem debugging my app.

Is there a kind of work around that I could use to document my code ?

All the application I've ever wrote were not really complex (most of all were "oneshot" application). But now, I've been working on couple of big project and my boss ask me to document my code to help me (or an other programmer) support it in future.

Can anyone help me on that ??
Thanks in advance.
Oteau
0
Comment
Question by:oteau
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 6936738
Yep this is a big problem in the coding industry. Making code reusable. There are manny way's to structorize your code but in general you should name variableles with propper names, comment things to their function

Bad example

for i:=1 to 100 do ; //Loop 100 times.

If you have funciotns /procedures 0ver 27 lines you should consider cutting them down (refractoring)

button1
{
 ...
  lots of code
  x := x +1;
 ...
}

button2
{
 ...
  lots of code
  x:=x+2;
 ...

}

button3
{
 ...
  lots of code
  x:=x+3;
 ...
}

consider making a procedure

procedure do_things(i:integer);
{
 ...
  lots of code
  x:=x:1;
 ...

}

also do pre en post

procedure do_things(i:integer);
(* pre: i has to be greater than 1  (or i>1)
   post: all cards for user 1 are deleted
*)
{
 ...
  lots of code
  x:=x:1;
 ...

}

remember these are just examples...!!!

Diagrams are most helpfull..

look in to these and find out what you like best..

- state diagrams
- uml
- psd
- dfd (dfd level > 3)

there are some others...

also putting classes in diffrent units helps..

TMyClass1 in unit myclass1

these are just some examples...

don't worry to throw away code to rewrite it. (This is hard i know)
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6936741
the use of a repository is verry recomended.!
0
 

Expert Comment

by:beno
ID: 6936805
You could code more like Borland suggests: http://www.econos.de/delphi/cs.html

Here is a page with some links on how to write readable and understandable code:
http://delphi.about.com/cs/standards/index.htm

DelForEx is a very nice freeware tool to format your existing source code to match Borland standards.

0
 

Author Comment

by:oteau
ID: 6936844
Hi beno,

Where can I find DelForEx ???

This could really help me.
Thanks
Oteau
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6936878
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:martin_g
ID: 6937045
A good book that has helped me is:
"Code Complete - A Handbook of Software Construction" by Steve McConnell - Microsoft Press.  Very easy to follow and even uses Pascal/Delphi examples!
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6937141
I would not start by using DelForEx. The tool itself is excellent, but you should better learn to write your code cleanly formatted.
The problem is your state of mind and a coding tool is only of limited use.
You should read the Borland coding standard and try to understand why it chooses the specific style convention.
0
 

Author Comment

by:oteau
ID: 6941826
You are right robert. DelForEx is wonderfull to realign all the code, change uppercase and lowercase on reserved words. My first problem is how can I organize the logicial sequence of my code. Example :

In a form, I have 10 procedures, 5 functions, and a lot of code for button, menus, etc. Is it better for me to create an other unit to store my procedure and functions and to keep the code about the form in his own unit ? How can I separate the code for the form. I have 10 OnClick event for menu, 5 buttons and code assign for event on the form. Is there a "magical" way to organize all this to preserve a logical sequence for me and other programmer when we try to modify something ?

Thanks again for your help
Oteau
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 200 total points
ID: 6942012
I do it this way:
All methods of a form are in the form unit.
The implementation in the same order as the declaration in the class.
Controls in the class sorted along the tab order.
Events in the class sorted like the controls.
Helper functions directly after "implementation".
Helper functions which are useful for several units in a separate unit.
If localisation is an issue then all language dependent 'literal' strings as constants in a separate unit.
Also all strings which are used several times in the unit as local constants.
0
 
LVL 1

Expert Comment

by:martin_g
ID: 6942027
I think you maky have answered your own question regarding separating code from form events (i.e OnClick). Separating functions from the form events will make more re-usable.  
As to how to organize the functions, there are several ways depending on your needs.  The simplest would be to group your functions alphabetically in the declaration section.  Be sure to use the same grouping in the implementation.  Comment your code profusely; explain what each procedure does and when/who last modified it and why.  There are several helps for code organization out there.  I like GE Experts.
0
 

Author Comment

by:oteau
ID: 6960041
Thank you all for your help.

It will be very usefull for me and other programmer.

Thank you very much.
Oteau
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 88
Path  to current project in Delphi. 2 71
Intraweb download file link ? 1 105
Show Listview image from database (String field) 5 112
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now