Category Archives: Tips and Tricks

Tips and tricks how to avoid common mistakes or how to generally improve your code.

GlobalConsts.pas

Today, I found another example of a common mistake, a unit with global constants. I guess most of you know this situation. There is a value that is used several times in a unit and you declare a constant for … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on GlobalConsts.pas

ObjGuard

Sometimes it happens that I would like to convert a Delphi object to an interfaces. Interfaces offer some advantages. On the one hand you can handle different objects that are not derived from a common hierarchy but share the same … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on ObjGuard

More About Records

Since records offer methods and properties they are often used to replace objects. This is possible if there is no need to use inheritance. But there is a significant difference between both. References to objects are like pointers to a … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on More About Records

TPath.Combine

I guess that everyone already had to solve this small issue. There are two variables, one with a path, the other with the filename and they have to be combined. In the old Delphi world there is the function IncludeTrailingPathDelimiter … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on TPath.Combine

StringOfChar

This week I found a new interesting piece of code. Someone wanted to initialize a string with a special length of the same char. procedure TMyClass.Init; const cBufferLength = 100; var I: Integer; begin … FBuffer := ” for I … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on StringOfChar

(Ab)use of Try..Except

Today I found an example of the abuse of a try.. except block. procedure TMyClass.DoSomething; var sValue: string; iValue: Integer; begin … try iValue := StrToInt(sValue); except iValue := 0; end; … end; The idea is simple. StrToInt converts a … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on (Ab)use of Try..Except

Records

Because of my post about the Switches I would like to mention some things about records. Records are are nice way to do things in an object-oriented way without the overhead of real objects. In the unit System.IOUtils you can … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on Records

Naming of Constants

Normally constants improve the readability and maintainability of your code. One example is the minimum value of a single. You can find it in the unit System.Math: … const MinSingle = 1.4012984643248170709e-45; … Instead of writing the float value it … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on Naming of Constants

Switches

During the last weeks it happened often that I had to write some command line apps. The reason for this is that more and more things are running automatically in tools like FinalBuilder (I will write an article about this … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on Switches

try…finally for multiple objects

Today I found another common mistake. Someone used more than one object and has to free them. var pObject1: TMyObject1; pObject2: TMyObject2; begin pObject1 := TMyObject1.Create; try pObject1.DoSomething; pObject2 := TMyObject2.Create; try pObject2.DoSomethingElse; finally pObject2.Free; end; finally pObject1.Free; end; end; … Continue reading

Posted in Tips and Tricks | Tagged | Comments Off on try…finally for multiple objects