A random BOM appears
I’ve written a post about BOM in the past. It’s quite an important thing in my line of work. Lack of it can cause surprising results. But sometimes it can also appear randomly and wreak havoc. Recently I’ve discovered it in the column header of an Excel file. It was prepended to the column name. The best part is that I was receiving this Excel file from an external party for years, but discovered it only now.
I’ve created a library for reading Excel files (ExcelORM). I’m probably the only person using it, but it’s irrelevant. To map columns before reading I’m using StringComparison.InvariantCultureIgnoreCase to compare whatever is in the given header’s cell to the value from ColumnAttribute or the name of the property of given object. A while back I’ve used AI to review this repository. Because it’s what cool kids do these days, and more seriously because when you’re developing something by yourself it’s good to get a second opinion, even if from a machine. Long story short, AI recommended that I change StringComparison.InvariantCultureIgnoreCase to StringComparison.OrdinalIgnoreCase. And that’s why BOM suddenly became an issue, my library wasn’t reading anything for a column with BOM as with ordinal comparison there wasn’t match anymore. I’ll spare you a story on how long it took me to discover what’s actually happening.
I don’t blame the AI, it was a reasonable choice. And it, same as I, couldn’t predict that somebody will decide to do something this stupid. And in most cases it worked fine. As to why BOM is being added to column header, my best bet is sloppy conversion from CSV. It’s the first column in the worksheet, so that’s very probable. However, I can’t influence producer of this Excel file to change their way, so I’ve reverted to StringComparison.InvariantCultureIgnoreCase in my library and added a test in case I’ll want to switch it in the future.
Some time ago I’ve shared a thought that I much more prefer to write libraries to programs. People were surprised as usually it’s the other way around, especially in Python’s world where you have a library for everything and you just build your code like Legos. I like writing libraries because I believe there’s real craft in it. I’ve always admired when people were saying about somebody’s code “don’t reinvent the wheel just use library x”. That was the true art for me, to create this library x which is considered so solid that you can really rely on it. I’ll probably not create anything like that, but that’s what I’m aiming for.