You know those crazy books, "Learn whatever programming in 21 days"? I mean, who can afford spending that much time, right?

Some background

I have a friend who employs a very particular workflow for dealing with his digital photos. It often involves renaming and merging files from different cameras into a single chronologically ordered event, relying on natural sorting of file names in Windows Explorer. File names are constructed of picture time fields and running counters, like "2015-02-06_001.jpg".

This is of course too tedious to do by hand, so he was very happy with a small specialized Windows utility that I wrote for him a few years ago when Windows XP ruled the world and I still programmed in Delphi. The program worked fine until, with the natural flow of time, the world switched to Unicode and newer Windows started to display question marks in place of Cyrillic characters in the program's UI. This made it rather unusable. There were also other small and not so small imperfections about the program that, as I understand, added considerable factor of irritation to the act of processing photos. ("And when it happens upon a panoramic shot you can as well go and pour yourself some coffee because UI is frozen for minutes while loading the preview…")

So a year ago when we've been visiting his family for Christmas he nagged me, politely but emphatically, about at least making the UI readable again and also, just may be, fixing some of the most outrageous annoyances uncovered over the years of usage. The only problem was… I've lost the source code! I know, it might sound utterly unbelievable these days but it was written in the era before GitHub, and back in those days I've been using — wait for it — Zip drives to store my backups. Which in hindsight turned out to be suboptimal: they fail.

All this, however, provided me with a unique opportunity for making a really good Christmas gift this year…

I suppose there exist people out there who could come up instantly with a perfect gift idea for any of their dozens of friends upon being woken up in the middle of the day, but most of us seem to be destined to endure the agony of scratching the bottom of the void bowl of "what on Earth should we give them this time that won't suck like the last time!" So I was pretty much stoked when some weeks before we were about to leave for the trip it hit me that I actually could write the same program from scratch!

And I'm happy to say that ultimately the idea did work out as intended and at some point it has even been uttered that it was "the best gift ever!"

The best thing though is that now I can actually maintain the code (which I'm doing once a week these days) and not feel sorry for writing another half-working utility. Software is a process, after all.

The endeavor

So I had to learn how to write Windows GUI apps, again. Going back to Delphi was pretty much out of the question as even back in the time it was already loosing the mind share to quickly rising C# and I simply assumed that by now this process has completed. Besides, I actually wanted to learn how Windows GUI programming is "officially" done these days. (Notwithstanding the fact that we're still talking about traditional desktop software, not Metro tiles.)

The lazy evaluation phase took me a couple of weeks, during which I only figured out which of the three-letter acronyms I need to know: WPF, MVVM, C#. The actual design and implementation with ongoing research took 4 days — literally. The most helpful resources along the way were WPF Tutorial and Stack Overflow (of course).

Most importantly though, it was rigorous planning and doing design ahead of coding that allowed me to get the thing done. Here's a few snapshots of my whiteboard with the UI mock-up and current tasks divided by priority:

And though this entire article is not of particular practical importance — I'm simply sharing my emotions here — there is one point I'd really like to drive home:

Planning works. Always.

If you're one of those who doesn't "believe" in it, and for whom "plans never work", I say you most certainly are just doing it wrong and fixing it is a matter of learning how. Indulge yourself.

C# and WPF

I'll say from the get go that I can't presume on having an accurate opinion about a mainstream language after spending just 4 days with it. This is only my first impression.

It feels to me like a modern Delphi, which is probably not surprising given that both were invented by the same Anders Hejlsberg. Type inference makes static typing a lot more palatable, however the time spent on satisfying the compiler's complaints about inconsistent types still feels to me like the time lost. I was pleasantly surprised though by some nice things making their way into a 10+ year old language: lambdas, += for registering event listeners, LINQ — this is all very handy.

But overall, for a Pythonista, the language still feels way too verbose and ceremonious. Want to display a regular public attribute in UI? Oh, just turn it into a property with a getter and a setter and an accompanying separate private field of the same type. A dozen or so lines of code to satisfy a convention — not cool.

Likewise, I can't compare WPF to any modern UI framework as I didn't use any (which is a shame, really). From this position, what immediately feels right about WPF is the data binding concept. Instead of writing disjoint pieces of imperative code updating disjoint pieces of UI and trying doing it in the right order and not forgetting anything, you now define relations like "this ListView shows this list from my data model" and "this action is enabled when these conditions are met and it is bound to these UI controls". And all the controls' state is updated pretty much automatically. I believe it's that thing they call "reactive programming" these days…

The GUI editor is unusable. It took me probably only half a day before I completely switched to editing XAML by hand, and as I understand it's how it's done in practice. Here's a simple example why the editor sucks. XAML layout works best by dividing your window into panels, some of which are of fixed size while others automatically fill available space. Only the GUI editor doesn't do that, instead it gives all panels fixed sizes in pixels, thus defeating the purpose completely. So, surprisingly the old Delphi GUI editor remains the best in my limited opinion: it was usable and it did the right things by default most of the time.

The code

I didn't publish it anywhere yet but I will once I figure out SSH keys on Windows and choose proper licensing. I'm very interested in a code review from someone versed in WPF/C# but what I don't want to do though is maintain it as a proper project with contribution and such, it's just too much hassle.

Comments: 7

  1. bormotov

    2015, but some widgets not use utf8 by default (filenames list at first screen)

  2. Ivan Sagalaev

    It's not the widget's fault, those filenames are indeed broken, they were downloaded this way from Yandex.Disk.

  3. optiklab

    The GUI editor is unusable

    You're right for 100%. It does allow to create very simple UI, but completely doesn't work for any serious project. Six years ago I worked on one corporation WPF project and we get stuck with lots of errors of GUI editor (if I'm correctly remember, VS 2008 contained 1st version of GUI editor) and it wasn't better after 2 years.

    I'm sure, C#/WPF is very good tool for creating applications very fast, but you should edit XAML by hands and this actually gives you more flexibility and more clear & readable code.

  4. razetdinov.ya.ru

    FastStone Image Viewer supports bulk image renaming, but I guess this post is not about image viewers :-)

  5. ashamrin.livejournal.com

    When I'm too lazy to fix Git+SSH problems I sometimes have (e.g key forwarding not working for some reason), I just push via https. Git will ask for GitHub username and password in this case:

    git remote add origin https://github.com/isagalaev/fotogryz.git
    git push origin master
    

    I've also heard GitHub for Windows is pretty cool.

  6. Ivan Sagalaev

    FastStone Image Viewer supports bulk image renaming, but I guess this post is not about image viewers :-)

    I didn't mention quite a few minor specific features that this thing has, and I doubt there's a single piece of software that does all of it. It's, basically, tailor-made versus off-the-shelf thing.

  7. Ivan Sagalaev

    I've also heard GitHub for Windows is pretty cool.

    Yeah, I was thinking about looking at that too. Thanks!

Add comment