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

      The Double-Edged Sustainability Sword Of AI In Web Design

      August 20, 2025

      Top 12 Reasons Enterprises Choose Node.js Development Services for Scalable Growth

      August 20, 2025

      GitHub’s coding agent can now be launched from anywhere on platform using new Agents panel

      August 20, 2025

      Stop writing tests: Automate fully with Generative AI

      August 19, 2025

      I’m a diehard Pixel fan, but I’m not upgrading to the Pixel 10. Here’s why

      August 21, 2025

      Google Pixel Watch 4 vs. Samsung Galaxy Watch 8: I compared the two best Androids, and here’s the winner

      August 21, 2025

      Get a free Amazon gift card up to $300 when you preorder a new Google Pixel 10 phone – here’s how

      August 21, 2025

      Everything announced at Made by Google 2025: Pixel 10 Pro, Fold, Watch 4, and more

      August 21, 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

      Copy Errors as Markdown to Share With AI in Laravel 12.25

      August 21, 2025
      Recent

      Copy Errors as Markdown to Share With AI in Laravel 12.25

      August 21, 2025

      Deconstructing the Request Lifecycle in Sitecore Headless – Part 2: SSG and ISR Modes in Next.js

      August 20, 2025

      Susan Etlinger, AI Analyst and Industry Watcher on Building Trust

      August 20, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      TerraMaster D1 SSD Plus Review: Experience a Faster External SSD

      August 20, 2025
      Recent

      TerraMaster D1 SSD Plus Review: Experience a Faster External SSD

      August 20, 2025

      Microsoft is investigating Windows 11 KB5063878 SSD data corruption/failure issue

      August 20, 2025

      Microsoft Surface Won’t Turn On: 6 Tested Solutions to Fix

      August 20, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Copy of a Copy of a

    CodeSOD: Copy of a Copy of a

    August 20, 2025

    Jessica recently started at a company still using Windows Forms.

    Well, that was a short article. Oh, you want more WTF than that? Sure, we can do that.

    As you might imagine, a company that’s still using Windows Forms isn’t going to upgrade any time soon; they’ve been using an API that’s been in maintenance mode for a decade, clearly they’re happy with it.

    But they’re not too happy- Jessica was asked to track down a badly performing report. This of course meant wading through a thicket of spaghetti code, pointless singletons, and the general sloppiness that is the code base. Some of the code was written using Entity Framework for database access, much of it is not.

    While it wasn’t the report that Jessica was sent to debug, this method caught her eye:

    private Dictionary<long, decimal> GetReportDiscounts(ReportCriteria criteria)
    {
        Dictionary<long, decimal> rows = new Dictionary<long, decimal>();
    
        string query = @"select  ii.IID,
            SUM(CASE WHEN ii.AdjustedTotal IS NULL THEN 
            (ii.UnitPrice * ii.Units)  ELSE
                ii.AdjustedTotal END) as 'Costs'
                from ii
                    where ItemType = 3
                group by ii.IID
                ";
    
        string connectionString = string.Empty;
        using (DataContext db = DataContextFactory.GetInstance<DataContext>())
        {
            connectionString = db.Database.Connection.ConnectionString;
        }
    
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@DateStart", criteria.Period.Value.Min.Value.Date);
                command.Parameters.AddWithValue("@DateEnd", criteria.Period.Value.Max.Value.Date.AddDays(1));
                command.Connection.Open();
    
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        decimal discount = (decimal)reader["Costs"];
                        long IID = (long)reader["IID"];
    
                        if (rows.ContainsKey(IID))
                        {
                            rows[IID] += discount;
                        }
                        else
                        {
                            rows.Add(IID, discount);
                        }
                    }
                }
            }
        }
    
        return rows;
    }
    

    This code constructs a query, opens a connection, runs the query, and iterates across the results, building a dictionary as its result set. The first thing which leaps out is that, in code, they’re doing a summary (iterating across the results and grouping by IID), which is also what they did in the query.

    It’s also notable that the table they’re querying is called ii, which is not a result of anonymization, and actually what they called it. Then there’s the fact that they set parameters on the query, for DateStart and DateEnd, but the query doesn’t use those. And then there’s that magic number 3 in the query, which is its own set of questions.

    Then, right beneath that method was one called GetReportTotals. I won’t share it, because it’s identical to what’s above, with one difference:

                string query = @"
    select   ii.IID,
                    SUM(CASE WHEN ii.AdjustedTotal IS NULL THEN 
                    (ii.UnitPrice * ii.Units)  ELSE
                     ii.AdjustedTotal END)  as 'Costs' from ii
    				  where  itemtype = 0 
    				 group by iid
    ";
    

    The magic number is now zero.

    So, clearly we’re in the world of copy/paste programming, but this raises the question: which came first, the 0 or the 3? The answer is neither. GetCancelledInvoices came first.

    private List<ReportDataRow> GetCancelledInvoices(ReportCriteria criteria, Dictionary<long, string> dictOfInfo)
    {
        List<ReportDataRow> rows = new List<ReportDataRow>();
    
        string fCriteriaName = "All";
    
        string query = @"select 
            A long query that could easily be done in EF, or at worst a stored procedure or view. Does actually use the associated parameters";
    
    
        string connectionString = string.Empty;
        using (DataContext db = DataContextFactory.GetInstance<DataContext>())
        {
            connectionString = db.Database.Connection.ConnectionString;
        }
    
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@DateStart", criteria.Period.Value.Min.Value.Date);
                command.Parameters.AddWithValue("@DateEnd", criteria.Period.Value.Max.Value.Date.AddDays(1));
                command.Connection.Open();
    
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        long ID = (long)reader["ID"];
                        decimal costs = (decimal)reader["Costs"];
                        string mNumber = (string)reader["MNumber"];
                        string mName = (string)reader["MName"];
                        DateTime idate = (DateTime)reader["IDate"];
                        DateTime lastUpdatedOn = (DateTime)reader["LastUpdatedOn"];
                        string iNumber = reader["INumber"] is DBNull ? string.Empty : (string)reader["INumber"];
                        long fId = (long)reader["FID"];
                        string empName = (string)reader["EmpName"];
                        string empNumber = reader["EmpNumber"] is DBNull ? string.Empty : (string)reader["empNumber"];
                        long mId = (long)reader["MID"];
    
                        string cName = dictOfInfo[matterId];
    
                        if (criteria.EmployeeID.HasValue && fId != criteria.EmployeeID.Value)
                        {
                            continue;
                        }
    
                        rows.Add(new ReportDataRow()
                        {
                            CName = cName,
                            IID = ID,
                            Costs = costs * -1, //Cancelled i - minus PC
                            TimedValue = 0,
                            MNumber = mNumber,
                            MName = mName,
                            BillDate = lastUpdatedOn,
                            BillNumber = iNumber + "A",
                            FID = fId,
                            EmployeeName = empName,
                            EmployeeNumber = empNumber
                        });
                    }
                }
            }
        }
    
    
        return rows;
    }
    

    This is the original version of the method. We can infer this because it actually uses the parameters of DateStart and DateEnd. Everything else just copy/pasted this method and stripped out bits until it worked. There are more children of this method, each an ugly baby of its own, but all alike in their ugliness.

    It’s also worth noting, the original version is doing filtering after getting data from the database, instead of putting that criteria in the WHERE clause.

    As for Jessica’s poor performing report, it wasn’t one of these methods. It was, however, another variation on “run a query, then filter, sort, and summarize in C#”. By simply rewriting it as a SQL query in a stored procedure that leveraged indexes, performance improved significantly.

    [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 ArticleCreating Dynamic Real-Time Features with Laravel Broadcasting
    Next Article 99Managers Futsal Edition – futsal team-management game

    Related Posts

    News & Updates

    I’m a diehard Pixel fan, but I’m not upgrading to the Pixel 10. Here’s why

    August 21, 2025
    News & Updates

    Google Pixel Watch 4 vs. Samsung Galaxy Watch 8: I compared the two best Androids, and here’s the winner

    August 21, 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-2022-4363 – Wholesale Market WooCommerce CSRF Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-32105 – Sangoma IMG2020 HTTP Server Remote Code Execution Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    The Secret to Effective UX Workshops

    Web Development

    New generative media models and tools, built with and for creators

    Artificial Intelligence

    Highlights

    Distribution Release: Ubuntu Unity 25.04

    April 18, 2025

    The DistroWatch news feed is brought to you by TUXEDO COMPUTERS. Rudra Saraswat has announced the release of Ubuntu Unity 25.04, the latest update of this Ubuntu community edition that integrates the Ubuntu base system with the Unity desktop (an interface which was used by all standard Ubuntu releases prior to 2017 when it was dropped in favour of….

    Facebook’s new passkey support could soon let you ditch your password forever

    June 19, 2025

    The best fonts for small text: our pick

    July 13, 2025

    SonicWall Exploit Chain Exposes Admin Hijack Risk via CVE-2023-44221 and CVE-2024-38475

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

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