Recent

Author Topic: how should i write code project?  (Read 3156 times)

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
how should i write code project?
« on: September 22, 2017, 01:22:34 pm »
Hi

my field is not IT or Software and i like software.

i write a project.

after 6 months my Main unit has 6237 lines and an others unit has not  more  than 1000 lines(i have 3 important uints and in my project)?!?!

almost all my codes are in Main uint.

i think after 6 months , mu project has more than 15000 lines ?!?!?

how should i write project?

should  have i many units in project ??

or  i should splitte my code project to many units??

i thinke , it is not true that (almost)all my code are in one (main)unit ?

waht is best way to write code project?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: how should i write code project?
« Reply #1 on: September 22, 2017, 02:47:24 pm »
Good questions to ask would include:
  • What is/are the core functionality/ies of the project?
  • If it is not possible to encapsulate that functionality in its own module, why is that?
  • How does the main GUI communicate with the core functionality 'engine' - are the two parts too tightly coupled, or is it possible to debug the one without the other?
  • How many global variables are there in my main form unit, and why do they need to be global? Do any of them represent entities that could be modularised to other unit(s)?
What is the best way to write project code? The way that is most readable, and most easily debugged. That nearly always means well-organised into self-contained modules, with loosely coupled units, and carefully chosen, self-descriptive identifiers.

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: how should i write code project?
« Reply #2 on: September 22, 2017, 04:25:31 pm »
  What is/are the core functionality/ies of the project?

Date Base - i get data from users and save them into database and then calculate it?
i just get data then process them and save and show.
 
    If it is not possible to encapsulate that functionality in its own module, why is that?

i don't know encapsulate so i don't use it.

    How does the main GUI communicate with the core functionality 'engine' - are the two parts too tightly coupled, or is it possible to debug the one without the other?


 :-\ %)  i have many units and i use them in main unit and all units  must be in my project.
 
    How many global variables are there in my main form unit, and why do they need to be global? Do any of them represent entities that could be modularised to other unit(s)?

oh my god. i have many global variables(integer byte string array .....) in main unit and in other units.?!?!?

What is the best way to write project code? The way that is most readable, and most easily debugged. That nearly always means well-organised into self-contained modules, with loosely coupled units, and carefully chosen, self-descriptive identifiers.

my code is most readable, and most easily debugged, but i write  in  one unit ?!?!

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: how should i write code project?
« Reply #3 on: September 22, 2017, 07:07:19 pm »
i do not have to much experience but:
- for the most readable part i am trying to have units of up to 500 lines and classes of up to 5 methods;
- for database i created a layer with the calls to the database i am mostly using (it is not an ORM).
this way the extraction of a dataset would look always about the same. it is further processing that could be different.
Lazarus 2.0.2 64b on Debian LXDE 10

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4458
  • I like bugs.
Re: how should i write code project?
« Reply #4 on: September 22, 2017, 07:39:27 pm »
i don't know encapsulate so i don't use it.
Encapsulation means restricting the visibility and dependencies of your code.
Mostly it is done in unit scope. Units use other units and see their definitions + published data.

Quote
:-\ %)  i have many units and i use them in main unit and all units  must be in my project.
... but do those units also use the main unit? That would be bad design.
A circular dependency is always a "serious" thing. It means 2 units are tightly intertwined together and cannot be used separately.
Object Pascal limits circular dependencies. Two units cannot refer to each other through their interface sections. At first it looks like a bad thing but actually it forces you to improve your code. If you break circular dependencies also through the implementation section, your code typically improves more.
Sometimes however pieces of code are logically so intertwined that it is OK to keep them in one unit.

Quote
oh my god. i have many global variables(integer byte string array .....) in main unit and in other units.?!?!?
Sometimes it is a sign of sloppy coding. Reduce dependencies using:
* Abstract base classes which act as interfaces
* Events

Fundamentally good SW design rolls around dependency management. Just look at design patterns. They have arrows which represent dependencies of various kind. The arrows always point to one direction. There are no circular dependencies in design patterns.
« Last Edit: September 22, 2017, 08:56:50 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: how should i write code project?
« Reply #5 on: September 23, 2017, 12:21:19 am »
One of the 1st steps is to remove as much of the code out of the "form" as possible and into units. When you do this, also think on how you would create a separate test program that can be used to test these new units.   The test program may need to provide input to your system (say a bad data base reference)  so you can make your new unit(s) code more robust. 
your results may differ but I've found breaking my code up into more smaller units gives me this testing flexibility and later a "library" of tools I can simply add to other projects.
windows 7/10 - laz 2.0 / 1.2.6 general releases

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: how should i write code project?
« Reply #6 on: September 27, 2017, 12:17:02 pm »
(...)
- for the most readable part i am trying to have units of up to 500 lines and classes of up to 5 methods;
(...)
An unit needs the lines it needs an a class needs as much methods and properties it needs.  Excuse me, but I think that using such arbitrary limitations as "number of lines" or "number of methods" as a design rule is plain silly.  And I think it doesn't adds anything to readability:  good design, goods names and good documentation does.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

TinyPortal © 2005-2018