Really Bad Exception Abuse

Nick Hodges' Coding In DelphiI was reading Nick HodgesCoding in Delphi again last night (insomnia). In chapter 1 on Exception Handling he opens talking about how not to use exceptions. It got me thinking about some of the atrocious exception usage I’ve seen. I thought I would share a few examples beyond the usual exception sins.

The Try / Except / Raise Pattern

The documentation on the Try / Except pattern says that all the code after the Except block gets ran because the exception is handled. Additionally you can call Raise during the handler to send the exception further up the call stack.

I had just started a new job and was sitting with one of the senior developers to fix some bugs and get to know the codebase. He was creating a local object and so needed to free it. This is the pattern he used:

sl := TStringList.Create;
try
  // using sl
except
  raise;
end;
sl.Free;

I looked at the code for a minute quizzically, and then asked why he did that. He explained that everything after the except always gets ran, so he can be sure the memory is freed, but still let the exception bubble up to the global exception handler so it is logged. I told him that I was pretty sure calling raise in the except block prevented the code after the block from being ran. He assured me he had been writing code like this for a long time, and it always works.

I thought about it a bit more and then suggested we test the code. So we made a little test application with a message after the except block, and forced it to raise an exception, and sure enough, the message didn’t show. (Always test your theories and patterns with test code!)

There was a look of panic on his face. He’d been using this pattern for years, and it was all over multiple codebases. He claimed that the documentation must be wrong or it changed in a recent update, but I tried to assure him that is was clear and always worked that way, he just misunderstood.

Log ’em All and Let Support Sort ’em Out

This was using an early version of C# and WinForms.NET in Visual Studio. I took over a project and was having a hard time tracking down some odd behavior. Some code just wasn’t being ran, and there was no error messages. This was a project that was really, really early in the development process, and my first large scale project in C#, and I was getting really frustrated.

I finally discovered that an exception was being raised, but it was never showing up as an error. Even if the debugger was attached there was no exception message. The program just kept on running as if nothing happened, but the operation aborts. I started to question .NET’s ability to handle exceptions.

I dug and dug and finally figured out that the previous developer had setup a global exception handling and logging system that captured all the exceptions and logged them into the eventual support database with a detailed call stack. IIRC WinForms apps close automatically if there is an unhandled exception, so having an exception handler is a good idea, but silently logging them seems questionable. And then since there was an exception logging system in place the developer set the project options to have the debugger ignore all exceptions.

The result is an app running completely silent of exceptions, even while developing, but at least support will have a lot of debug information. I talked to the original developer, and he thought it was a brilliant idea, and said it is the first thing he does in all his projects. He just looks in the support database to see if things are working. His logic being users don’t want to see error messages. Not sure how hiding all the error messages is a good idea, especially hiding them from the developer during development.

Exceptions as Robust Return Objects

I was maintaining some code where a previous developer built this system where custom Exception were used to return a complex type system. He built this impressive hierarchy of exception classes, and then only certain types of exceptions were handled at different levels, where the data was needed. So instead of methods returning a result, or modifying a passed value, they all just raised different exceptions, with the properties set to different values.

There are really two issues here in my opinion. The first is raising and handling exceptions is rather expensive. A better option would have been to just create plain old objects (or even interfaced objects) and return them as results. You can still examine their types and decide when to handle them. You would just need to be aware of managing the object lifecycle (the automatic lifecycle handling was his justification for using exceptions).

The second issue is this was just one subsystem of a larger project. It was only this subsystem that used this bizarre data management system. So when you were fixing a bug here there was a mental tax of wrapping your brain around the different architecture. As soon as you figure it all out, fix the bug, and move on to other code you go back to the normal way of thinking. Again paying the mental tax of forgetting the atrocity you just witnessed.

I really believe that in a large project there should be some consistency in how the pieces are architected unless there is a compelling reason to do things different in one subsystem. That makes it easier for developers to work in different parts of the system.

So what are the craziest abuses of exceptions and exception handling you’ve seen?

By the way, Nick has a new book out on Dependency Injection in Delphi. You should pick up a copy.Nick Hodges' Dependency Injection In Delphi

Posted in Architecture, Funny | 10 Comments

The April 2017 Tokyo Hotfix Works with the new Xcode & iOS

Apple pushed out a new Xcode and iOS this week with WWDC. Naturally I updated and tried it. Xcode version 8.3.3 (8E3004b) and iOS version 10.3.2 (14F89). I’m running 10.2 Tokyo with the April 2017 Hotfix and it all worked as expected. I deployed to my iPad and the iPhone simulator (I don’t have an iPhone anymore, upgrade to a Pixel XL!)

Are you running Toyko 10.2 with the April 2017 Hotfix? There are updates coming too. Check out the roadmap to see what is planned for 10.2.1 & 10.2.2. Great new features and a lot of fixes too!

Posted in iOS, News | 5 Comments

Simple Mobile FireDAC App

The DocWiki has a great mobile tutorial on building a mobile To Do app with FireDAC and SQLite. I find I usually build it a little differently though, so I thought I would share my code here.

Go ahead and lay the UI out the same, and put down most of the same FireDAC components, with the visual LiveBindings. I love how it uses the LiveBindings wizard to create the data source. What I change is the code in the event handlers, but what you don’t need is the Insert and Delete FDQuery components. While that code all works, it uses an older model that doesn’t take full advantage of the amazing features of FireDAC.

So your form will look something like this . . .

FireDAC-ToDo-1

Add an implementation uses clause

uses FMX.DialogService.Async, IOUtils;

And then your addButtonClick event handler will look like this . . .

procedure TForm42.addButtonClick(Sender: TObject);
begin
  TDialogServiceAsync.InputQuery('New item',['Name'], [''],
  procedure (const AResult: TModalResult;
             const AValues: array of string)
  begin
    if (AResult = mrOK) and (Length(AValues) > 0) and
       (Length(Trim(AValues[0])) > 0) then
    begin
      FDQuery1.InsertRecord([AValues[0]]);
    end;
    UpdateDeleteButton;
  end);
end;

You’ll notice a few changes. First of all, I used an anonymous method for the async callback. Also, instead of using an entirely different insert query, I just call InsertRecord on the existing FDQuery1, passing in the value. For simple tables like this one this is so much easier. This really simplified the code in my opinion. Also instead of spreading the code to update the Delete button’s visibility all over the place I used a procedure called UpdateDeleteButton. Here is the rest of the code with a few comments.

procedure TForm42.deleteButtonClick(Sender: TObject);
begin 
  // Again we use the built in Delete method instead of a separate
  //   Delete query. This just deletes the currently active record.
  FDQuery1.Delete;
  UpdateDeleteButton;
end;

procedure TForm42.FDConnection1AfterConnect(Sender: TObject);
begin
  // This makes sure our table exists, just in case the data file
  //   no longer exists. Makes the app more resilient. 
  FDQueryCreateTable.ExecSQL;
  // And let's not assume anything about that Delete button status
  UpdateDeleteButton;
end;

procedure TForm42.FDConnection1BeforeConnect(Sender: TObject);
begin
  // We don't need to use a compiler directive here, but that does
  //   mean our database file is in our documents folder, which 
  //   doesn't make as much since for a desktop.
  {$if DEFINED(iOS) or DEFINED(ANDROID)} 
  FDConnection1.Params.Values['Database'] := 
    TPath.Combine(TPath.GetDocumentsPath, 'todolist.sdb');  
  {$ENDIF} 
end;

procedure TForm42.ListView1Click(Sender: TObject);
begin
  UpdateDeleteButton;
end;

procedure TForm42.UpdateDeleteButton;
begin
  deleteButton.Visible := ListView1.Selected <> nil;
end;

The beauty of calling the FDQueryCreateTable.ExecSQL is that you don’t need to deploy an empty database file. It just creates an empty one the first time it runs.

So what do you think, is this simpler? What would you do differently?

Posted in FireDAC | 4 Comments

Get Bit Episode with Holger Flick

Holger FlickNick and Jim talk with MVP Holger Flick of Korfmann Air Technology and Flix Engineering fame about smart textiles, the different software development disciplines and the future of technology. Holger also reminds everyone to see him and others at the Delphi Code Camp in Germany.

Posted in Audio podCast, MVP, podcast | Leave a comment

Rapid Prototyping Mobile Projects with Arduino and Open Hardware

These are the slides from my Mobile Dev and Test session on Rapid Prototyping Mobile Projects with Arduino and Open Hardware in San Diego. I’ll update later with links and more resources.

slides download (v0.9)

SlideShare

Visuino Links

Posted in News | Leave a comment

Some Great Women of Programming

It frequently puzzles me that programming seems to be dominated by men when the founders of programming and many of the greatest programmers were women. I read an article that this may be contributed to by early home computers being advertised as game systems for boys, which tipped the scales temporarily.

Here are a few amazing women who pioneered the computer programing field.

Ada LovelaceLady Ada Lovelace (1815 – 1852) Invented the idea of programming. Her father Lord Byron was known for his poetry. Lovelace was a poet of mathematics.

[The Analytical Engine] might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine…Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent. –As quoted by Menabrea, Luigi (1842).

Grace Hopper“Amazing” Grace Murray Hopper (1906 – 1992) invented the idea of human readable programming languages, then created COBOL. She achieved the rank of Rear Admiral in the US Navy, then had a Missile Destroyer and Cray Supercomputer named after her. The term debugging was attributed to her discovering a moth in a computer at one point. She carried around a nanosecond worth of wire to help people understand the relation between size and speed of computers.

Humans are allergic to change. They love to say, “We’ve always done it this way.” I try to fight that. That’s why I have a clock on my wall that runs counter-clockwise.

Adele Goldstine (1920 – 1964) wrote the complete technical description for ENIAC, the first electronic digital computer. And the original programmers for ENIAC were also all women: Kay McNulty, Betty Jennings, Betty Snyder, Marlyn Meltzer, Fran Bilas, and Ruth Lichterman.Betty Jean Jennigs & Fran Bilas, ENIAC Programmers

Jean E. Sammet (1928 – ) developed the FORMAC programming language, a variation of FORTRAN.

Marissa Mayer (1975 – ) employee #20 at Google, and the first female engineer. Later went on to run Yahoo.

My daughters have as much, if not more, interest in computer programming than my boys. I hope we will continue to see more amazing women in computer programming!

Posted in News | 2 Comments

Keep Threading in Mind

In my interview with Martin he talked a lot about the importance of performance, which is one of the reasons he uses Delphi for his real-time lighting control systems. He said something along the lines that if you are not programming with threading in mind then you are never going to get good performance. That is good advice. Even cell phones have quad-core processors in them these days.

Keep Threading in Mind

 

Delphi includes a great Parallel Programming Library which I really like. There is also the ever popular OmniThreadLibrary. They both make parallel programming much easier. I just learned about a new helper library to make parallel programming even easier.

CocinAsync: A Delphi library to simplify coding and improve performance of asynchronous and multithreaded applications.

It includes a number of different units and helpers. According to Jason Southwell, the developer, he has found a “100 to 150% improvement in performance over equivalent generic container wrapped in a critical section.” So how does it do this? If you look at the page the first thing you see is a brilliant couple helpers: QueueIfInThread & SynchronizeIfInThread.

We all know it is important to make sure certain code is synchronized into the main thread, unfortunately we end up with multiple code paths some and some code could be executed in the main thread via one call, and a background thread in another call. Using this helper only performs the synchronization if the code was called from a background thread.

That is just one of the helper classes included in CocinAsync. Take a look at it and keep threading in mind.

Posted in News | Tagged , , | Leave a comment

The Niagara Falls Lighting Episode

Martin SearanckeThis episode is extra special in that it include a case study of the software behind the lights for Niagara Falls. Nick and I spoke with Martin Searancke of Dream Solutions, Ltd. in New Zealand, who is the architect of the Light Factory software used by the Niagara Falls Illumination Board to illuminate Niagara Falls.  After you listen to the podcast you can read the case study write up here.

The software uses both VCL and FireMonkey. The backend control software is written in VCL and the frontend user interface side is written in FireMonkey. It also uses components by Mitov Software, TMS Software, and LMD Innovative.

A few links we mentioned

Here is a little documentary about the new lights at Niagara Falls. It mostly focuses on the hardware side of things, but you can watch it knowing the software comes from your favorite development tools.

Niagara Falls Lighting Software Case Study

Introduction

The decision to upgrade the lighting that illuminates Niagara Falls seems as though it should be the main story but, it’s not. The part of the project we are interested in is the one where the Niagara Falls Illumination Board (NFI) added a requirement to build a more flexible color illumination scheme to replace the old guillotine color-changing scheme that had been in place since 1974. The guillotine color-changing synchro-server mechanical system was to be replaced by a digital color control system and the twenty-one 4000w Xenon lights were to be replaced by 1400 digital-friendly nine-light LED modules in which the LED’s would be multi-color rather than having to place a filter in front of the white light of the Xenons. The LED system of lights would be software controlled from a 22-inch touch screen from over a 2000 foot distance from the falls.

Niagara Falls LEDs Powered by Delphi

The Solution

The technical and engineering challenges to a project of this scope and magnitude were significant right down to the last task of choosing the lighting controller and connecting it with the 1400 LED light modules. The software that drives the system is housed in the controller and is built using Embarcadero’s Delphi Powered by Embarcadero DelphiIntegrated Development Environment (IDE). The FireMonkey (FMX) application development platform is used to create the front end functionality, and the Visual Component Library (VCL) is used for the back-end services. FireMonkey and VCL are both integrated elements of the Delphi IDE.

The lighting controller was a commercially available machine marketed by Philips Strand Lighting as part of their NEO line of lighting consoles.

NEO Console running Martain's software

The existing software code in the NEO lighting console dated back a few years given that the console had been sold commercially before the Niagara Falls lighting project was even considered. The first challenge to overcome was to review the technical and performance requirements needed to accomplish the vision of the NFI and update the FireMonkey and VCL code to meet the required functionality. Then, came the customization of the code to accommodate all the connecting equipment that would drive the LED modules and the end user interface, the 22-inch touchscreen.

The overall objective of the new control console and software code within was not to try and upstage the majestic beauty of the falls but rather to organically enhance visual images of the falls at night when it couldn’t be seen otherwise. So, special effects such as strobing and other awe and wow factors were ignored in favor of techniques such as backlighting the falls to reveal hidden beauty in addition to a penetrating forward color-changing illumination. The results were nothing less than magnificent requiring new software profiles due to the color changing properties of the LED modules. The final result was ten preset modes for each side of the Falls (American and Canadian) encompassing color schemes to match naturally occurring, organic events such as the aurora borealis, sunset, sunrise, waves, and color gradients.

One unique capability that was requested to be programmed into the FireMonkey and VCL code was a user interface the allowed operators to use a touch-sensitive color-picker button on the screen to change scenes instantaneously. The LED lighting modules instantly responsive making these color changes dramatic and inspiring. A mobile interface module and application was created using FireMonkey to allow visitors and tourists to be able to manipulate the solid color palette and see the changes appear right before their eyes.

Conclusion

The Niagara Falls Lighting Project brought to light one of the real values of Embarcadero software. In this case, the Delphi IDE and its integrated parts (FireMonkey and VCL) is life-cycle engineered so that code developed years ago can be updated and reused to create an entirely new, cutting edge capability for new technologies such as LED lighting modules. This fit the Niagara Falls Illumination Board (NFI) requirements perfectly. The design of the system was mandated to done such that in the future, changes and additions can be easily accommodated. Embarcadero and its Delphi IDE proved its worth and was demonstrated to be able to meet future requirements as the Niagara Falls lighting system evolves and new technologies come online.

Niagara Falls Lit by Delphi

Posted in Audio podCast, Cool Apps, FireMonkey, podcast, TMS, VCL | Tagged , | 1 Comment

MVVM-MVC-RAD Architectures with ColumbusEgg4Delphi

Loosely coupled is good. We don’t want our business logic and data access all mixed together, that makes our code harder to maintain. But we all love RAD and being able to create our applications quickly. Drop some components on the form, set some properties and write a little code and we are done! Unfortunately we usually end up with tightly coupled code.

Most architectures I’ve seen for keeping everything separated results in needing to write all the data binding manually. You still get to design your forms with the visual designer, but missing the data binding kind of sucks.

Modern Delphi Architectures

That all changes with Daniele Teti‘s ColumbusEgg4Delphi. It lets you build loosely coupled applications where the business logic, data access and user interface are all kept separately, but you still get to use all those great RAD design tools. Daniele did a Modern Delphi Software Architectures webinar where he introduced ColumbusEgg4Delphi and explained how it works. You can catch the replay here . . . .

The first 10 minutes explains the need for ColumbusEgg4Delphi, then after that he explains how it works and shows a great example and answers questions.

Is ColumbusEgg4Delphi perfect? I don’t know, but it is a huge step in the right direction.

Check out Daniele Teti’s blog and his company site, his MVC framework, as well as his books.

Posted in Architecture | Leave a comment

Advanced HTTP Hacking Webinar Code

You can find all the code from my HTTP webinar in my special HTTP folder.

[Project source code] [YouTube Replay]

This is the script I use to demonstrate HTTP Range requests through Telnet. Just copy and paste each block of code (including the trailing blank line) into a command window and it will run telnet and make the HTTP request. You can view the test file here. Read HTTP Status Codes (including 418), Methods, Headers, and Access Control (CORS).

telnet delphi.org 80
HEAD /http/httptestfile.txt HTTP/1.1
Host: delphi.org
Connection: close
telnet delphi.org 80
GET /http/httptestfile.txt HTTP/1.1
Host: delphi.org
Connection: close
telnet delphi.org 80
GET /http/httptestfile.txt HTTP/1.1
Host: delphi.org
Range: bytes=0-77
Connection: close
telnet delphi.org 80
GET /http/httptestfile.txt HTTP/1.1
Host: delphi.org
Range: bytes=115-154
Connection: close
telnet delphi.org 80
GET /http/httptestfile.txt HTTP/1.1
Host: delphi.org
Range: bytes=78-113
Connection: close
telnet delphi.org 80
GET /http/httptestfile.txt HTTP/1.1
Host: delphi.org	
Range: bytes=115-154,127-127
Connection: close

This last one stopped working because of a change on the web server.

Here are the images. They all are available as both JPG and BMP and are 640×472 in resolution.


original.jpg

blue.jpg

green.jpg

red.jpg

purple.jpg

teal.jpg

yellow.jpg

Here is the code I used to stream all 7 images into one image. HTTPClient is a TNetHTTPClient and HTTPReq is a TNetHTTPRequest on the form. The reason it uses Bitmap images is that they are an uncompressed stream of pixel data, so are easier to recombine into one image.

const
  baseurl: string = 'http://delphi.org/http/';
  files: array[0..5] of string = ('red.bmp','green.bmp','blue.bmp',
    'yellow.bmp','purple.bmp','teal.bmp');

procedure TForm34.Button1Click(Sender: TObject);
var
  resp: IHTTPResponse;
  chunk: Integer;
  mem: TMemoryStream;
  I: Integer;
begin
  resp := HTTPReq.Head(baseurl + 'original.bmp');
  chunk := resp.ContentLength div 12;
  ProgressBar1.Max := resp.ContentLength;
  resp := HTTPReq.Get(baseurl + 'original.bmp');
  mem := TMemoryStream.Create;
  try
    mem.LoadFromStream(resp.ContentStream);
    for I := 5 to 11 do
    begin
      ProgressBar1.Tag := chunk*i;
      if I < 11 then
        httpreq.CustomHeaders['Range'] := 'bytes=' + IntToStr(chunk*i) +'-' + IntToStr(chunk*i+chunk-1)
      else
        httpreq.CustomHeaders['Range'] := 'bytes=' + IntToStr(chunk*i) +'-';
      HTTPReq.MethodString := 'GET';
      button1.Text := files[i mod 6];
      HTTPReq.URL := baseurl + files[i mod 6];
      resp := HTTPReq.Execute();
      mem.Position := chunk*i;
      TMemoryStream(resp.ContentStream).SaveToStream(mem);
    end;
    Image1.Bitmap.LoadFromStream(mem);
  finally
    mem.DisposeOf;
  end;
end;

 

 

Posted in Source Code | Leave a comment