CodeSOD: Blocked Up
Agatha has inherited some Windows Forms code. This particular batch of such code falls into that delightful category of code that's wrong in multiple ways, multiple times. The task here is to disable a few panels worth of controls, based on a condition. Or, since this is in Spanish, "bloquear controles". Let's see how they did it.
private void BloquearControles() { bool bolBloquear = SomeConditionTM; // SomeConditionTM = a bunch of stuff. Replaced for clarity. // Some code. Removed for clarity. // private System.Windows.Forms.Panel pnlPrincipal; foreach (Control C in this.pnlPrincipal.Controls) { if (C.GetType() == typeof(System.Windows.Forms.TextBox)) { C.Enabled = bolBloquear; } if (C.GetType() == typeof(System.Windows.Forms.ComboBox)) { C.Enabled = bolBloquear; } if (C.GetType() == typeof(System.Windows.Forms.CheckBox)) { C.Enabled = bolBloquear; } if (C.GetType() == typeof(System.Windows.Forms.DateTimePicker)) { C.Enabled = bolBloquear; } if (C.GetType() == typeof(System.Windows.Forms.NumericUpDown)) { C.Enabled = bolBloquear; } } // private System.Windows.Forms.GroupBox grpProveedor; foreach (Control C1 in this.grpProveedor.Controls) { if (C1.GetType() == typeof(System.Windows.Forms.TextBox)) { C1.Enabled = bolBloquear; } if (C1.GetType() == typeof(System.Windows.Forms.ComboBox)) { C1.Enabled = bolBloquear; } if (C1.GetType() == typeof(System.Windows.Forms.CheckBox)) { C1.Enabled = bolBloquear; } if (C1.GetType() == typeof(System.Windows.Forms.DateTimePicker)) { C1.Enabled = bolBloquear; } if (C1.GetType() == typeof(System.Windows.Forms.NumericUpDown)) { C1.Enabled = bolBloquear; } } // private System.Windows.Forms.GroupBox grpDescuentoGeneral; foreach (Control C2 in this.grpDescuentoGeneral.Controls) { if (C2.GetType() == typeof(System.Windows.Forms.TextBox)) { C2.Enabled = bolBloquear; } if (C2.GetType() == typeof(System.Windows.Forms.ComboBox)) { C2.Enabled = bolBloquear; } if (C2.GetType() == typeof(System.Windows.Forms.CheckBox)) { C2.Enabled = bolBloquear; } if (C2.GetType() == typeof(System.Windows.Forms.DateTimePicker)) { C2.Enabled = bolBloquear; } if (C2.GetType() == typeof(System.Windows.Forms.NumericUpDown)) { C2.Enabled = bolBloquear; } } // Some more code. Removed for clarity. }This manages two group boxes and a panel. It checks a condition, then iterates across every control beneath it, and sets their enabled property on the control. In order to do this, it checks the type of the control for some reason.
Now, a few things: every control inherits from the base Control class, which has an Enabled property, so we're not doing this check to make sure the property exists. And every built-in container control automatically passes its enabled/disabled state to its child controls. So there's a four line version of this function where we just set the enabled property on each container.
This leaves us with two possible explanations. The first, and most likely, is that the developer responsible just didn't understand how these controls worked, and how inheritance worked, and wrote this abomination as an expression of that ignorance. This is extremely plausible, extremely likely, and honestly, our best case scenario.
Because our worse case scenario is that this code's job isn't to disable all of the controls. The reason they're doing type checking is that there are some controls used in these containers that don't match the types listed. The purpose of this code, then, is to disable some of the controls, leaving others enabled. Doing this by type would be a terrible way to manage that, and is endlessly confusing. Worse, I can't imagine how this behavior is interpreted by the end users; the enabling/disabling of controls following no intuitive pattern, just filtered based on the kind of control in use.
The good news is that Agatha can point us towards the first option. She adds:
They decided to not only disable the child controls one by one but to check their type and only disable those five types, some of which aren't event present in the containers. And to make sure this was WTF-worthy the didn't even bother to use else-if so every type is checked for every child control
She also adds:
At this point I'm not going to bother commenting on the use of GetType() == typeof() instead of is to do the type checking.
Bad news, Agatha: you did bother commenting. And even if you didn't, don't worry, someone would have.
.comment { border: none; } [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.Two Harlow playgrounds to be transformed with 'new equipment for all ages'
Incredible moment two people rescued from hot air balloon after striking radio tower in Texas
NHS branded 'disgraceful' after cancelling new health centre in Essex town in latest revamp set back
Qantas slammed by Aussies for selling $2,000 flights to the Middle East despite the airspace being closed
JD Vance says Iran conflict has 'clear' objective and will not devolve into another Iraq or Afghanistan: Live updates
Thief stole more than 300 works from British museum in plain sight - before being let off with suspended sentence
Edinburgh knife attack suspect 'tried to enter nursery': Man 'who left two people injured and ransacked shop' is arrested after seven hour stand-off with police
LAURA CRAIK's fashion verdict on the stars who take the plunge
As MPs' pay is set to soar to £110,000, do they REALLY need a 'cost-of-living uplift'?
Strikes on Iran killed US's preferred successors to take over the regime, Trump claims
I watched a 12-year-old murderer smile in the dock... her evil is something I will NEVER forget, writes TOM RAWSTORNE. Read it exclusively in The Crime Desk newsletter
Inside the crumbling leisure centre that Oasis named themselves after
Russian tycoon, 67, who was named in the Epstein files and called Ghislaine Maxwell his 'soulmate' is found dead
Justin Timberlake fights to keep DWI bodycam footage from public eyes
Father races against time to raise £300,000 for German treatment for son with rare degenerative condition before illness leaves him severely disabled
Michael Jackson's son Bigi - formerly Blanket - flashes animated expression during rare outing
Trump obliterates Iran's navy by sinking ELEVEN boats including the 'mothership' as Tehran's brazen lies are exposed
AWS says drones hit two of its datacenters in UAE, urges users to move resources to different regions
UPDATED Multiple Amazon Web Services (AWS) availability zones in the Middle East are experiencing outages or degraded connectivity after objects struck a UAE facility, as Iranian retaliatory missile and drone attacks hit targets across the Gulf.…