Don’t Reinvent the Wheel — Use Built-in Utilities
Fermin Perdomo
I was working on adding a canonical URL structure in a project when I came across some old code that was manually parsing the query string.
Then I remembered — C# already provides a built-in utility for this:
Before:
queryParameters = new NameValueCollection();
var queryString = Request.QueryString.ToString();
if (!string.IsNullOrEmpty(queryString))
{
string[] querySegments = queryString.Split('&');
foreach (string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
if (string.IsNullOrEmpty(queryParameters.Get(key)))
{
string decodedUrl = HttpUtility.UrlDecode(val);
val = decodedUrl.Split(',')[0];
queryParameters.Add(key, val);
}
}
}
}
After:
queryParameters = HttpUtility.ParseQueryString(Request?.Url?.Query ?? string.Empty);
With just one line of code, you can replace what used to take several lines of manual string splitting and decoding.
💡 Lesson learned: Don’t reinvent the wheel.
Always check if the language already provides a built-in, optimized function for what you’re trying to do.
It’s not PHP this time 😅 — but I thought it was worth sharing anyway.
Newsletter
Get new posts delivered straight to your inbox.
Great Tools for Developers
Git Tower
Get Started - It's FreeA powerful Git client for Mac and Windows that simplifies version control.
Mailcoach
Start freeSelf-hosted email marketing platform for sending newsletters and automated emails.
Uptimia
Start freeWebsite monitoring and performance testing tool to ensure your site is always up and running.
Cloudways
Start freeManaged cloud hosting platform that simplifies server management for developers.
Comments
No comments yet. Be the first to share your thoughts.