The Daily WTF
Follow
CodeSOD: Copy of a Copy of a
Jessica is working on a legacy Windows Forms application that uses an outdated API. She was tasked with investigating a poorly performing report. The codebase is described as spaghetti code with many inefficiencies. Jessica discovered a method called GetReportDiscounts that executes a SQL query, but paradoxically performs summarization in C# that is already present in the SQL. This method also ignores its own date parameters and uses a magic number for filtering. Another method, GetReportTotals, is nearly identical but uses a different magic number. Both of these methods are derived from an earlier method, GetCancelledInvoices, which correctly utilizes date parameters. However, GetCancelledInvoices also performs filtering in C# rather than in the database query. Jessica found that the actual problematic report was a variation of these methods, relying on client-side processing for data manipulation. By refactoring this report into a SQL stored procedure with proper indexing, the performance improved significantly. The article highlights the common issue of copy-pasting code and performing database operations inefficiently within the application layer.