Books Writers Publishers Languages

Delphi, in all its glory (part 2) : Reaching for the Gods

Writed by : Gabriel Moraru

Published date : 31/01/2024

ISBN-13 : 9798878103749

Language : English English

Web site : https://gabrielmoraru.com/publications-citations/the-delphi-in-all-its-glory-2-book/

About Delphi, in all its glory (part 2) : Reaching for the Gods

Whether you’re a curious novice venturing into the world of coding for the first time or already the user of a different programming language, this book is your gateway to Delphi coding.

The first book concentrates on the exploration of the Delphi language. It doesn’t matter if you never wrote a line of code – we will start from ground zero, but the process will be faster if you already have done some programming in other languages.

Even if you are a (young) Delphi programmer, you could still learn a few tricks from the first book (check the summary).
If you are an old-school Pascal programmer that was always afraid to switch to objects, I promise, this is THE BOOK for you!

The second book is for the more seasoned developer aiming to enhance their craft and concentrates on the more advanced topics, such as libraries and architecture design, compiler’s and debugger’s inner working, proper exception handling and most important safer code.

What will you learn from this book?

This is not the regular “programmer’s manual” kind of book where I show you the basics of programming and keep circling around stupid-simple code examples. While I do kick-start you into programming, the main purpose of this book is to take you to the “meta” level of programming and even deep beyond that. But while study the language as regular, mere programmers, we will learn in the background how to build stable programs through reliable code, how to avoid typical programming pitfalls, why some programming concepts are safer than others, how to turn mondain Delphi features into an error-prevention system…

A second purpose of this book is also to demonstrate that Delphi is the best tool if you want to develop:

Good looking GUI applications
Cross-platform applications
Fast applications (for example data-processing, games)
Business and scientific applications

In parallel with all the above, we will compare Delphi with other languages (mostly C++, C#, R, Java, and Python) to see their merits and pitfalls and to reassure you that Delphi is THE language for you.
I will never ask you in this book to just believe me. I will always try to present hard evidence – scientific articles, proved concepts, read code examples, benchmark tests.

Even if the stakes are high, I promise it will NOT be a bumpy road. The learning curve might be steep in some places, but the book is peppered with code examples, case studies and diagrams that will explain the concepts we study.

Book layout

In the first book we have seen what kind of applications we can do with Delphi, then we dived quite deep into the Delphi language and object-oriented programming, and we saw what is safe to do and what is not, and most important, why. And in the background, we learned about high quality code, because in the end this is the ultimate goal if you want to more reach the Gods of programming.
The “safety first” concept will be strengthened in this book where continue to learn about code reliability, designing sound architectures and preemptive programming. All this, while studying in foreground advanced concepts such as debugging, memory management, proper exception-handling, etc. Oh boy… we will have fun! 

Where to buy ?

This book has the ISBN13 "9798878103749".
If it is still available for sale, you can order it in your favorite bookstore, by its publisher or online at
Amazon CA, Amazon FR, Amazon JP, Amazon UK or Amazon USA depending on your country.

Table of content

1. Overview

Intended Audience
What will you learn from this book?
Book layout
Who am I?
How is this book written?
Environment-responsible
Pre-requisites for this book
Let’s start

2. Table of content

3. The Delphi compiler

What is a compiler? What is an interpreter?
Compiled vs interpreted
A reliable compiler
The toolchain
Executable size
Compiler speed
From Pas to Exe
The compilation order
Compiling and running the program
DCUs
Debug vs Release DCUs
The linker
Compile vs Build
Improving compiler’s speed
Remove unused units
Rearrange the units in the uses section
Unused paths

4. Libraries

Why do we need libraries?
Libraries are not for everyone
The four rules of creating packages
Library architecture
Circular references
It is all about aliens and pyramids
The anatomy of a package
DPK
BPL/DCP
Project groups
Compiler Directives
Delphi’s framework
The RTL library
The VCL library
Library paths
Environment variables
Compiling with multiple Delphi versions
Finding files via Library paths
Search Paths
Under-development libraries
Global vs local paths
Precompiled files
Precompiling our own files
The “Package/DCP/HPP output directory” fields
Project Paths
Recap
How to install a library?
Installing a library manually
Single-file libraries
What to do when things go south?
Library folder structure
Installing libraries via Setup
Creating our own installer
IDE plugins
Installing libraries via GetIt
How to uninstall a library
Runtime/Designtime libraries
Run-time libraries
Design time libraries
Design time vs Runtime
Including a library in our project
Linking with runtime packages
Drawbacks
How does the linker optimize unused code?
Experiment calibration
Proceedings
Extension to the experiment
Debugging 3rd party libraries
Recompiling parts of the VCL
Why libraries are mandatory for large projects?
Logic separation
The Chaos of Monolithic Projects
The Breaking Point
The Refractory Ignition Spark
A Better Way: Modular Code via Libraries
Overcoming Preconceptions
Using C/++ libraries in Delphi
When you have the source code
When you have the DLL
Popular libraries
LightSaber
Proteus
About libraries and licenses
Meaning of the fields
Discussions
Conclusions
More details
The Initialization of packages
The Register procedure
Building our own visual components
5. Automatic compilation

Using MSBuild in Delphi

6. The debugger

What is a debugger?
Preparing our project for debugging
Stacks
Configurations
The “Compiling” page
The “Use debug DCUs” field
Other settings
A picture is worth a thousand words.
The “Linking” page
The “Debug source path” field
Breakpoints
Breakpoint properties
Relocate the execution point manually
Finally, ready to debug
Starting the program under the debugger
Debugging from a crash point
Cannot debug?
Fun stuff to do during debugging
Hint for geeks
Debugging cheat sheet

7. Remote debugging

How does it work?
Install PAserver (on the remote machine)
Run PAServer (on the remote machine)
Configure the environment (locally)
Assign a Connection Profile to the Delphi project
Choose which files to deploy
Deploying the files manually
Start debugging
Stability

8. Automatic bug logging & reporting

MadShi MadExcept
EurekaLog

9. Advanced debugging techniques

Method 1: Attach to process
Method 2: Debug with “Run -> Load process”.
Method 3: Host application (Recommended)
Method 4: External tools
Method 5: Start Delphi in “safe” mode
Method 6: Use external tools
Stupid Windows 10

10. Speed up your program

Profiling your program
Home-brewed method (as cheap as the home-made beer)
Third party profilers (aka non-free)
Improving the speed
Record field alignment
CPU Optimization
Compared to Julia

11. Compiler! Please save my ass!

Overflow checking
IO checking
Range Checking
Enabling Runtime Error Checking
Temporary disabling range checking
Assertions
Assert vs Raise
Nothing comes for free

12. Memory management

FastMM
1. Dramatic speed increase for memory-bound operations
2. Prevents memory fragmentation
3. Better memory sharing
4. Safer code
5. No more access violations
Switching FastMM to Full Debug Mode
How to read a FastMM leak report?
The summary
Leaked blocks
Advanced topics
You create it, you kill it
Memory ownership
Dangling references
Alice loses her preferred book
Getting our dirty paws on a dangling reference
The root of all evil
There are even worst scenarios
Do some noise
Bruce Willis (and FastMM) saves the day… again!
Garbage collectors
About ownership
Passing the ownership around
Summary

13. Exceptions

Exception Handling
Let’s do the “What this code does” game
Amateur mistake
Keep it real
Spit, don't swallow
Good boy, Debugger!
Ignoring exceptions during debugging
Try/Finally vs Try/Except
Raising exceptions

14. Windows Messages in Delphi

The Windows Messaging System
Message Handlers in Delphi
Practical Examples

15. Programmer’s toolbox

Delphi registry secrets
Restoring the registry
How to start a “mint” Delphi instance?
Total Commander
SoftPerfect RAMDisk
Version control
Do we need a repository?
Creating a new Tortoise repository
Revisions
Resolving Conflicts
Commit Progress
Icon overlays
Shelving
Normalizers

16. Code safety

Things that are ok, but dangerous
Don't use pointers
Don’t do direct typecasts
Avoid threads
Avoid TTimer
Avoid using unsafe functions
Things related to the Delphi language
Use strong typing
Avoid “with”
Use assertions. Abundantly
Document your code
Follow coding standards
Extra care with Interfaces
Perform boundary checks
Avoid using unsafe functions
Handling Boundary Values
Dealing with Wrapping
Simply “don’t do it”
Don’t swallow exceptions
Don't use Application.ProcessMessages
Letting the compiler to help us
Activate rage checking
Activate I/O checking
Activate integer overflow
Activate compiler hints and warnings
Other runtime checks
Enable FastMM to check for memory leaks
Implement "safety net" for enumerations
Use Free instead of FreeAndNil
Extra techniques
Test program thoroughly
Implement Unit Testing for your code.
Use a bug tracking tool, like MadShi!
External libraries
Encapsulate your code
Cheat sheet
Quantify the source of errors
Classification of error types:
Program architecture
Extra stuff to check for
On FreeAndNil
A word of warning on Application.ProcessMessages
TControl.Perform
Safely storing a list of objects
Security update 2024

17. Advance topics

Running a single instance of our program
How does it work?
Let’s see the code
Anonymous methods
Capture of Variables
Usage
Flexible Usage
Inline Definitions
Analyzing the code above
Multithreading in Delphi
Delphi's Multithreading Infrastructure
Creating Threads with TThread
Thread Safety
Synchronization with the Main GUI Thread
Using TThread.Synchronize
Using TThread.Queue
Other synchronization mechanisms:
Best Practices for Multithreading
1. Plan Your Multithreading Strategy
2. Handle Exceptions Gracefully
3. Minimize Shared Data
Common Pitfalls to Avoid
Delphi Parallel Programming Library (PPL)
Hardware limitations

18. Migrating 32-bit Code to 64-bit: Best Practices and Considerations

Alternative paths
How to set the LARGE_ADDRESS_AWARE?
Before you start
Upgrading Third-Party Libraries
Adding 64-bit Support to the Project
Fixing the paths
Inspecting the code
Choosing the correct data types
Typecast from pointer to integer
Typecast between Record and TObject
WinAPI issues
SendMessage
Vcl.Controls.TControl.Perform
SetWindowLong/ GetWindowLong
SetWindowLong:
SetWindowLongPtr:
Special types
Avoid using LongInt
Extended type
Inline ASM code
Functions that return Cardinal
Summary

19. Deploying our application on other platforms

Android
Apple
Linux
Summary

20. Being a programmer

Change is good… but hard
On Technical Debt
Rocking the boat
So, do you still want to do it?

21. All good things must come to an end

Where do you go from here?
Delphi sample projects
Your next big project could be a game
About Stack Overflow
Fighting bullies on Stack Overflow
Ask the artificial intelligence
Video tutorials
Books
My books
Already published books:
What I have learned in the last 25 years of software development

22. Notes and credits

Contact me!
Disclaimer
Source code
Be environment-responsible
Credits