When, What, Why!

A common pattern seen in Delphi code is the “code inside an event handler” syndrome. Lines and lines of code are placed inside an event handler. Not enough, these event handlers are often called directly from other places in the code – just because the code inside it needs to be executed. Take that as a big sign of code smell!

You know what? The event handler is not the right place for that code.

Extract it into a separate method – or probably more of them. Name those methods so that the name describes what the code executed is supposed to do. (It is probably not doing that, but that is a case for debugging.)

You can give some extended description in a comment to the method. If you have Document Insight (you should have the Express version with any recent Delphi), use it to display this comment wherever you hover the mouse over that method name.

Place the method calls into the event handlers. May be, you can write a little comment that explains why you are calling these methods. Don’t reword the method names here.

Remember: The event handler names tell you when that code is executed, the method names tell you what is executed and the comments tell you why it is executed.

BTW: Don’t try to be clever and rename the event handler so that its name resembles the what. You will have a hard time to find out the when! It would only be stored in the event wiring of the DFM and needs the Object Inspector to see it. It only makes the code harder to read. Keep the event handler names as given when possible. In case it is wired to multiple events, name it according to its usage (like DataSetsAfterPost), not its purpose.

Posted in Delphi, Programming | 3 Comments

18 Years of Delphi Programming

Some weeks after I got my Delphi 1 I had some contract work for a CAD/CAM system at a company located in my home town. After discussing a specific problem with their chief programmer (he was on C++) for the whole morning I was not able to convince him that my proposed solution will work.

During lunch break I went home, started Delphi and just wrote a prototype for proof. When I came back only about an hour after I left and showed him the working sample, he couldn’t believe it – always trying to find out how I did manage to cheat.

A few months later he quit…

Happy Birthday Delphi!

Posted in Delphi, Programming | 2 Comments

Do you know Build Groups?

Can you make a build in one step? (It is #2 in The Joel Test, but that is not a main topic of this article.) If you can, you have probably set up a clean build system for doing daily builds (#3 of The Joel Test) or even continuous builds based on a source control system (#1 of The Joel Test) and some of the many really useful tools available. The build system (hopefully) takes care to make sure that your sources are at least compilable for the different platforms and different build configurations as they are available in more recent Delphi versions.

You also do Smoke Testing before you check in, don’t you? There is no reason to break the build just for being lazy. But here things may become somewhat complicated, at least when you are developing for different platforms and some of the target platforms are currently not available in your working environment. But the least thing you can do before checking in is to make sure it will compile – for all platforms and all build configurations and whatever combination of those is feasible!

Nerd

The Delphi IDE actually provides the necessary buttons to change the platform and build configuration for all projects in the current project group with only a few mouse clicks, but even that might sum up to plenty of clicks and actions, which is as tedious as it is error prone. Here Build Groups come to the rescue.

Build Groups are a lesser known part of the Project Manager. They are useful to compile, build or clean selected projects of a project group  for different platforms and build configurations in one step.

To make use of them you have to click onShowBuildGroups.bmp Show Build Groups Pane inside the Project Manager. This will attach a new window inside the Project Manager where you can manage your build groups.

BuildGroupsPane1

For illustrations I am using the DataSnapDemos sample from Delphi here, but this should work with every project group.

First thing to do is create a new build group. Click the appropriate button CreateBuildGroup.bmp, enter a decent name and you end up with a list of all available projects from the current project group. By default each project comes with its current build configuration and platform.

BuildGroupsPane2
SelectConfigurations

Now click into the Configurations column of the DelphiDataSnapServer project. A second click will switch to edit mode which unveils an ellipsis button. Click it. From the configurations available for this project select the ones that should go into this build group.

Do the same for the Platforms column. You are prompted with a list of all platforms for the selected project.SelectPlatforms

After selecting the required configurations and platforms for each project in the group, you can compile, build or clean the whole pack with only one click. Very handy to make sure your changes actually compile when you are working with lots of {$IFDEF Debug}.

 

Oh, btw: You can have multiple build groups for different purposes or to target only part of the projects. Just select the projects you need.

 

Posted in Delphi | 4 Comments

DataSnap in the Cloud

Last weekend I spoke at the Delphi Tage in Heidelberg about DataSnap In The Cloud and I promised to provide the referenced data for download. So here it is:

DataSnap in the Cloud (PDF)

The slides from the presentation. I omitted those slides serving transition effects only.

FishFacts (Project)

The modified FishFacts project. The console application project is called FishFactsServer.  It contains the changes to make it work with SQL Database in Azure as well as the patched Data.DBXMSSQLMetaDataReader.pas (XE2) to make it work with SQL (Azure) Database.

Azure Cloud Service

The Azure Cloud Services Project. You have to copy the FishFactsServer.exe into the WorkerRole folder after compilation and before making the package.

DbDemos SQL Script

SQL Script for DbDemos Database. Executing this script in SQL Managment Studio will create and populate the complete dbdemos database in a format suitable for SQL (Azure) Database.

 

Posted in Delphi, Programming | 3 Comments

Connecting to SQL Azure with dbExpress

Did you notice that there is a free trial of SQL Azure? Well, I stepped into it – just to gain experience. Continue reading

Posted in Delphi, Programming | 1 Comment

A DUnit Folder Iterator Extension

You have unit tests, don’t you? Or at least you run some tests to make sure your recent changes broke something?

These regression tests are vital to any serious software development. What would your customers say when they get a new version breaking their day to day work? Well, at least I can’t afford that – and be it just that I’m not keen to fix those problems at a customer under high pressure. So I have regression tests. Continue reading

Posted in Delphi, Programming | Comments Off

Windows 7 Previews – the Delphi Way

In the ancient days of Windows XP and Delphi 7 I used to provide a preview of the proprietary file content inside the file open dialog of one of my applications. This was accomplished by deriving from TOpenDialog, just like the TOpenPictureDialog does that comes with Delphi itself.

The drawback was that since Windows Vista this was no way to go when you wanted to use the new Vista style dialogs. This was an even more nuisance as the new dialogs already come with a preview ability – so why not use it directly? Continue reading

Posted in Delphi, Programming | 12 Comments

On Upgrading Delphi

Back from Delphi Developer Days (Kudos to Marco and Cary) and after cleaning up the mess in my inbox I would like to share some thought I had during one of the sessions named “Delphi: Today and Tomorrow”.

Some of the attendants mentioned they were still working with older Delphi versions and even if the newer versions were laying around they refused to make the step migrating their applications to the current Delphi XE. One argument from a fellow developer working still with Delphi 2007 actually alerted myself as he rated the step from Delphi 2007 to Delphi 2009 a very big one regarding the Unicode migration, but the step from Delphi 2010 to Delphi XE seems rather small, which makes himself unsure wether to make the migration to Delphi XE now or wait for the next version to come.

Honestly, I don’t get that. There already is a large step from Delphi 2007 to Delphi XE. Why making it even larger? What about “divide and conquer”? Do you really expect the migration to XE2 being easier than that to XE? Continue reading

Posted in Delphi, Programming | 28 Comments

Synchronize and Queue with Parameters

In Delphi’s TThread class there is Synchronize to call a method in the context of the GUI thread. This is necessary in case, for example, you want to update a progressbar or a status label in a form, because the VCL is not thread safe. While Synchronize is a blocking method (i.e. the thread code continues when the GUI thread has finished the method call), recent versions of Delphi introduced a non-blocking Queue method.

The drawback of using Synchronize is that it is very cumbersome to handle parameters during such a call. The standard solution is to use fields in the TThread descendant holding the parameters during Synchronize, but this won’t work properly with Queue. Continue reading

Posted in Delphi, Programming | 12 Comments

The Visitor Pattern – Part 4

The interface approach introduced in Part 2 of this series and extended in Part 3 did cover most of our needs. But there is nothing that can’t be done better with the right tools.
Continue reading

Posted in Delphi, Programming | 4 Comments