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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 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

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»CodeSOD: Hall of Mirrors

    CodeSOD: Hall of Mirrors

    November 26, 2024

    Robert was diagnosing a problem in a reporting module. The application code ran a fairly simple query- SELECT field1, field2, field3 FROM report_table– so he foolishly assumed that it would be easy to understand the problem. Of course, the “table” driving the report wasn’t actually a table, it was a view in the database.

    Most of our readers are familiar with how views work, but for those who have had been corrupted by NoSQL databases: database views are great- take a query you run often, and create it as an object in the database:

    CREATE VIEW my_report
    AS
    SELECT t1.someField as someField, t2.someOtherField as someOtherField
    FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id
    

    Now you can query SELECT * FROM my_report WHERE someField > 5.

    Like I said: great! Well, usually great. Well, sometimes great. Well, like anything else, with great power comes great responsibility.

    Robert dug into the definition of the view, only to find that the tables it queried were themselves views. And those were in turn, also views. All in all, there were nineteen layers of nested views. The top level query he was trying to debug had no real relation to the underlying data, because 19 layers of abstraction had been injected between the report and the actual data. Even better- many of these nested views queried the same tables, so data was being split up and rejoined together in non-obvious and complex ways.

    The view that caused Robert to reach out to us was this:

    ALTER VIEW [LSFDR].[v_ControlDate]
    AS
    SELECT
    GETDATE() AS controlDate
    --GETDATE() - 7 AS controlDate
    

    This query is simply invoking a built-in function which returns today’s date. Why not just call the function? We can see that once upon a time, it did offset the date by seven days, making the control date a week earlier. So I suppose there’s some readability in mytable m INNER JOIN v_ControlDate cd ON m.transactionDate > cd.controlDate, but that readability also hides the meaning of control date.

    That’s the fundamental problem of abstraction. We lose details and meaning, and end up with 19 layers of stuff to puzzle through. A more proper solution may have been to actually implement this as a function, not a view- FROM mytable m WHERE m.transactionDate > getControlDate(). At least here, it’s clear that I’m invoking a function, instead of hiding it deep inside of a view called from a view called from a view.

    In any case, I’d argue that the actual code we’re looking at isn’t the true WTF. I don’t like this view, and I wouldn’t implement it this way, but it doesn’t make me go “WTF?” The context the view exists in, on the other hand, absolutely does. 19 layers! Is this a database or a Russian Honey Cake?

    The report, of course, didn’t have any requirements defining its data. Instead, the users had worked with the software team to gradually tweak the output over time until it gave them what they believed they wanted. This meant actually changing the views to be something comprehensible and maintainable wasn’t a viable option- changes could break the report in surprising and non-obvious ways. So Robert was compelled to suffer through and make the minimally invasive changes required to fix the view and get the output looking like what the users wanted.

    The real WTF? The easiest fix was to create another view, and join it in. Problems compound themselves over time.

    [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 ArticleThe best Black Friday Sam’s Club deals 2024: Sales available now
    Next Article Case Study: Nod Coding Bootcamp

    Related Posts

    Machine Learning

    LLMs Struggle with Real Conversations: Microsoft and Salesforce Researchers Reveal a 39% Performance Drop in Multi-Turn Underspecified Tasks

    May 17, 2025
    Machine Learning

    This AI paper from DeepSeek-AI Explores How DeepSeek-V3 Delivers High-Performance Language Modeling by Minimizing Hardware Overhead and Maximizing Computational Efficiency

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Free ChatGPT users can now create images with DALL-E 3, but there’s a catch

    Development

    You’re not fired, Slack is down – here’s what we know so far

    News & Updates

    Chinese APT Gelsemium Targets Linux Systems with New WolfsBane Backdoor

    Development

    Military Spouse Assistants

    Web Development

    Highlights

    Development

    ProTrek: A Tri-Modal Protein Language Model for Advancing Sequence-Structure-Function Analysis

    January 3, 2025

    Proteins, the essential molecular machinery of life, play a central role in numerous biological processes.…

    The MarTech Summit Asia opens its doors on 23-24 April

    April 16, 2024

    Crypto Payment Gateway

    January 13, 2025

    Real‑world video demo: Using different AI models in GitHub Copilot

    May 10, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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