Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      From Data To Decisions: UX Strategies For Real-Time Dashboards

      September 13, 2025

      Honeycomb launches AI observability suite for developers

      September 13, 2025

      Low-Code vs No-Code Platforms for Node.js: What CTOs Must Know Before Investing

      September 12, 2025

      ServiceNow unveils Zurich AI platform

      September 12, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025

      Distribution Release: Q4OS 6.1

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Learning from PHP Log to File Example

      September 13, 2025
      Recent

      Learning from PHP Log to File Example

      September 13, 2025

      Online EMI Calculator using PHP – Calculate Loan EMI, Interest, and Amortization Schedule

      September 13, 2025

      Package efficiency and dependency hygiene

      September 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Dmitry — The Deep Magic

      September 13, 2025
      Recent

      Dmitry — The Deep Magic

      September 13, 2025

      Right way to record and share our Terminal sessions

      September 13, 2025

      Windows 11 Powers Up WSL: How GPU Acceleration & Kernel Upgrades Change the Game

      September 13, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Gridding My Teeth

    CodeSOD: Gridding My Teeth

    June 12, 2025

    Dan‘s co-workers like passing around TDWTF stories, mostly because seeing code worse than what they’re writing makes them feel less bad about how often they end up hacking things together.

    One day, a co-worker told Dan: “Hey, I think I found something for that website with the bad code stories!”

    Dan’s heart sank. He didn’t really want to shame any of his co-workers. Fortunately, the source-control history put the blame squarely on someone who didn’t work there any more, so he felt better about submitting it.

    This is another ASP .Net page, and this one made heavy use of GridView elements. GridView controls applied the logic of UI controls to generating a table. They had a page which contained six of these controls, defined like this:

    <span class="hljs-tag"><<span class="hljs-name">asp:GridView</span> <span class="hljs-attr">ID</span>=<span class="hljs-string">"gvTaskMonth1"</span> <span class="hljs-attr">runat</span>=<span class="hljs-string">"server"</span> <span class="hljs-attr">CssClass</span>=<span class="hljs-string">"leadsGridView"</span> <span class="hljs-attr">AutoGenerateColumns</span>=<span class="hljs-string">"False"</span> <span class="hljs-attr">OnRowDataBound</span>=<span class="hljs-string">"gvTaskMonth1_RowDataBound"</span>></span> ... <span class="hljs-tag"></<span class="hljs-name">asp:GridView</span>></span>
    
    <span class="hljs-tag"><<span class="hljs-name">asp:GridView</span> <span class="hljs-attr">ID</span>=<span class="hljs-string">"gvTaskMonth2"</span> <span class="hljs-attr">runat</span>=<span class="hljs-string">"server"</span> <span class="hljs-attr">CssClass</span>=<span class="hljs-string">"leadsGridView"</span> <span class="hljs-attr">AutoGenerateColumns</span>=<span class="hljs-string">"False"</span> <span class="hljs-attr">OnRowDataBound</span>=<span class="hljs-string">"gvTaskMonth1_RowDataBound"</span>></span> ... <span class="hljs-tag"></<span class="hljs-name">asp:GridView</span>></span>
    
    <span class="hljs-tag"><<span class="hljs-name">asp:GridView</span> <span class="hljs-attr">ID</span>=<span class="hljs-string">"gvTaskMonth3"</span> <span class="hljs-attr">runat</span>=<span class="hljs-string">"server"</span> <span class="hljs-attr">CssClass</span>=<span class="hljs-string">"leadsGridView"</span> <span class="hljs-attr">AutoGenerateColumns</span>=<span class="hljs-string">"False"</span> <span class="hljs-attr">OnRowDataBound</span>=<span class="hljs-string">"gvTaskMonth1_RowDataBound"</span>></span> ... <span class="hljs-tag"></<span class="hljs-name">asp:GridView</span>></span>
    

    The purpose of this screen was to display a roadmap of coming tasks, broken up by how many months in the future they were. The first thing that leaps out to me is that they all use the same event handler for binding data to the table, which isn’t in-and-of-itself a problem, but the naming of it is certainly a recipe for confusion.

    Now, to bind these controls to the data, there needed to be some code in the code-behind of this view which handled that. That’s where the WTF lurks:

    <span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"><summary></span></span>
    <span class="hljs-comment"><span class="hljs-doctag">///</span> Create a roadmap for the selected client</span>
    <span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"></summary></span></span>
    
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">CreateRoadmap</span>()</span>
    {
    	<span class="hljs-keyword">for</span> (<span class="hljs-built_in">int</span> i = <span class="hljs-number">1</span>; i < <span class="hljs-number">7</span>; i++)
    	{
    		<span class="hljs-keyword">switch</span> (i)
    		{
    			<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth1.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth1, DateTime.Parse(txtDatePeriod1.Text), <span class="hljs-string">"1"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    			<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth2.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth2, DateTime.Parse(txtDatePeriod2.Text), <span class="hljs-string">"2"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    			<span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth3.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth3, DateTime.Parse(txtDatePeriod3.Text), <span class="hljs-string">"3"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    			<span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth4.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth4, DateTime.Parse(txtDatePeriod4.Text), <span class="hljs-string">"4"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    			<span class="hljs-keyword">case</span> <span class="hljs-number">5</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth5.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth5, DateTime.Parse(txtDatePeriod5.Text), <span class="hljs-string">"5"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    			<span class="hljs-keyword">case</span> <span class="hljs-number">6</span>:
    				<span class="hljs-keyword">if</span> (gvTaskMonth6.Rows.Count > <span class="hljs-number">0</span>)
    				{
    					InsertTasks(gvTaskMonth6, DateTime.Parse(txtDatePeriod6.Text), <span class="hljs-string">"6"</span>);
    				}
    				<span class="hljs-keyword">break</span>;
    		}
    	}
    }
    

    Ah, the good old fashioned loop-switch sequence anti-pattern. I understand the motivation: “I want to do the same thing for six different controls, so I should use a loop to not repeat myself,” but then couldn’t quite figure out how to do that, so they just repeated themselves, but inside of a loop.

    The “fix” was to replace all of this with something more compact:

    	<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">CreateRoadmap</span>()</span>
    	{
    		InsertTasks(gvTaskMonth1, DateTime.Parse(txtDatePeriod1.Text), <span class="hljs-string">"1"</span>);
    		InsertTasks(gvTaskMonth2, DateTime.Parse(txtDatePeriod2.Text), <span class="hljs-string">"2"</span>);
    		InsertTasks(gvTaskMonth3, DateTime.Parse(txtDatePeriod3.Text), <span class="hljs-string">"3"</span>);
    		InsertTasks(gvTaskMonth4, DateTime.Parse(txtDatePeriod4.Text), <span class="hljs-string">"4"</span>);
    		InsertTasks(gvTaskMonth5, DateTime.Parse(txtDatePeriod5.Text), <span class="hljs-string">"5"</span>);
    		InsertTasks(gvTaskMonth6, DateTime.Parse(txtDatePeriod6.Text), <span class="hljs-string">"6"</span>); 
    	}
    

    That said, I’d recommend not trying to parse date times inside of a text box inside of this method, but that’s just me. Bubbling up the inevitable FormatException that this will generate is going to be a giant nuisance. It’s likely that they’ve got a validator somewhere, so it’s probably fine- I just don’t like it.

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleFlaws in Weidmueller IE-SR-2TX Routers Allow Remote Root Access!
    Next Article Schemes – create syntax highlighting schemes

    Related Posts

    News & Updates

    Building personal apps with open source and AI

    September 12, 2025
    News & Updates

    What Can We Actually Do With corner-shape?

    September 12, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-3794 – WordPress WPForms Stored Cross-Site Scripting Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Microsoft Copilot roasts Bill Gates, Satya Nadella, and asks Steve Ballmer if his enthusiasm might ever short-circuit the AI

    News & Updates

    Lenovo Legion Go 2 specs unveiled: The handheld gaming device to watch this October

    News & Updates

    Learn the Evolution of the Transformer Architecture Used in LLMs

    Development

    Highlights

    Machine Learning

    Model Compression Without Compromise: Loop-Residual Neural Networks Show Comparable Results to Larger GPT-2 Variants Using Iterative Refinement

    April 16, 2025

    The transformer architecture has revolutionized natural language processing, enabling models like GPT to predict the…

    Is ABC Classification a thing of the past?

    April 27, 2025

    CVE-2025-4316 – “Devolutions Server PAM Unauthorized Self-Approval”

    May 5, 2025

    Kubernetes IngressNightmare Vulnerabilities: What You Need to Know

    April 24, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.