Flotsam and Jetsam #90

  • I mentioned this video before, but it is so good I’m going to mention it again:  Parallel Programming Library: Create Responsive Object Pascal Apps.   Danny’s blog is in Dutch, but he has an English entry that includes the source code to his session.
  • Something I’m surprised by:  The number of questions on the Delphi StackOverflow tag that deal with integrating with Excel.  Also, tons of Indy questions.
  • This is interesting: Jon Aasenden is creating DelphiArmy – a real, no kidding jobsite for Delphi developers.  I’ll be interested to see how that goes.  It appears to be alive and running, so make an entry if you need to.
  • I’ve mentioned free this tool before, but its usefulness is so awesome that I wanted to point it out again.  It’s called “Everything”, and it allows you to search for any file by name on your entire system.  It’s lightening fast, and I use it probably twenty times a day to find things I’m looking for.  It’s one of the first things I install on a new system.  Truly outstanding and amazingly useful.  Highly recommended. 
  • While I’m at this “mentioning before” thing, I’ll give a strong endorsement to the Delphi-built Clipmate. Again, I use this thing about 25 times a day.  It’s a clipboard cache that remembers what you’ve put on the clipboard.  It’s not free – but it’s well worth the price.  Saved my butt a thousand times, too, when I overwrite something on the clipboard that I didn’t want to. 

Flotsam and Jetsam #89

  • A little while ago, I wrote an article on VSoft’s command line arguments library.  Since then, I’ve discovered a couple of other solutions that might be of interest.  First is from John Kaster, TCommandParser, which has an EDN article (it’s mentioned in there, don’t worry) and a CodeCentral entryPrimož Gabrijelčič has created one that uses attributes.  And this StackOverflow question has a number of other solutions as well. 
  • Speaking of command line parameters, how about someone create a default Console Application that has command line processing built right into it.  You could go “File|New|Console Application” and have a new console application with command-line options.  Maybe there could even be a wizard to define the command line parameters that your console application would have.  Just and idea – I don’t have the time to do it right now.  🙂
  • A while back, Marco posted about the “TurboPack”, a set of open source libraries over which Embarcadero appears to be taking ownership.  I was glad to see SynEdit on that list, as I use that in an app I built, and have always found its varying versions confusing.  I hope EMBT is able to tighten that up.  Anyway, Marco now has a short video on his blog that demonstrates some of these components working.  Nothing big, just a quick look.  I think this is a good initiative on EMBT’s part, and so it would be good for those of you that agree to show support as well.  You can find links to the code on Roman Kassebaum’s blog. I guess we owe Roman a Thank You as I bet he did most of the work.
  • Clearly the focus of the last few releases of RAD Studio have been on mobile development. Some folks have complained that the VCL developer has been left behind.  It seems that EMBT has been aware of this feeling, and has been emphasizing that it isn’t true – that there have been quite a few developments for VCL developers, including REST client components (which are remarkably easy to use), app tethering, taskbar components, the Parallel Library, and more.  They also have a webinar coming up entitled “Modernize Your VCL Applications Today”.  Yes, the focus hasn’t been 100% on the VCL as in days gone by, but the VCL hasn’t been ignored by any means.  Quite the contrary.
  • Book I’m ReadingObject Thinking by David West.  The idea here is that even though we are all using OOP languages now, we are really doing procedural programming inside the world of OOP.  West discusses how we need to start thinking totally differently about our code – to think about objects.  Interesting so far – not your typical coding book.

Flotsam and Jetsam #88

  • The inestimable Malcolm Groves has a nice series going on the new Delphi Parallel Library.  Recommended reading.
  • I put a tag cloud over there on the left.  Now I just have to remember to put tags on all my posts.
  • I welcome feedback on the new blog.  As I said, my goal is to keep the blog as simple and clean as possible and the content as interesting and useful as possible.
  • I guess I should have realized that such a thing would exist, but I just found the Delphi Reddit.  Thanks to folks that post my blog entries there.
  • Another entry in the “How did I miss this?” department:  A guy named Branko Stojakovic has built a blog covering most of the Gang of Four patterns.  Maybe I did mentioned this before – I have a dim memory of it – but anyway, worth looking at and learning from either way.

Command/Query Separation


An important step to writing Clean Code is the notion of separating “commands” and “queries” by using the Command Query Separation Principle.  The notion was first discussed by Bertrand Meyer in his book Object Oriented Software Construction.  This means that the idea is not new.  In its basic form, it means we should separate the things that read data from the system and things that write data to the system.

The Command/Query Separation Principle means that there should be a clear separation between the updating of information and status in your program and the way that you read information from the program.  Commands and queries should be separately declared (though you’ll find that commands can call queries but not vice-versa.)  It can be a complex as ensuring that reading and writing take place in completely separate object, or as simple as ensuring that commands and queries are simply done in different methods of your objects.

Of course, the first question you’ll have is “What do you mean by ‘command’ and ‘query’?”  Well, I’ll tell you.


A query is an operation that returns a result without effecting the state of the class or application.  In Object Pascal, this is typically a function.  Queries should not mutate the state of a class.  They should be idempotent.   That means “denoting an element of a set that is unchanged in value when multiplied or otherwise operated on by itself.”  (I had to look that up, by the way….)

As a general rule, queries should return a single value, and “asking a question should not change the answer”.  They should be referentially transparent; that is, they should be perfectly replaceable with their literal result without changing the meaning of the system.

What this means practically is that your functions shouldn’t change the status of the system you are working on, whether that be a class, a framework, or an application.  You should be able to run a query, i.e. a function, a hundred times in a row and get the same answer back each time.  The function, because it doesn’t change the status of the system, can be safely called at any time without repercussions.

Now this should be a general rule — there are certainly cases where your query will change the state of the system (a dataset’s Next call comes to mind).  But generally, it’s a good idea to have your queries not change state.


A command is any operation that has an observable side-effect.  It is any code that changes something in your class or application.  Typically, in Object Pascal, a command will be a procedure — that is, code that takes actions without returning a value.  Commands can call queries (but queries should never call commands, because commands change the status of the system).

Commands should not in general return values.  Thus, the use of var parameters should be discouraged if not down-right banned.

Don’t Mix the Two

All your methods and routines should be easily identifiable as either a command or a query.  Commands and queries should be separate entities in your code — with the exception that a command can call a query if need be. The use of var or out parameters in a procedure will confuse this issue, and thus should be discouraged.   If you follow this rule, your code should be more “reasonable” — it should be easier to understand and easier to modify.

CQRS also encourages you not to violate what I consider to be a bedrock of sound development technique:  Don’t try to make one thing do two things.   For instance, here is some code that does exactly that:

This method is a clear violation of CQS [NOTE:  I orginally had this as CQRS] as it obviously is trying to be a command and altering the state of the system by processing widgets, but also tries to be a query by “returning” through a var parameter the number of processed widgets.  Instead, the system should have a simple command to process widgets and a separate query to return the number of widgets that were processed.  The procedure is trying to be two things at once, and all kinds of mischief comes from making one thing do two things.

Following the CQRS principle in the design of your code will help to ensure the proper separation of concerns, resulting in cleaner, easier to read and easier to maintain code.

Obey Postel’s Law in Your Code

Jon Postel was a guy that wrote an early specification for the Transmission Control Protocol (TCP), one of the core protocols of the Internet protocol suite.  You use it every day to surf the web, send emails, etc.  One of the guiding principles that he used when writing it was this:

Be conservative in what you do, be liberal in what you accept from others.

This idea is also call the Robustness Principle, and is sometimes rephrased as “Be conservative in what you send, be liberal in what you accept.”  When applied to TCP, it means that the sender of data should be strict in what is sent, ensuring that it is accurate and precise.  It also means that the receiver of data should be forgiving and understanding of data to as large a degree as possible.  If you send data, be as clear as possible in what you send.  If you can accept the data sent, then you should.

Any application programming interface (API) should follow this principle.  Thus, the same principle should apply to your code.  The public interface of your class should be viewed as an API, and it should be conservative in what it sends, and forgiving in what it receives.  When calling another API, your code should send out data in a completely conformant way, following the rules laid down by the receiving API.  But it should be willing to receive input in a non-conformant way as long as the input can be understood.

For instance, if passed a string, you might be happy to accept strings with blank spaces on the beginning and end, and use the Trim function to clean things up for the sender.  Your classes might provide overloads for input methods, accepting both and integer and a string as input, providing a way for your class to be as forgiving as possible.

But when you call another API, your code should be strict and always send data in the correct, expected form.  You should trim your strings before they get sent along to the API you are calling.  If the system expects integers, you are going to send integers.  You should meet the specification completely.

Another example is the use of nil.  First, your code should never pass nil to a method that you are calling.  Always provide a valid instance if the API calls for one.  Second, your code should accept nil, but “fail fast” if passed nil and raise an exception.  You should never let your internals get into the state of nil, and while you should accept nil, you should immediately raise an exception at any attempt to set one of your internal references to nil.

Postel’s Law – a small but important way to write better code.

Flotsam and Jetsam #87

    • Yet again, I’d like to thank all of you who have bought my book or received it as an upgrade incentive.  Once again I’ll say that the book has been more successful than I ever imagined.  So successful, in fact, that I’m going to work on another one along the same lines.   By that I mean that it will be a book focused on code and not the building of UI’s or other such applications.  Keep your eye on this space for more information.  And thanks again – I’ve really very grateful for all the support.
    • Blog Update: I’ve added Disqus as the default commenting system here.  Seems really popular and cool, and I love to be popular and cool.  However, it may have screwed up some of the early comments.   I’ve also added a barrage of share options on each post, so share away.
    • I hope you all can see this – it’s a Facebook link and I have no idea what their rules are – but this post led to an interesting conversation.  The link about MS tools that spawned the conversation is interesting as well.
    • Malcolm Groves does a great job explaining one of the more difficult things to understand in System.Threading.pas: IFuture<T>
    • I have thoroughly enjoyed having Castalia as a free part of my IDE as a promotion with Delphi XE7.  Good news – an update is coming.  And this new feature looks, well, amazing.

Flotsam and Jetsam #86

  • Delphi XE7 Update 1 is available.  Nice. The release notes have the link to where you can get it.
  • I’d like to thank the great staff at EKON 18 for a great conference and a great trip.  It was fun (despite the fact that I lost my phone and felt like a lost puppy without it).  The conference was interesting, and I got to see old friends like Ray Konopka, Cary Jensen and Loy Anderson, Marco Cantu, and Sebastian Gingter.  It was a great conference, and I was honored to be a speaker.  Thanks to everyone who came to my talks.
  • I also presented at another great CodeRage.  I talked on enumerators and IEnumerable<T> from the Delphi Spring Framework.   The replays are available online – or at least many of them are as they post them. I see that currently mine isn’t – I think that’s because I have to redo the Q&A portion.  Stay tuned.
  • Book Update:  An e-book version of Coding in Delphi is available to you free – all you have to do is activate the trial of XE7.  Good deal, eh?
  • Apparently Delphi had a small but important role in the ESA Rosetta Project – you know, that spacecraft that actually landed on a comet.  Pretty cool. 

Testing My New Code Formatter

I am using the Crayon Code Highlighter.  Let’s see how it works:

Looks pretty good I think.



Hello World from my new blog!

This is my new blog.  My goal for it is to keep the design simple and the content interesting.