Don’t Reinvent the Wheel — Use Built-in Utilities

Fermin Perdomo Fermin Perdomo
schedule 1 min read

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.

Reactions

lock You need to be logged in to react.
Log In

Newsletter

Get new posts delivered straight to your inbox.

mail

Great Tools for Developers

Git Tower

Git Tower

A powerful Git client for Mac and Windows that simplifies version control.

Visit arrow_forward
Mailcoach

Mailcoach

Self-hosted email marketing platform for sending newsletters and automated emails.

Visit arrow_forward
Uptimia

Uptimia

Website monitoring and performance testing tool to ensure your site is always up and running.

Visit arrow_forward
Cloudways

Cloudways

Managed cloud hosting platform that simplifies server management for developers.

Visit arrow_forward

Comments

No comments yet. Be the first to share your thoughts.

chat_bubble Join the conversation — log in to leave a comment.
Log In