Stefan Boos

My personal GitHub pages

Follow me on GitHub

Delphi

Table of Contents generated with DocToc

Getting Started

Download and Install

To learn and get started with Delphi you can use the Delphi Community Edition.

Getting Started by Example

Next Things to Read and Do

At the end of the page you can find further references including the Books section.

The Object Pascal Handbook is a good read [1].

Improving Daily Work

Language References

  • RTL - Reference about the standard libraries.

  • System - Contains the Delphi run-time library (RTL) units, classes, functions, types, variables, and constants.

  • Date and Time Support - Delphi types, routines, and variables that add Date and Time support.

Keyboard Shortcuts

Changing the Default Window Layouts

Static Code Analysis

Know How, Tips and Tricks

Good Practices

  • From every constructor call the base constructor first. This ensures that all members are initialized when a derived constructor expects them to be [1], p. 389
  • Every destructor should be virtual. This ensures that all memory allocated by derived classes gets freed even if you call Free for a base type [1], p. 389
  • From every destructor call the base destructor last. This ensures that all memory allocated by parent classes gets freed after depending memory has been freed in derived classes [1], p. 389
  • In a destructor never assume the object has been fully initialized, because the constructor might throw an exception while still allocating objects. Thus, always check assigned(object) before object.Free in the destructor [1], p.390

Tips and Tricks

  • Create line breaks in string messages: 'Hello'#13#10'World'
  • Path and file name operations: TPath, TFile (cross platform)
  • Path of the current executable: TPath.GetLibraryPath (on iOS this may be the ZIP file containing the executable)
  • Implementing Smart Pointers is described in [1], pp. 436

Memory Management Insights

While learning Delphi I have encountered the following questions:

Q: Does Delphi have a Garbage Collector? Why do I have to call <MyObjectInstance>.Free() in a TForm.FormDestroy method? (see section “Wiring the Buttons to the Code” in Learn to Program with Delphi Community Edition: Part 5 - Putting the Calculator Together)

A: If you use an interface as the variable type, then reference counters are used and the instance is freed automatically. If you don’t use an interface as the variable type, then the instance is must be cleaned up. The entire story about memory management with ownership, weak references and reference counting is told in chapter “13 Objects and Memory” in Maco Cantú: Object Pascal Handbook [1].

Note that in chapter “Smart Pointers in Object Pascal” [1], Marco Cantú introduces a Smart Pointer implementation for Delphi < 10.4 and another one for Delphi >= 10.4. Erik Bilsen disucsses the latter solution in his blog entry Custom Managed Records for Smart Pointers.

Coding Support: Components and IDE Extensions for Delphi RAD Studio

Open Source OR Free for Personal Use

License Required

  • madExcept - exception reporter facilitating crash analysis.

Visual Studio Code Plugins

Visual Studio Code is an alternative to using the Embarcadero RAD Studio code editor.

The following extensions are useful:

Check the Pascal plugin GitHub page on how to install the GNU Global source code tagging system dependencies. For Windows there is a GNU Global mingw package as part of the MSYS2 / MinGW suite. On macOS, GNU Global it is distributed via Homebrew and MacPorts.

Apart from that, refer to the language agnostic extensions in Visual Studio Code Plugins

Techniques

Unit Testing

Test Insight

Test Insight is a unit testing IDE Plugin for Delphi which eases unit test development.

After installation you can access TestInsight in the IDE by selecting View → TestInsight Explorer.

Bug: TestInsight Explorer does not consider --include and --exclude filters.

Stefan Glienke has provided a workaround here: TestInsights seems to not recognize runParam to exclude DUnitX Tests by category.

Apply the suggested fix in the file(s) C:\Users\<your-user-name>\AppData\Local\Programs\TestInsight\Source\TestInsight.DUnit*.pas matching your test framework.

Adapting your Project

In order to make TestInsight work with your project it needs to have the TESTINSIGHT compiler directive defined in the project. You can do that quickly with the context menu in the project manager.

In your application you need to use the TestInsight.<framework> unit for the framework you are using. Just call the RunRegisteredTests routine or manually register the Listener/Logger to your test framework.

Continously Testing (Watch Tests)

In Tools → Options / Fremdhersteller (3rd party providers?) → TestInsight check the Run Continous Tests Silently checkbox.

Other Topics
  • Tools → Options / Fremdhersteller (3rd party providers?) → TestInsight allows changing the ports used by TestInsight at a later point of time.

DUnitX

Installing the DUnitX Expert into Delphi

DUnitX includes a UI component.

The following installation steps are taken from the video Von 0 auf 100 - DUnitX - Ein Testframework für Delphi:

  1. Clone the DUnitX master branch: git clone https://github.com/VSoftTechnologies/DUnitX.git
  2. Open the Delphi project matching your IDE from DUnitX\Expert
  3. Right click the DUnitX_IDE_Expert_...bpl project and select Install
  4. File → New → Other now contains Delphi Projects → DUnitX → DUnitX Project and DUnitX Test Unit
  5. Select Tools → Options / Environment Options → Environment Variables and add a User Environment Variable named DUnitX with the path to the checked out sources as its value.

Notes:

  • When installing the expert, I received an exception about the expert being registered twice. I ignored it … Is it possible that the DUnitX Expert is already present in Delphi 10.4.2 Sydney?

  • DUnitX can create NUnit compatible output for use in a build server and test result monitoring environment.

Mock Frameworks

Spring4D (Spring.Mocking)
Attention

If you are using the Community Edition of Delphi, then you need to compile the Spring4D projects from within the Delphi IDE. Otherwise, the Build.exe will not create .dcu files in the Library subfolder.

  1. In the Delphi IDE create all projects in ...\spring4d\Packages\Delphi10Sydney\Spring4D.groupproj
  2. Run Build.exe with build configuration Debug and Update Delphi Registry checked for your Delphi version
Delphi.Mocks

Delphi Code Coverage

Further Information on Unit Testing

Services and Microservices

FireDAC Database Access

Accessing a SQL Lite Database via FireDAC

Step by Step: Using FireDAC from the Visual Designer

Configure the DB Connection
  1. Drag a FireDAC Connection onto the designer of your DataSource (Repository) class. Note that the right (grey) side of the FireDAC configuration dialog shows the default settings.
  2. Select Driver ID sQLite
  3. Configure Locking Mode Normal (Exclusive mode will hinder development because the running application will lock the DB. However Exclusive mode may be the best for a production ready application)
  4. Configure Database file path
  5. Caveat: JournalMode facilitates logging all activities on the DB. If you are using many insert / delete operations, the DB will grow quickly. Also inserting many records at once will take much time, if JournalMode is Persist. If you don’t need the Journal, then select Delete here.
  6. In the FireDAC Object Inspector set LoginPrompt to False. SQLite does not provide username / password authentication, but you may want to consider encrypting the DB.
Create a Query
  1. Drag a FireDAC Query onto your DataSource (Repository) class designer.
  2. From the Object Inspector dialog open the FireDAC Query Editor
  3. Configure your SQL query with parameters and design time defaults for the parameters
Prepare the Application at Runtime
  1. In the BeforeConnect event handler check whether the DB file exists and then assign the path to Connection.Params.Database.

Miscellaneous Programming Libraries

Books

[1] Marco Cantú: Object Pascal Handbook - A compendium introducing object pascal and Delphi to beginners.

[2] Nick Hodges: Coding in Delhpi

[3] Nick Hodges: More Coding in Delhpi

[4] Huw Collingbourne: The Little Book Of Delphi Programming: Learn To Program with Object Pascal

Blogs and Forums

Magazines

Criticism and Troubleshooting

After having used Delphi 10.4 for several weeks, the following issues are bothering me:

Delphi 10.4 IDE

  • The refactoring tool seems unreliable. Sometimes the refactoring is incomplete and I have to rename types or methods by hand.
    • Refactoring → Inline Variable does not work.
  • Introducing folders into an existing project is not intuitive. When trying to do so I had issues with removing an obsolete file from the project - it turned out that the project view just didn’t refresh properly.
  • In some cases, the code editor does not refresh compiler warnings once they have been cured.

DUnitX and TestInsight

  • TestInsight sometimes does not refresh test status correctly. Sometimes I need to clear the TestInsight window, re-run all tests and then only see that a test has turned from red to green.