CodeSOD: Capital Irregularities

Carolyn's company has a bunch of stringly-typed enums. That's already a problem, even in Python, but the other problem is that they need to display these in a human readable format. So, "ProductCategory" needs to become "Product Category". Now, it's true that every one of these stringly typed enums follows the PascalCase convention. It's also true that the list of them is constantly growing.

So this is the method someone wrote for formatting:

def format_text(data): field = data["field_name"] if field == "ProductCategory": field = "Product Category" elif field == "ProductPrice": field = "Product Price" elif field == "ProductName": field = "Product Name"elif field == "UnknownField": field = "Unknown Field" return field

It's unclear how many fields there actually are from the submission, but suffice to say, it's a lot. The fact that "field_name" is a dictionary key hints at a deeper WTF, like an inner-platform style homebrew ORM or something similar, but I have no real evidence about how the code is actually used.

But looking at this code, it makes me wish there was some way to identify the regularities in the input strings, some sort of expression that could identify substrings that I could modify according to the regular pattern. Some sort of regular expression if you will.

Nah, something like that would probably make my code to cryptic to read, and probably just give me extra problems.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.