Re: [fpc-pascal] How to have smaller units
Rafael Picanço via fpc-pascal schrieb am So., 30. Nov. 2025, 17:10: > Then you simply have the wrong idea what a unit it. It is intended for code >> with high cohesion, so it doesn't matter if it gets large. >> >> Regards, >> Sven >> > > Can you help me out with a few questions, Sven? > > (1) Do you consciously use formal cohesion criteria? (Of course, some more > or less frequently than others.) > A quick search here returned this study, for example: > https://dl.acm.org/doi/10.1145/1131421.1131422 > No. > > (2) Can you list the underlying cohesion criteria used in the units of > Free Pascal, or point to any material on the subject? > There are none. Regards, Sven > ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On Nov 30, 2025 at 10:44:47 PM, DougC via fpc-pascal < [email protected]> wrote: > One good exercise is to place each class in its own unit. Do the classes > now work well together? If not, you have not designed your hierarchy > according to proper OOP methodology. > The problem I'm assuming is he wants to write access to some fields across many classes but ONLY in the library. Users of the library should not have write access. This makes sense to me but it means due to the "file private" nature of the scoping all classes must be in the same unit. Personally I'm fine with big units. I have some that are nearly 10,000 lines but that doesn't matter because compilation times are fast and there is a language server so you can jump to methods easily. I think OP should just accept this and forget about putting each class in its own file, which for me personally is harder to navigate. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
> > Then you simply have the wrong idea what a unit it. It is intended for code > with high cohesion, so it doesn't matter if it gets large. > > Regards, > Sven > Can you help me out with a few questions, Sven? (1) Do you consciously use formal cohesion criteria? (Of course, some more or less frequently than others.) A quick search here returned this study, for example: https://dl.acm.org/doi/10.1145/1131421.1131422 (2) Can you list the underlying cohesion criteria used in the units of Free Pascal, or point to any material on the subject? Best regards, R ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
I agree with what Giuliano has said, but would also encourage Amir to just look at his object hierarchy critically. He does not have to abandon the OOP method just because he got into trouble here. I have run into similar problems when designing my object hierarchies. The solution was found in recognizing my PARTIAL adoption of the OOP method. In other words, I followed OOP up to a certain point, and then saw an easy way to solve some data access problems by short-circuiting the OOP method and making use of the private access afforded by locating several classes in the same unit. But that is, indeed, a shortcut that violates the OOP principles. One good exercise is to place each class in its own unit. Do the classes now work well together? If not, you have not designed your hierarchy according to proper OOP methodology. On Sun, 30 Nov 2025 07:49:13 -0500 Giuliano Colla via fpc-pascal wrote --- Hi, IMHO your basic problem is that you are trying to use objects in a way which contradict the basic object concept. The basic concept is to expose an interface and hide the implementation. Higher level code should be able to use objects without caring of implementation. Private object members are just private, implementation dependent, and therefore should not be accessible. If they are, you are making your high level code dependent from object implementation, which is wrong. If for some reasons you cannot rearrange your code in such a way as to be coherent with object philosophy, that means that you should not use objects. You're not obliged to. An object is nothing else but a data structure, which includes in addition the procedures to deal with the data. But you may write just data structures and procedures separately, and make whatever you need be accessible form whatever else you need. And if you don't want end users have the same rights, you just use an interface. Your code will be loaded at run time, and end users will only be able to access what the interface exposes. That is the trick used by Lazarus, whose high level objects are implemented in a totally different way depending on the selected widgetset. Just my two cents. Giuliano Il 28/11/25 22:58, Amir via fpc-pascal ha scritto: > Hi, > > One of the main thing bothering me while developing projects in > Pascal is that my units are getting huge in size! The main reason is > that the nice property that the classes could access each other > private members if they are defined in the same unit motivates/forces > me to define a lot of classes in the same unit. Then, all the > functions/procedures implementation must go into the same unit. I know > I can use inc file but I do not like it! The other option is to use > "Cracker" pattern, in Delphi, which is fine but seems like a hack! > > In C++, the concept of header files vs c++ files is helpful. In > Golang, one could implement the functions for a class in several > files, as long as they are in the same directory (namespace). > > Wondering if there is a solution for this in Object-Pascal? > > Best, > Amir > > > ___ > fpc-pascal maillist - mailto:[email protected] > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Do not do to others as you would have them do to you.They might have different tastes. ___ fpc-pascal maillist - mailto:[email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Well said Giuliano ! Regards Vern On 2025-11-30 07:49, Giuliano Colla via fpc-pascal wrote: Hi, IMHO your basic problem is that you are trying to use objects in a way which contradict the basic object concept. The basic concept is to expose an interface and hide the implementation. Higher level code should be able to use objects without caring of implementation. Private object members are just private, implementation dependent, and therefore should not be accessible. If they are, you are making your high level code dependent from object implementation, which is wrong. If for some reasons you cannot rearrange your code in such a way as to be coherent with object philosophy, that means that you should not use objects. You're not obliged to. An object is nothing else but a data structure, which includes in addition the procedures to deal with the data. But you may write just data structures and procedures separately, and make whatever you need be accessible form whatever else you need. And if you don't want end users have the same rights, you just use an interface. Your code will be loaded at run time, and end users will only be able to access what the interface exposes. That is the trick used by Lazarus, whose high level objects are implemented in a totally different way depending on the selected widgetset. Just my two cents. Giuliano Il 28/11/25 22:58, Amir via fpc-pascal ha scritto: Hi, One of the main thing bothering me while developing projects in Pascal is that my units are getting huge in size! The main reason is that the nice property that the classes could access each other private members if they are defined in the same unit motivates/forces me to define a lot of classes in the same unit. Then, all the functions/procedures implementation must go into the same unit. I know I can use inc file but I do not like it! The other option is to use "Cracker" pattern, in Delphi, which is fine but seems like a hack! In C++, the concept of header files vs c++ files is helpful. In Golang, one could implement the functions for a class in several files, as long as they are in the same directory (namespace). Wondering if there is a solution for this in Object-Pascal? Best, Amir ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Hi, IMHO your basic problem is that you are trying to use objects in a way which contradict the basic object concept. The basic concept is to expose an interface and hide the implementation. Higher level code should be able to use objects without caring of implementation. Private object members are just private, implementation dependent, and therefore should not be accessible. If they are, you are making your high level code dependent from object implementation, which is wrong. If for some reasons you cannot rearrange your code in such a way as to be coherent with object philosophy, that means that you should not use objects. You're not obliged to. An object is nothing else but a data structure, which includes in addition the procedures to deal with the data. But you may write just data structures and procedures separately, and make whatever you need be accessible form whatever else you need. And if you don't want end users have the same rights, you just use an interface. Your code will be loaded at run time, and end users will only be able to access what the interface exposes. That is the trick used by Lazarus, whose high level objects are implemented in a totally different way depending on the selected widgetset. Just my two cents. Giuliano Il 28/11/25 22:58, Amir via fpc-pascal ha scritto: Hi, One of the main thing bothering me while developing projects in Pascal is that my units are getting huge in size! The main reason is that the nice property that the classes could access each other private members if they are defined in the same unit motivates/forces me to define a lot of classes in the same unit. Then, all the functions/procedures implementation must go into the same unit. I know I can use inc file but I do not like it! The other option is to use "Cracker" pattern, in Delphi, which is fine but seems like a hack! In C++, the concept of header files vs c++ files is helpful. In Golang, one could implement the functions for a class in several files, as long as they are in the same directory (namespace). Wondering if there is a solution for this in Object-Pascal? Best, Amir ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Do not do to others as you would have them do to you.They might have different tastes. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Amir via fpc-pascal schrieb am Fr., 28. Nov. 2025, 23:06: > Hi, > > One of the main thing bothering me while developing projects in Pascal > is that my units are getting huge in size! The main reason is that the > nice property that the classes could access each other private members > if they are defined in the same unit motivates/forces me to define a lot > of classes in the same unit. Then, all the functions/procedures > implementation must go into the same unit. I know I can use inc file but > I do not like it! The other option is to use "Cracker" pattern, in > Delphi, which is fine but seems like a hack! > Then you simply have the wrong idea what a unit it. It is intended for code with high cohesion, so it doesn't matter if it gets large. Regards, Sven ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On 11/29/25 5:09 AM, Hairy Pixels via fpc-pascal wrote: Honestly though it's not an unsolvable problem it just means you need to make interface/implementation IFDEFs in the units and then join them together yourself. I think this pattern is widely used but it is hacking around a feature the compiler should have in my opinion. That's exactly what I am looking for. Amir ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On Nov 29, 2025 at 8:40:33 PM, Thomas Kurz via fpc-pascal < [email protected]> wrote: > Can't you use a type helper to access the fields only locally in other > classes? Unfortunately this works only for `protected` and not for > `private` fields, but if it's your own class, maybe you'd like to give it a > try. > > Btw: I would be great if type helpers could be given access to `private` > fields, too! > > Pretty sure no because the fields are private which is scoped to a single file so if you declare the type helper in another unit you can't access the field. That would be annoying to make helpers for all the types too. I just tried actually and a property which read a public field from a class in another unit failed too. That seems like a bug to me. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Can't you use a type helper to access the fields only locally in other classes? Unfortunately this works only for `protected` and not for `private` fields, but if it's your own class, maybe you'd like to give it a try. Btw: I would be great if type helpers could be given access to `private` fields, too! - Original Message - From: Hairy Pixels via fpc-pascal To: FPC-Pascal users discussions Sent: Saturday, November 29, 2025, 14:31:33 Subject: [fpc-pascal] How to have smaller units On Nov 29, 2025 at 6:43:44 PM, Guillermo Martínez Jiménez via fpc-pascal < [email protected]> wrote: > I have no idea about what are you doing but I'm pretty sure you can fix > this by using a different design; maybe to revise some patterns will > help you to find the way to do it more properly. He has a fair point. Imagine you have two classes and they want to write to their fields but this only happens internally in your library while the users of the library should never do this. How do you solve it? You basically want a write property but just internally in your library. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On Nov 29, 2025 at 6:43:44 PM, Guillermo Martínez Jiménez via fpc-pascal < [email protected]> wrote: > I have no idea about what are you doing but I'm pretty sure you can fix > this by using a different design; maybe to revise some patterns will > help you to find the way to do it more properly. > He has a fair point. Imagine you have two classes and they want to write to their fields but this only happens internally in your library while the users of the library should never do this. How do you solve it? You basically want a write property but just internally in your library. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On Nov 29, 2025 at 4:58:38 AM, Amir via fpc-pascal < [email protected]> wrote: > In C++, the concept of header files vs c++ files is helpful. In > Golang, one could implement the functions for a class in several files, > as long as they are in the same directory (namespace). > I see your point, if there is a "file private" restriction then that constrains you to one file by definition and now you must use includes to break code apart. Personally I don't like them either because they break Pascals unit design and I've long wanted a way to include a unit so it behaves like an include. Honestly though it's not an unsolvable problem it just means you need to make interface/implementation IFDEFs in the units and then join them together yourself. I think this pattern is widely used but it is hacking around a feature the compiler should have in my opinion. ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Good morning, What you are asking for is "friend classes", a concept from C++. I admit it wasn't a very bad idea but I think it breaks the "Object" philosophy. An object should be self-contained. If you think that an object should access to the privacy of another object, then there's a mistake in your design. I have no idea about what are you doing but I'm pretty sure you can fix this by using a different design; maybe to revise some patterns will help you to find the way to do it more properly. These are my two cents. Guillermo "Ñuño" Marínez El Fri, 28 Nov 2025 13:58:38 -0800 Amir via fpc-pascal escribió: > Hi, > > One of the main thing bothering me while developing projects in > Pascal is that my units are getting huge in size! The main reason is > that the nice property that the classes could access each other > private members if they are defined in the same unit motivates/forces > me to define a lot of classes in the same unit. Then, all the > functions/procedures implementation must go into the same unit. I > know I can use inc file but I do not like it! The other option is to > use "Cracker" pattern, in Delphi, which is fine but seems like a hack! > > In C++, the concept of header files vs c++ files is helpful. In > Golang, one could implement the functions for a class in several > files, as long as they are in the same directory (namespace). > > Wondering if there is a solution for this in Object-Pascal? > > Best, > Amir > > > ___ > fpc-pascal maillist - [email protected] > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On 28/11/2025 21:58, Amir via fpc-pascal wrote: I know I can use inc file but I do not like it! Unfortunate that you don't like them! Its an obvious solution. Cheers, Perter ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
That's an interesting approach! On November 28, 2025 2:42:42 PM PST, Martin Frb via fpc-pascal wrote: >On 28/11/2025 22:58, Amir via fpc-pascal wrote: >> Hi, >> >> One of the main thing bothering me while developing projects in Pascal is >> that my units are getting huge in size! The main reason is that the nice >> property that the classes could access each other private members if they >> are defined in the same unit motivates/forces me to define a lot of classes >> in the same unit. Then, all the functions/procedures implementation must go >> into the same unit. I know I can use inc file but I do not like it! The >> other option is to use "Cracker" pattern, in Delphi, which is fine but seems >> like a hack! >> >> In C++, the concept of header files vs c++ files is helpful. In Golang, >> one could implement the functions for a class in several files, as long as >> they are in the same directory (namespace). >> >> Wondering if there is a solution for this in Object-Pascal? > >You don't need the entire classes in the same unit. > >Introduce baseclasses, that have access to each others fields > >TBarBase = class; > >TFooBase = class >private > function GetBarFieldSomeThing(ABar: TBarBase): TSomeType; >end; > >TBarBase = class >private > procedure DoBeforeGettingSomeThing; virtual; abstract; > function GetFieldSomeThing: TSomeType; >// and vice versa > function GetFooOtherStuff(AFoo: TFooBase); >end; > >Those methods are just field accessors. They do not do any work. > >If you need work to be done, then call an abstract method. >e.g. GetFieldSomeThing can call DoBeforeGettingSomeThing if the actual TBar >class needs to do work. > >Then TFoo and TBar can go into 2 separate units and don't need to know >anything of each other. > > >That works, with twin (dual) relations. But it introduces limits. > >The other option is that each class can have "interfaces/api-accessors" (class >or interface), and control to whom it gives them. > >That is in the unit for TBar you also have TBarFooAPI (you can have many >different ones). > >TBarFooAPI has everything public that TFoo needs. >But it can only be created by TBar. So TBar can decide who get it. Then you >only need one connection between the 2 classes, to exchange the access object. >TBarFooAPI is a token to access TBar. > >To make sure only TBar can created it => create an abstract base. >Then have an implementation section sub-class that implements everything. >Since it is implementation section, only TBar can see it, and create it. But >the instance can be given away, and since the methods are all >virtual/overrides, they can be accessed by knowing the public abstract base >class. >(May also work with nested class declarations) > > > >___ >fpc-pascal maillist - [email protected] >https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
Defining the elements as public (or having public getters/setters) allows anyone who initializes the class to set/update the values. This is what I do not want. I just want the other class(es) defined in the same unit to be able to do so. On 11/28/25 2:27 PM, Tomas Hajny via fpc-pascal wrote: On 2025-11-28 22:58, Amir via fpc-pascal wrote: Hi, One of the main thing bothering me while developing projects in Pascal is that my units are getting huge in size! The main reason is that the nice property that the classes could access each other private members if they are defined in the same unit motivates/forces me to define a lot of classes in the same unit. Then, all the functions/procedures implementation must go into the same unit. I know I can use inc file but I do not like it! The other option is to use "Cracker" pattern, in Delphi, which is fine but seems like a hack! In C++, the concept of header files vs c++ files is helpful. In Golang, one could implement the functions for a class in several files, as long as they are in the same directory (namespace). Wondering if there is a solution for this in Object-Pascal? What prevents you from declaring the class members as public if you want/need to access them from other classes? What prevents you from using (public) getters/setters and/or other methods instead of accessing the members directly? Tomas ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On 28/11/2025 22:58, Amir via fpc-pascal wrote: Hi, One of the main thing bothering me while developing projects in Pascal is that my units are getting huge in size! The main reason is that the nice property that the classes could access each other private members if they are defined in the same unit motivates/forces me to define a lot of classes in the same unit. Then, all the functions/procedures implementation must go into the same unit. I know I can use inc file but I do not like it! The other option is to use "Cracker" pattern, in Delphi, which is fine but seems like a hack! In C++, the concept of header files vs c++ files is helpful. In Golang, one could implement the functions for a class in several files, as long as they are in the same directory (namespace). Wondering if there is a solution for this in Object-Pascal? You don't need the entire classes in the same unit. Introduce baseclasses, that have access to each others fields TBarBase = class; TFooBase = class private function GetBarFieldSomeThing(ABar: TBarBase): TSomeType; end; TBarBase = class private procedure DoBeforeGettingSomeThing; virtual; abstract; function GetFieldSomeThing: TSomeType; // and vice versa function GetFooOtherStuff(AFoo: TFooBase); end; Those methods are just field accessors. They do not do any work. If you need work to be done, then call an abstract method. e.g. GetFieldSomeThing can call DoBeforeGettingSomeThing if the actual TBar class needs to do work. Then TFoo and TBar can go into 2 separate units and don't need to know anything of each other. That works, with twin (dual) relations. But it introduces limits. The other option is that each class can have "interfaces/api-accessors" (class or interface), and control to whom it gives them. That is in the unit for TBar you also have TBarFooAPI (you can have many different ones). TBarFooAPI has everything public that TFoo needs. But it can only be created by TBar. So TBar can decide who get it. Then you only need one connection between the 2 classes, to exchange the access object. TBarFooAPI is a token to access TBar. To make sure only TBar can created it => create an abstract base. Then have an implementation section sub-class that implements everything. Since it is implementation section, only TBar can see it, and create it. But the instance can be given away, and since the methods are all virtual/overrides, they can be accessed by knowing the public abstract base class. (May also work with nested class declarations) ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to have smaller units
On 2025-11-28 22:58, Amir via fpc-pascal wrote: Hi, One of the main thing bothering me while developing projects in Pascal is that my units are getting huge in size! The main reason is that the nice property that the classes could access each other private members if they are defined in the same unit motivates/forces me to define a lot of classes in the same unit. Then, all the functions/procedures implementation must go into the same unit. I know I can use inc file but I do not like it! The other option is to use "Cracker" pattern, in Delphi, which is fine but seems like a hack! In C++, the concept of header files vs c++ files is helpful. In Golang, one could implement the functions for a class in several files, as long as they are in the same directory (namespace). Wondering if there is a solution for this in Object-Pascal? What prevents you from declaring the class members as public if you want/need to access them from other classes? What prevents you from using (public) getters/setters and/or other methods instead of accessing the members directly? Tomas ___ fpc-pascal maillist - [email protected] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
