Flotsam and Jetsam #94

Flotsam and Jetsam #93

  • In my post about my new book, I mentioned that I thought my outline was a little thin, and indeed it was.  I left out a large topic about which I planned on writing – “Writing SOLID Code”.  Add that in, and things seem a bit more fleshed out.  The silly thing was that I was working on that chapter when I put together the outline and wrote the blog post.  Anyway, the outline has been updated and all is well.  Thanks again for the support.  If you want to keep up with the book’s progress, help out answering my silly questions, and generally be supportive, you can sign up at the LeanPub site, or you can join the Google Group
  • Here’s some food for thought for my post about not using nilThe Null Object Pattern.  Yet another reason never to return nil.  Instead of doing that, you can return a null object that won’t cause an access violation and yet won’t do anything.  You can check if the object is a null object if you need to.  Win all around.
  • So the rumors were true – Embarcadero has bought the main products of TwoDesk Software.  Jacob Thurman has stated that while he’s not now an Embarcadero employee, he will be working on the Delphi IDE.  I guess it’s none of our business exactly what the relationship will be, but if Jacob is working to improve what the IDE can do with code and to improve developer productivity in the Code Editor, I’m very happy.  I know that Jacob has had a lot of things that he wanted to do, but found it difficult as an “outsider”.  Now that he’s an “insider”, I hope he can do them.  I think we’ll see some fun stuff coming out of this deal. Congratulations to Jacob, who I hope was well remunerated, and to Embarcadero for the wisdom to see and acquire Jacob’s skills.
  • True words tweeted by me: “There are two things that developers love to do: Make pronouncements, and point out the exceptions to other developers’ pronouncements.”  I like to make pronouncements as you’ve probably noted.  I’m always trying to learn new things and new ways of doing things, and I don’t want to be held back by the “well, we’ve always done it that way” way of thinking.  I believe that the way software is developed is evolving and improving, and that we’ve found new ways of doing things better.  I try to learn those things and blog about them.  That’s what I’m trying to do here.  If you disagree with what I say, I welcome your comments. But I ask you to consider that maybe I’m actually on to something.  I’d encourage you to take the same path I have – the path of learning these new things that developers are finding and doing.  For instance, I don’t ever intend on writing another Delphi application without utilizing MVVM.  But ten years ago, such a thought would have occurred to almost no Delphi developer.  I encourage you to study the SOLID principles, to read Clean Code, to find better ways to decouple your code, and to generally seek out new thinking about software development.  I’m a firm believer that there is always a better way, and I encourage you to think so as well.

On the Use and Acceptance of nil

Yesterday I made the following tweet:

and it started and interesting little discussion on Twitter.  (I was actually honored that the great Mark Seemann himself entered into the fray…) Naturally, the conversation tried to point out why I was wrong, and all the ways that it was fine to use nil.  That’s okay – if there it is one thing developers like better than making pronouncements, it’s finding exceptions and arguing against other developer’s pronouncements.

Well, I remain unconvinced and stand by my statement.  No surprise there, I know.

There are two parts to what I said and I’ll argue them one at a time.

First – “never pass nil to a method”.  Now people freak out about “never”, but it’s Twitter and you don’t have a lot of room for caveats.  First, sometimes I guess you have to pass nil to some methods  — ShellExecute  comes to mind —  but I’d argue that those methods are poorly designed (as I’ll talk more about in a minute), and that you should pass nil only very, very reluctantly.  Again, I’m talking about your code, not other’s code.    You should never send nil to a place where a valid object will be accepted.  Never do that to someone’s code.  Returning nil is always a bad design decision.

The Robustness Principle states, in part, that you should be very precise in what you pass, and passing nil is not precise.  It misses the mark by as much as you can miss the mark.  The target is a valid, working instance of whatever the method is asking for, and passing nil is like turning 180 degrees around and firing your arrow away from the bulls-eye.  Nil should not be used as a “signal”, leaving the class you passed it to in a state where they can’t make use of what you’ve given them.  You should avoid like the plague allowing a  class to be in a state where it can’t do what it is designed to do, and if it asks for a object in a method or constructor, you are obligated to pass a working, constructed object that it can use without error.  To do anything else is to invite an exception or worse, an access violation.

The second part is this:  “Don’t let your methods – especially your constructors — accept nil as a parameter value”.  Your classes should never let themselves get into an unusable state, and blindly accepting nil as a parameter will do that.  Instead, your classes should carefully guard against being passed nil and quickly – no, immediately – fail if passed a nil reference.  There is even a pattern – the Guard Pattern – that is employed to ensure that a program won’t continue unless things are acceptable.  Getting nil is not acceptable:

    procedure TMyClass.constructor(aSomeClass: TSomeClass);
    begin
      if aSomeClass = nil then
      begin
        raise NoNilParametersException.Create('Don''t you dare pass me a nil reference.  Pass only valid instances');
      end;
      FSomeClass := aSomeClass;
    end;

Every single time you accept an object as a method parameter, you need to use the Guard Pattern to prevent your object from being placed in a bad state where it can’t do what it is supposed to do. Fail fast and tell the user exactly what they did wrong and what they need to do to fix it. In fact, if a nil reference sneaks in, you’ll eventually get an access violation when you try to use this nil reference, right? Sure, it’s work, but it is time well spent.  Knowing that your references are always valid makes for easier code, and actually can reduce the amount of nil checking that you have to do.  Who wants to check for nil every time you use an object?

Don’t pass nil references.  Don’t accept nil references.  This seems obvious to me and I confess I don’t understand objections to this aphorism that only results in cleaner code.   Why would you ever want to accept a nil object?  Why would you want your code in a state that is begging for an access violation to occur?  Insist that your objects are valid, and never impose invalid objects – nil – on others.  Seems blatantly obvious to me.

So I am going to guess that this will not make some of you happy.  So here is my challenge:  Show me code that you have written where nil is acceptable.  Don’t show me code that calls the Win32 API or the VCL, show me code you have yourself written where nil is acceptable and useful as a valid method parameter.  I’d love to see it – and then point out the problems that it will cause.  🙂

Delphi and #Code2014

Hey, Delphi ended up really well in the #code2014 survey.  Sure, it’s self-selected and thus statistically dubious, but it definitely is nice to see Delphi in ninth position with a prominent circle.  (If you add Delphi and Pascal together, we’d be in sixth place.  Yes, I can see the comments already…)

If I were part of EMBT’s marketing team, I’d be shouting this one from the mountain tops.  I know that technically it’s not statistically significant, but the whole chart “feels” right.  The languages at the very top seem like the right ones.  And Delphi at the ninth position seems right too.  Why?  Because I think there are a lot more Delphi developers out there than people know.  Is it more popular than C/C++?  Maybe not, but then again, maybe it is.  No way to know. 

The part that bums me out is the community members that are so quick to point out that the survey “doesn’t mean anything”.  I most strongly disagree.  I think it means a lot.  I think it shows that the Delphi community is real, it’s bigger than folks think, and it’s willing to speak up and be heard.  All I know is the Delphi community sure came out of the woodwork and made Delphi rise into a prominent position in a poll that many developers will see. That can’t be bad.  It is good.

If you are a Delphi developer, you should be happy about this, and you should be doing what you can to show people the results.  I took a screen capture of the results in case the URL goes away (www.code2013.com is for sale for a mere $3095).  If you are a Delphi developer and are poo-pooing the results, I say “Lighten up, Francis”. 

This is good.  Enjoy it.  Use it.  Delphi is still here and going strong, almost twenty years after it launched.  We’ve had our ups and downs, but the bottom line is that you have nothing to be ashamed of when you say “I am a Delphi developer”. 

Flotsam and Jetsam #92

  • We have a vibrant .Net community here in the Eastern Pennsylvania/Philadelphia area.  Philly.Net runs events once or twice a month at the Microsoft campus, and semi-annual Code Camps that are usually attended by well over 700 people.  I attend whenever I can.  But I’ve noticed something interesting over the last year or two:  almost all the coding topics are now Javascript-based.  I see them doing very few C# – based presentations.  This event is pretty typical.  (I am going to be attending this one, by the way…..) Anyway, just an observation that I think is interesting.
  • If you aren’t satisfied with the RAD Studio IDE’s bookmarking system – and you shouldn’t be as it leaves a lot to be desired – you might be interested in the free IDE plugin called Parnassus Bookmarks from David Millington.  It works great, is totally free, and is a solid replacement for the existing system.   Download and install is recommended.
  • Well, Delphi has done really well in the #Code2014 survey.  Thanks to everyone who tweeted.  It’s nice to see Delphi up near the top in a very respectable position.  As of this writing, Delphi is only 19 tweets behind C++ – nothing to blow your nose at.    Javascript continues to dominate in a pretty big way, nearly doubling the second place Python.  (See my comment above).
  • New Project of the Week:  Another gem from the inestimable Malcolm Groves – TStateMachine.  From the README:  “TStateMachine is a framework for declaring and running state machines in Delphi. It supports generic types for states and triggers, anonymous methods for Guards and Effects (State.OnEntry? and State.OnExit?), and a Fluid interface for configuring the State Machine.”  Very interesting and worth a look.  Maybe even worth a new chapter in my book.
  • Cary and Ray have set the agenda for their Delphi Developer Days conference.  Again, this is always a high-quality event, and I try to make it every year.  Word of advice: Don’t ever miss Ray Konopka when he is speaking.  He’s very, very good.

More Coding in Delphi

First of all, I want to once again thank all of you who have purchased, read, or otherwise supported my book Coding in Delphi.  I’ve said it before and I’ll say it again  — the response has been overwhelming and humbling.  The book has been more successful than I could have ever hoped for.

It’s so successful, in fact, that I’ve decided to write another one – More Coding in Delphi.  I am once again using the LeanPub publishing platform.  LeanPub is really an interesting tool – it makes it really, really easy to produce a book. Highly recommended.  You write the book in Markdown, and they take that and turn it into PDF, MOBI, and ePUB files for you.  Easy as pie.  I’ve been using MarkdownPad for writing the book.  It does a nice job of helping you write Markdown text. 

By default, LeanPub uses DropBox.  First, I granted LeanPub access to a shared folder on Dropbox.  Then, when I go to publish a preview or the real thing, LeanPub looks in that folder for a file called “Book.txt” which describes the format of the book by pointing to *.md files.  It then accesses those files in the shared folder and creates the book. 

This time around, though, they’ve added a new feature where you store the book in GitHub and they pull from the repository to get the files for the book.  Cool.  When the book is ready for proofing by any of you interested in doing so, you’ll get access to the manuscript by pulling it from the private repository on Github.  So if you want to help out, you’ll need to brush up on your Git. SmileIt also means that I get the benefits of a DVCS, and you guys can make pull requests if you have improvements for me. 

Right now, the outline for More Coding in Delphi is currently:

  • Four Thoughts on Coding Before We Start
    • Encapsulation, CQS, Postrel’s Law, Compostion over Inheritance
  • Duck Typing
  • JSON
  • Aspect-oriented Programming
  • Design Patterns
    • Observer Pattern
    • Adapter Pattern
    • Decorator Pattern
    • Another Pattern to be named later
    • Another Pattern to be named later
  • Parallel Programming
  • Model-View-ViewModel

That feels a little thin to me right now, so I am seeking more topics.  If you want to suggest anything, I’m all ears — either comment here on this post, or join the Google Community for the book.  Remember the philosophy of the book, though – it’s all about the code.  I’ll write about code frameworks, RTL type stuff, etc., but I don’t want too many screen shots of forms in the book.  There will be some in the MVVM chapter, but the idea is that the book covers code and how to write good code.   That’s what I want to focus on.  I’m also open to turning “Four Things” into five or six or seven things, so if you have small topics – a page or two, not enough for a full chapter — I’d love to hear those as well.

Okay, so now the real work begins.  It’s a lot of work to write a book. My hope is to have the book published by the end of this year.  I know that I can’t do it without your help – you guys made the first book possible.  Again, I’m grateful to all you readers and supporters. 

Flotsam and Jetsam #91

  • It’s #Code2014 time again! Time to tweet out what languages you used for the year.  Just list your languages and include the hashtag `#code2014`, and they’ll do the rest.  Of course, you can include Delphi all you want.  I won’t mind.
  • Looks like Ray Konopka will be replacing Dr. Bob this coming year for Delphi Developer Days.  If you haven’t heard Ray speak, he’s excellent.  Really excellent. I’ve been to DDD three or four times, and it’s always great.  Adding Ray this year will maintain the high standards that it has always had.  Looks like Dr. Bob is just taking some time off from traveling, and Ray is stepping in.  Sounds great.  And as Cary notes in his blog post, you can get a reduced rate if you register early!  Smile
  • In a recent post, I talked about `IncludeTrailingPathDelimiter` and why you should always use it with your path strings.  I should have mentioned that there is also a `ExcludeTrailingPathDelimiter` that is part of the RTL, too, which does what it says – takes off the trailing path delimiter if it is there.  I’m not sure why you’d want to do that, but hey, there it is. 
  • Happy New Year, everyone.  I hope that 2015 has everything in it that you want and hope for.  In case you were wondering, my New Years Resolutions are to finish my new book and 1920×1080.

Never Assume Your Paths Have Ending Delimiters

I think I’ve mentioned that I have a “challenging” codebase that I deal with in my day job.  It’s a classic Delphi legacy app, worked on by numerous different people over the years with multiple different styles and techniques. Part of the product includes the use of an Address Engine – a third-party utility that “scrubs” addresses and ensures that they exist and that they are in a Post Office acceptable form.

Recently, we’ve had to change Address Engine providers.  It should have been easy – I had created an interface to isolate the engine before the switch.  So I got the new engine all plugged in, ready to go, and it all should have worked.  But try as I might, I couldn’t get anything but garbage data out of the engine.  Argh.  It simply wouldn’t return any data.  Now mind you, the DLL in question was written in C, and I had a translation of the *.h file that I was pretty confident in.  It all should have worked.

But the problem was much simpler than that – the `*.ini` file that had an entry for the path to the postal data was missing an ending backslash.  That was it.  I put the trailing backslash on the entry (why didn’t the installer do that?) and it all worked.

Which brings me to the point of this blog post:  Always assume that your paths don’t have that trailing delimiter, and that it is your responsibility to put it there.

In fact, the RTL provides a routine that allows you to do that very thing:

function IncludeTrailingPathDelimiter(const S: string): string; overload;

`IncludeTrailingPathDelimeter` will place either a backslash or a forward slash on the end of a path, depending on the operating system’s definition of a path delimiter if there isn’t one there.  If there is, it will do nothing.   You should use it to ensure that every path string you use has a delimiter on the end of it.  Your code should thus assume that all paths have that trailing backslash on it.  If you have code that looks like this:

MyFilenameWithPath := SomePath + ‘\’ + ‘somefile.txt’;

this should be changed to

MyFilenameWithPath := IncludingTrailingPathDelimiter(SomePath) + ‘somefile.txt’;

The former code can be buggy – what if `SomePath` already has a trailing delimiter?  You’ll end up with an invalid path.  The latter code has no such problem.  It is safe both ways – with and without the trailing delimiter.  This is especially necessary when your paths come as a result of user input, such as that `*.ini` file above.

The moral of the story:  Never assume that your path variables have a trailing delimiter, and always wrap them up with `IncludeTrailingPathDelimiter` to ensure that they do.

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.