Poll of the Day > if conditions are an antipattern

Topic List
Page List: 1
chelsea_wtf
11/11/21 11:52:17 AM
#1:


theyre basically just gotos and goto is bad

---
hi im chelsea ^__^
You have entered the lair of SOMA - the Sega Online Machine Agent. Turn back now, or prepare to be digitized!
... Copied to Clipboard!
captpackrat
11/11/21 12:36:33 PM
#2:




gosub

---
Minutus cantorum, minutus balorum,
Minutus carborata descendum pantorum.
... Copied to Clipboard!
Sahuagin
11/11/21 12:54:30 PM
#3:


chelsea_wtf posted...
theyre basically just gotos and goto is bad
I guess this is bait, but a goto is very specifically an unconditional jump

otherwise yes, too many ifs can definitely be a code smell (but not necessarily)

---
... Copied to Clipboard!
chelsea_wtf
11/11/21 1:45:55 PM
#4:


Sahuagin posted...
too many ifs can definitely be a code smell

thats why its so important to have an AbstractConditionFactory to create a variety of different IBooleanHandlers so that your conditional handling logic can be dependency-injected instead of hardcoded. just having an if statement in your business logic is basically two different unconditional jumps combined into a conditional jump

---
hi im chelsea ^__^
You have entered the lair of SOMA - the Sega Online Machine Agent. Turn back now, or prepare to be digitized!
... Copied to Clipboard!
Sahuagin
11/11/21 1:55:50 PM
#5:


chelsea_wtf posted...
thats why its so important to have an AbstractConditionFactory to create a variety of different IBooleanHandlers so that your conditional handling logic can be dependency-injected instead of hardcoded. just having an if statement in your business logic is basically two different unconditional jumps combined into a conditional jump
the right approach can be to put the logic from each of the if blocks into their own classes and simply call a method on the abstract class or interface.

this comes up a lot when for example switching on enum types. IMO enums are generally an anti-pattern since they don't allow for any polymorphism or code reuse and you end up with these "logic varies with value" areas all over the place when they should be in that type itself instead.

---
... Copied to Clipboard!
Topic List
Page List: 1