Accessibility
navigation | page content |
Accessibility
top of site | navigation |
Latest Tutorials
Tutorials

Hands on with Delphi 2005

Tim Anderson introduces the new language elements in Delphi 2005, the biggest change to Delphi in years.
Hands on with Delphi 2005

Borland's Delphi 2005 is the biggest change to hit Delphi for years, combining Delphi for Win32, Delphi for .NET and C# Builder into one IDE. There are new language elements in both versions of Delphi, and the IDE has plenty of new features, such as refactoring, local history, code snippets, and a redesigned Tool Palette. Overall, it's a great improvement, though it's spoiled by a buggy first release and high memory requirements.

Delphi 2005 brings several products together in one IDE. These include Delphi for native Win32 code, and for the .NET Framework Delphi and C# Builder. On the face of it, it's an attractive upgrade for any Delphi user. Win32 developers get a new IDE with significant enhancements, while Delphi developers moving to .NET can easily migrate their code. Unfortunately, some early adopters of Delphi 2005 ran into quality issues concerning IDE stability and performance. Win32 developers were frustrated to find that the Delphi 2005 IDE has .NET dependencies, resulting in higher resource requirements and occasionally other difficulties.

Acting with almost indecent speed, Borland has so far issued two updates, and these improve matters considerably. While the new IDE is slower and more demanding than the old, new features like refactoring and local history make it worth switching, provided your development machine is up to the task.

The obvious choice
If you're thinking of getting hold of Delphi 2005, the next question is which edition you should purchase. This time around, Borland has made the Professional edition impressively complete, which makes it the obvious choice for most developers. You get both versions of Delphi, C# Builder, and the ability to create GUI, web and database applications. If you opt for the Enterprise version, you get better source control integration, a Star Team licence, additional dbExpress and BDP.NET drivers, dbWeb controls, and Janeva for integration with J2EE or CORBA.

The Architect edition primarily adds model-driven development, with the Together UML designer, complete with two-way code synchronization, and the ECO (Enterprise Core Objects) II framework. ECO is an interesting technology that enables you to build your application as a model and have Delphi generate most of the code. Depending on your point of view, model-driven development is either the next big thing in application development, or a fascinating but futile distraction from the real business of writing code.

advertisement
Although ECO is now in its second version, there are still some performance issues with the Together integration that it depends on. Some users have gone so far as to deliberately disable Together to improve performance and reduce memory usage. Overall, there's little point investing in the Architect edition unless you're seriously interested in the modelling aspect and are willing to put up with a few quirks along the way. It's also worth mentioning the OptimizeIt profiler, only with the Architect edition. Oddly, this is actually a Java application, but that doesn't matter if it does the job and profiling .NET applications is usually worthwhile. It's particularly important to track object creation and destruction, since despite .NET garbage collection, it's all too easy to suffer from memory leaks when objects hang around in memory, which may be due to faulty application design or even bugs in the Framework class library. Then again, there are other .NET profilers available for much less than the extra price of the Architect edition, so this isn't something to get really excited about. Now it's time for a hands-on look at some key new features in Delphi 2005.

The history view
The history view in Delphi 2005 keeps a back-up of your changes as you work. It doesn't keep track of every edit, but keeps a copy of the old version each time you save a file. The number of copies retained is configurable in the Tools | Options | Editor Options dialog. To change it, alter the 'File Backup Limit' in this dialog. Local history is not an alternative to a Software Configuration Management (SCM) system, but works alongside it. An SCM system supports multiple users and is more powerful, but it can only record those versions that you check in, whereas local history works in the background.

To try out history for yourself, start a new project, save it, and then make some edits. Save your project again, make a few more edits, and then click the 'History' tab in the editor. This view has the following three tabs of its own:

Contents: Enables you to view each revision as a complete file. It shows the last saved file as 'Local File', and each back-up as a revision with its date and time.

Info: Shows comments for each revision. Not much use unless you use Borland's Star Team SCMS, in which case, you can compare local versions with versions in the repository.

Diff: This is the most powerful view, as it shows the differences between any two versions. The one called 'Buffer' is the current unsaved version. Click the 'Next Difference' or 'Previous Difference' icon to find each difference. Each added or deleted line is displayed with a chunky '+' or '-' sign.

So, what if you messed up and want to go back to an earlier version? Just choose the Contents tab, select the version you want to restore and click the 'Revert' icon.

Refactoring
This is the business of improving code without changing its functionality. At first, this may sound like a strange thing to want to do, but there are many benefits. Perhaps it's the case that you wrote some code in a hurry and created an extravagantly long procedure that only you can understand. Well, in a few weeks' time, even you may have a hard time figuring it out, and the solution is to break it into several smaller procedures. Another common problem is known as 'clipboard inheritance', where large chunks of code have been copied into several different places in the project. One day, you'll find a bug in that code and forget to fix it in all the different copies. Once again, the solution is to eliminate the duplication by creating a separate procedure. This is refactoring and it's an essential part of application development.

Delphi 2005 has five different refactorings built into the IDE. The first is 'Rename', which changes the name of a Delphi element such as a type, field, variable, method or procedure. The trick here is to change it everywhere it occurs, and Delphi will do this for you without the need for 'search and replace'. 'Rename' can be invoked by placing the edit cursor in the target name, and then by selecting 'Rename' from the Refactor menu.

'Declare Variable' is more like a coding tool than a true refactoring. This comes into play when you're coding a method and find you need to use a variable that you've not yet declared. Type it anyway and Delphi will put a red squiggly error marking under it. Click on the error and choose 'Declare Variable'. A dialog will pop up. Here, you can choose the variable type and its initial value. Click 'OK' and Delphi will add the necessary code. 'Declare Field' is similar, but it creates a field instead of a variable, and then enables you to choose the visibility.

'Extract Resource String' converts a string literal to a resource string. Place the edit cursor in the string you want to extract, choose Refactor | Extract Resource String and click 'OK' on the pop-up dialog. Your string now appears in the 'resourcestring' section of the 'implementation' section. Delphi will compile these as resources.

'Extract Method' is perhaps the most powerful refactoring of those on offer. To use it, select a block of code and choose Refactor | Extract Method. In the pop-up dialog, choose a name for the new method. Delphi then automatically creates the necessary arguments.

This is all good stuff, but Delphi isn't exactly cutting edge when it comes to refactoring. By comparison, IntelliJ IDEA, a Java IDE, offers nearly 30 refactors. Even so, it's a good start and it's already becoming a well-used feature.

Code Snippets
Some Delphi VCL functions are awkward to remember. One example is MessageDlg, but you can create a code snippet that will save time. In the Tool Palette, select the 'Code Snippets' category and type this:
MessageDlg('The message',mtInformation,[mbOK],0 );
Next, select the line and drag it to the Tool Palette while holding down [ALT]. A new code snippet is listed. Now you can drag this back to the editor whenever you need a skeleton MessageDlg call. Remember though, cut-and-paste is bad for your program's health..

SyncEdit
'SyncEdit' enables you to change multiple instances of a Delphi identifier in one go. Imagine you've written some code that includes several references to a label control called 'Label1'. You now realise that that this is the wrong control, and you need to change all these references to 'Label2'. Instead of doing a 'search and replace', you can use 'SyncEdit'. Select the block of code and click the 'SyncEdit' icon that appears in the left margin. Click on any occurrence of 'Label1', change it to 'Label2' and all the other instances will be changed as well.

Find Unit
'Find Unit' on the Refactoring menu is the quickest method to use to locate a VCL function. Imagine you wanted to call PosEx, which searches a string in the same way as Pos, but with an option to specify the starting point of the search. Open up 'Find Unit' and type 'PosEx'. When you see the correct function, select it and choose either 'Interface' or 'Implementation'. Click 'OK' to add the relevant unit to the Uses clause in the section you chose. Incidentally, adding units to the Implementation section is preferable, because you refer to the unit's types in other Interface declarations. Moving units to the Uses clause is more efficient for the compiler, and good practice to boot.

Tim Anderson  
  PC Plus Issue 230 - June 2005