Showing off code is hard

I recently created a Dribbble ‘follower’ account. Dribbble is a great website where designers show off small slices of their work.
I could spend hours just browsing and enjoying the nice stuff other people created. It’s nice that designers just need a few pixels on your screen to show off their work. Almost everyone can relate to their work and has an opinion on it. But if you write code for a living, like me, it’s a lot harder to show off your work.

People that don’t write code themselves have no clue what you’re doing in the first place. It’s just a bunch of lines in a language they don’t understand. Of course, the people using the web applications you create have a few ways to directly notice the impact of your code. For example, they can tell that it ‘it works smooth’, ‘never crashes’ etc. But that’s just the top of the iceberg, and most of the time, visitors and customers just take these things for granted. You only hear them when something bad happens, like crashes, data loss or downtime 🙂 It’s a pattern; front-end developers get more of the praise when something good happens, and coders take more heat when things go downhill. Devs might get a pat on the back for delivering solid code within the deadline. But in general, code is often regarded as a commodity instead of a craft, while I still find the thought process of creating software inspiring, fun and challenging.

But it gets even more complicated. It’s pretty difficult to show off code to our fellow devs as well. If I want to post some code snippets here, I’d have to add a lot of context before it starts making sense. I can’t just copy-paste some lines out and get ooohs and aaaahs for elegant LINQ-queries or tightly structured methods.
Open source projects are great – it’s common for a developer to have a Github account with a few open source projects in them. But creating and maintaining a decent open-source project takes a lot of time, determination and inspiration. It’s not something you do in just a few hours of spare time.
Sharing with colleagues is great, but we’re the www-generation so we want to share with the whole world.

At the moment, 90% of the work I do goes unnoticed. It’s buried in a codebase that’s silently buzzing on a server somewhere. It’s not very satisfying to write code and then forget about it. I know there’s more value to be gained from the work I do. But the process of extracting knowledge we can share and appreciate out of ‘anonymous’ codebases is hard and takes a lot of time. It’s difficult to put structure in our thoughts and create and share thoughts, snippets, libraries, patterns and projects. Personally, it even takes me way too much time to write a single blogpost. But even if it’s hard, quitting or whining is not an option. Progress starts with ourselves. Blogposts, open-source projects, conferences – we’re doing this stuff after our hours to create a community where we can share. I really appreciate the time people put in sharing, because I know how hard it is. Some people write thousands of great blogposts, and great new initiatives, projects and communities arise everyday. I’m confident that things will always get a bit better.

For me pesonally, staring at my feet was a mistake I made too often in the past. One of my goals for the future is to spend slightly less time coding, and a little more time sharing. If we don’t ‘market’ our work to our managers, our coworkers, the community and our clients, it might as well not exist. We have to show what we do and make clear why it matters. The key investment is time, and lots of it. But I’m convinced that it’s a good investment to make in the long run.

If you’re a developer, you probably experienced the feelings I’m describing here in some way. What’s your take? How do you show off your code? Comments or follow-up posts are appreciated 🙂

Advertisements

Strongly typed XtraReports

At work, we have an application that needs to generate PDF documents related to invoicing. While I usually dislike working with WYSIWYG editors of any kind, the process of creating PDFs to mail and print is so fixed unit-based that it actually makes some sense here. We ended up using XtraReports from DevExpress.
The problem with most reporting frameworks is that they are, well, reporting solutions instead of pure document generation frameworks. So all their concepts are tightly coupled to directly querying a database and aggregating data. Things I don’t need here: I just want strongly typed bindings on in-memory objects! Out of the box, I don’t see any simple way to achieve this. This means: let’s get to work! 🙂
 
Here’s what I want to end up with:
  • Layout created by the report designer, following all conventions of the reporting tool itself
  • A clean viewmodel, so we can prepare the data we want, using LINQ etc instead of some obscure reporting binding language.
  • All databinding strongly typed (e.g. in the’ code behind’ of the report document)
I was very happy when I found the blogpost ‘Strongly-typed Telerik reports
by Jimmy Bogard. I quote: “Magic strings are maintainability grenades, and I like to squash them wherever they show up.” This post really set me on the right track. But Jimmy’s solution is for Telerik, and since we used DevExpress, I had to move some things around. The solution presetend here also uses the ‘UINameHelper‘ class (just google “UINameHelper”). Our custom class where the magic will happen is DevExpressBindingExtensions:
using System;
using System.Linq.Expressions;
using DevExpress.XtraReports.UI;

namespace Reporting
{
    public enum SimpleFormats
    {
        None,
        Date,
        Currency
    }

    public static class DevExpressBindingExtensions
    {
        public static void BindTo<TModel>(this XRLabel label, Expression<Func<TModel, object>> expression, SimpleFormats format = SimpleFormats.None)
        {
            label.DataBindings.Add(new XRBinding("Text", null, UINameHelper.BuildNameFrom(expression), GetFormatString(format)));
        }

        public static void BindVisibilityTo<TModel>(this XRControl control, Expression<Func<TModel, object>> expression)
        {
            control.DataBindings.Add(new XRBinding("Visible", null, UINameHelper.BuildNameFrom(expression)));
        }

        private static string GetFormatString(SimpleFormats format)
        {
            if (format == SimpleFormats.None) return "";
            if (format == SimpleFormats.Date) return "{0:dd/MM/yyyy}";
            if (format == SimpleFormats.Currency) return "{0:€ 0.00}";
            return "";
        }
    }
}
 Then, in a document, we can bind a label like this:
lblCompanyName.BindTo<InvoiceDocumentModel>(m => m.Invoice.Company.DisplayName);
Or we can use a simple format:
lblAmountTotal.BindTo<InvoiceDocumentModel>(m => m.Invoice.AmountTotal, SimpleFormats.Currency);

Finally, we need to address one more issue. As far as I could tell, XtraReports only allows us to bind to collections, and we have a single model instead. Let’s put the fix for that in a single place, the line where we render the actual report:

        internal byte[] RenderTemplate(XtraReport template, TModel model)
        {
            using (var memoryStream = new MemoryStream())
            {
                template.DataSource = new[] { model };        // WHY: XtraReports can only bind to collections
                template.CreateDocument();
                template.ExportToPdf(memoryStream);

                return memoryStream.ToArray();
            }
        }
This feels a lot more natural to me than the standard databinding features. What do you think? Useful or not? Any feedback is welcome!