CodeSOD: A Linking Approach
Web development has gone in a bizarre circle since the early 2000s. Whet ASP.NET launched, its "big sell" was Web Forms, which allowed developers to use a desktop model for handling interactive applications. The basic logic you used for building a desktop forms application would work for a web app, because at the time, everyone was a desktop developer.
Fast forward to today, where we bundle our applications up in web browsers and use web metaphors to build desktop applications, because everyone's a web developer. This may be proof that developers can only ever learn a single way of doing things, and that two options is one too many.
Which brings us to some code Petr Tomanek recently inherited. The original developer, Josiah, started their career around the early 2000s. They were big advocates of the data row approach to data access. In code, they'd forget that they'd already run a query, and run it three more times just to be sure to get the data. Their approach to UI wasn't even quite up to Web Forms standards, and was more, "mash together my personal library of snippets and Stack Overflow copy/paste". The current incarnation of the application used the more modern ASP.NET MVC, but you wouldn't know it to look at most of the code.
But Josiah would at least try to use the new features. For example, when they wanted the URL of a screen in the MVC application, they knew that the ActionLink method was how you created links in your view. There was also a Url.Action method which would construct just the URL. But that's two different ways to do something, and that's one too many. Josiah did this:
html.ActionLink(model.NextButtonText, model.RouteActionName, model.RouteControllerName, model.RouteValues, null).ToString().Split('"')[1];
The ActionLink method returns the full <a href=">"</a>. Then we split on ", and grab the first element after a quote.
At least Josiah was consistent, and used this hammer everywhere they needed a URL.
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!