Recent

Author Topic: Effective FreePascal book or cookbook for FPC?  (Read 3003 times)

ikel

  • New Member
  • *
  • Posts: 26
Effective FreePascal book or cookbook for FPC?
« on: May 17, 2023, 09:31:57 pm »
Hi all,

I have 2 quick questions.

C++ has Effective C++ (Scott Meyers).
Java has Effective Java (Joshua Bloch).

(Q1) Does FPC have anything like that?

(Q2) Also, does anyone know a cookbook for FPC? I'd like to start one. If anyone has started, then I'd like to contribute.

Thanks,
ikel

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: Effective FreePascal book or cookbook for FPC?
« Reply #1 on: May 18, 2023, 01:44:03 am »
I'm not familiar with the books, so took a look at the index for Effective C++.  It looks to me like what I would call a "Manual of Style."   As far as I know, no such thing exists for Pascal -- although such a book would certainly find a home here with me.  There's  no shortage of good advice articles, but they're scattered all over the internet AFAIK and buried in dozens of Delphi books. 

I hope more experienced Pascal programmers will weigh in on this -- it would be a valuable contribution, at least for me. 
« Last Edit: May 18, 2023, 04:20:33 am by Curt Carpenter »

Swami With The Salami

  • Guest
Re: Effective FreePascal book or cookbook for FPC?
« Reply #2 on: May 18, 2023, 04:09:26 am »
Back in the nineties all the Delphi books outlined what I would consider the “Manual of Style”. Conventionally class names always started with T and field names always started with F - there were members with private, protected or public visibility - there were properties that may or may not have had custom setters and getters…

Everybody that approaches Lazarus to write a GUI application will be presented with a template that looks like this:

Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.   private
  3.  
  4.   public
  5.  
  6.   end;
  7.  

Members were added in the order of fields followed by functions followed by procedures followed by properties - preferably in alphabetical order. Some of these were hard rules and others merely convention.

Of course I am talking about the books that were published when Delphi was first introduced. Object Pascal was all the rage in those days. These old books are still a great place to start for anybody approaching Lazarus for the first time.

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Effective FreePascal book or cookbook for FPC?
« Reply #3 on: May 18, 2023, 06:20:52 am »
Delphi’s Object Pascal Style Guide can be found here
Manual on Object Pascal Style Guide can be found here
There is a topic in the wiki
« Last Edit: May 18, 2023, 06:24:47 am by TRon »

cdbc

  • Hero Member
  • *****
  • Posts: 1026
    • http://www.cdbc.dk
Re: Effective FreePascal book or cookbook for FPC?
« Reply #4 on: May 18, 2023, 06:53:33 am »
Hi
Michael van Canneyt has written a lot of articles, tutorials and books, amongst "Free Pascal 3.0: Reference Guide" & "Lazarus Handbook".
Also Graeme Geldenhuys has written a number of articles. Peter Hinrichsen too, mostly about patterns and tiOPF...
If You want algorithms, then Julian Bucknall has written an excellent book: "The Tomes of Delphi, Algorithms and Datastructures". Also you might want to look for Marco Cantu...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: Effective FreePascal book or cookbook for FPC?
« Reply #5 on: May 19, 2023, 12:21:56 am »
Thanks for the links TRon.  I've spent a few hours reading through the Object Pascal Style Guide (which I'm sure would raise some eyebrows (not in a bad way -- just differences in long established habits).

I may have done Ikei a disservice by referring to the Effective C++ book as a Manual of Style.  It's a little more complicated than that.  I'll have to read further in the Object Pascal Style Guide to see how deeply it goes into things like program structure, refactoring and so on.

Thanks again.  Links well worth following.

ikel

  • New Member
  • *
  • Posts: 26
Re: Effective FreePascal book or cookbook for FPC?
« Reply #6 on: May 19, 2023, 02:02:27 am »
@Curt, thanks for the reply. Effective C++ is a bit more than a style. It is a style plus avoiding gotchas/dark corners in C++ (auto, const, pointers, etc).

@Tron, thanks for the links. I believe Manual on Object Pascal Style Guide deserves more visibility.

@ cdbc, thanks for Free Pascal 3.0: Reference Guide, it is available in PDF too. Nice! I'll spend more time on it

---

I'd like to think that FreePascal is sufficiently effective, and hence has no need for Effective FPC books  :D

-Ikel

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Effective FreePascal book or cookbook for FPC?
« Reply #7 on: May 19, 2023, 03:40:37 am »
Well style guides are one of the issues where you ask 3 people and get 4 opinions. E.g. the Style that is used within the RTL and Compiler, the begin-end usage of which for example is pretty similar to the GNU C style, I personally would never use in my personal projects (as I also do not use GNU style in C).

Personally, I use the style that the automatic code formatter uses, simply for convinience, even though I think there are better readable styles. E.g. if you have a long function definition, the code formatter will do the following:
Code: Pascal  [Select][+][-]
  1. function QuiteALongName(const Param1: TParam1; const Param2: TParam2;
  2.   const Param3: TParam3; const Param4: TParam4): TResultType;
  3.  
While I personally preferr to Align the parameters:
Code: Pascal  [Select][+][-]
  1. function QuiteALongName(const Param1: TParam1; const Param2: TParam2;
  2.                         const Param3: TParam3; const Param4: TParam4
  3.                        ): TResultType;
And if parameter list is too complex (too many parameters, or many parameters of the same type or with default values that may be confusing), even have one line per parameter:
Code: Pascal  [Select][+][-]
  1. function QuiteALongName(const Param1: TParam1;
  2.                         const Param2: TParam2;
  3.                         const Param3: TParam3;
  4.                         const Param4: TParam4
  5.                        ): TResultType;
Personally I find this much more readable, but you see this style very rarely for parameter lists.

In Pascal we don't have many "formalized" styleguides, as there are in C, that doesn't matter that there aren't as many options. There is no right or wrong style (look at the C community, there are probably dozens of styles: Google, LLVM, GNU, etc.) but in the end, the only important part is that when you work on a project with others, you agree on a common style and all stick to it.

Also, if there is one key advice that I would give everyone at anytime: use meaningful names as long as necessary but as concise as possible. We have gigabytes of storage and an IDE that does auto complete, there is absolutely no need to stick to three letter names like it's the 70s. Like use "password" instead of "pwd" or "value" instead of "val". An example for this is the function Val that converts strings to integers, it's a very bad name, because first it is an abbreviation for value, and if you don't know, you may think this means something different (e.g. valid), second the term "value" does not describe what the function actually does. There is no way a programmer finds out what this does without looking it up in the documentation. Oppose that with TryStrToInt, which does the exact same thing, but has a much more meaningful name, this is a function that you can discover and fully understand what it does just from the name and signature alone.

This also goes in the other direction, e.g. you often see types having arbitrary additions like Data or so. If you have a class for holding configuration data, just call it TConfiguration, not TConfigurationData. That additional Data does not add any meaning
« Last Edit: May 19, 2023, 03:54:21 am by Warfley »

ikel

  • New Member
  • *
  • Posts: 26
Re: Effective FreePascal book or cookbook for FPC?
« Reply #8 on: May 19, 2023, 03:57:25 am »
Thanks Warfley,

Thanks for the styles provided.
Yes, I am fully aware, asking code styles you get different answers.

Effective C++ is more than just a style. It is style plus guidelines on writing effective C++.

I attached an excerpt from `Effective C++` book, on using `const` in C++.




440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Effective FreePascal book or cookbook for FPC?
« Reply #9 on: May 19, 2023, 06:46:42 am »
Just for anyone who might be interested, I use the following form:
Code: Pascal  [Select][+][-]
  1. function QuiteALongName
  2.            (
  3.             constref Param1: TParam1;
  4.                const Param2: TParam2;
  5.                      Param3: TParam3;
  6.                  var Param4: TParam4
  7.            )
  8.          : TResultType;
  9. begin
  10.   <indented code>
  11. end;

This way the result type is always in the same place (vertically flush with the function name), it is instantly localizable (no need to read left to right) and the number of parameters as well as their disposition is easy to establish.

I find the result to be very "eye friendly" and very easy to read and understand.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Effective FreePascal book or cookbook for FPC?
« Reply #10 on: May 19, 2023, 12:33:46 pm »
I attached an excerpt from `Effective C++` book, on using `const` in C++.

I know, I have skimmed through the book, this was more about the posts early with the links to the style guidelines. The book also describes a few of the undefined behavior and other pitfalls. It's something thats for touched by most Books to a certain degree, but there is a big thing about Pascal that is very different to C++, C++ is much more complex, has much more features compared to Pascal. There simply are not as many pitfalls in Pascal than there are in C++. And whats also important C++ has changed much more over the years than object pascal has. If you program today like it's Delphi 5, it may not use all of the newest features, but it will generally be totally servable today. In contrast, if you follow some of the advice in the Effective C++ book from 2005, your code will probably not be accepted into a modern code base (e.g. it reccomends using enums as hack for global integer variables, while since C11 from 2011, you should use constref for this)

There are of course some tricks and recommendations commonly given for Pascal, but even there the quality of such advice can vary, e.g. I have recently created a thread where I discuss the problems with Free and FreeAndNil and how it is unsafer, yet pretty much everywhere it is reccomended to never use Destroy directly but rather Free.

So I'm always a bit concious when it comes to information on how to utilize programming features best. For example the idea that you should use Free instead of Destroy comes from a kind of fear of crashes, and Free helps to avoid crashes by simply hiding the problem that causes them. This speaks to many people because there is the prevalent opinion that crashes are bad, and thats completely understandable from from a users perspective, but as a developer you want your application to crash as much and hard as possible as soon as something is not how it should, because finding the source of a crash is easy.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Effective FreePascal book or cookbook for FPC?
« Reply #11 on: May 19, 2023, 12:42:44 pm »
Additional to above "Parameters" inside methods, I would suggest using a leading A for Argument.
Example:
procedure Foobar(AParam: TParam); ....
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

ikel

  • New Member
  • *
  • Posts: 26
Re: Effective FreePascal book or cookbook for FPC?
« Reply #12 on: May 19, 2023, 01:12:34 pm »
Just for anyone who might be interested, I use the following form:
Code: Pascal  [Select][+][-]
  1. function QuiteALongName
  2.            (
  3.             constref Param1: TParam1;
  4.                const Param2: TParam2;
  5.                      Param3: TParam3;
  6.                  var Param4: TParam4
  7.            )
  8.          : TResultType;
  9. begin
  10.   <indented code>
  11. end;

....

Thanks 440bx, that style is very new to me, and easy to read!

ikel

  • New Member
  • *
  • Posts: 26
Re: Effective FreePascal book or cookbook for FPC?
« Reply #13 on: May 19, 2023, 01:27:36 pm »
I attached an excerpt from `Effective C++` book, on using `const` in C++.
... but there is a big thing about Pascal that is very different to C++, C++ is much more complex, has much more features compared to Pascal. There simply are not as many pitfalls in Pascal than there are in C++. And whats also important C++ has changed much more over the years than object pascal has...

... So I'm always a bit concious when it comes to information on how to utilize programming features best. For example the idea that you should use Free instead of Destroy comes from a kind of fear of crashes, and Free helps to avoid crashes by simply hiding the problem that causes them. ..., but as a developer you want your application to crash as much and hard as possible as soon as something is not how it should, because finding the source of a crash is easy.

True, C++ is far more complex than Pascal, and there are more pitfalls devs need to keep in mind.

Thanks for the link to Free vs FreeAndNil discussion. I will check it too.

-ikel

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Effective FreePascal book or cookbook for FPC?
« Reply #14 on: May 19, 2023, 01:50:37 pm »
Thanks 440bx, that style is very new to me, and easy to read!
You're welcome.

Another convention I follow which I didn't show in the example is that I prefix all function/procedure parameters with "In",  "Inout", "Out" depending on whether the parameter is In/Inout/Out.  The advantages are twofold, it is easy to tell a parameter from a local variable because only parameters can be In/Inout/Out and the disposition also serves as documentation and consistency checking, e.g, an output or Input&output parameter should be declared as "out", whereas an input parameter should be declared as "var" if passed by reference and left without the "var" modifier if passed by value. 

If you are interested to see the result of applying these conventions in a program, there is a program that uses them at https://forum.lazarus.freepascal.org/index.php/topic,61228.0.html

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018