5 ms·
This is getting ridiculous " ifs are a smelly thing in a Object Oriented language." What's smelly is such a absurd assumption. Yes, let's replace every small
by raverbashing 14y ago
This is getting ridiculous
" ifs are a smelly thing in a Object Oriented language."
What's smelly is such a absurd assumption. Yes, let's replace every small decision in our program with inheritance and another level of abstraction, that will work out swell
Please go read an Assembly manual because you have obviously forgotten what a computer is and does
And then we end up with unmaintainable systems with a class hierarchy higher than the Empire State.
Of course, as mentioned, replacing an if with a "software structure" (like pattern matching in an functional language) is beneficial, but to call it a code smell is ridiculous
- salboaie 14y agoRight, object oriented is not everything, sometimes ifs or switches are just fine. I would suggest that decision tables, rule engines, workflow, DSLs, even finite automaton and other abstractions etc are better than having many many objects that are avoiding ifs :)
- alisnic 14y ago> "ifs are a smelly thing in a Object Oriented language." It is partially my fault because I failed to express what I really ment. English not my native, so sorry. What I really ment in this sentece is "why using (a lot of) ifs can be a bad practice in a Object Oriented language"
- raverbashing 14y agoOk, with that I can agree. Or maybe better: how can ifs be replaced (sometimes) using OO principles
- jeremiep 14y agoMy take on why ifs are "bad" is that they are statements, just like for, while, switch and others. Being statements they can't be composed as you would do with expressions.
- dragonwriter 14y agoThat's not universally true; there are languages where if is an expression rather than a statement (e.g., Ruby.)
- ufo 14y agoMost languages have a ternary if operator like (?:). I don't see how this is such a big issue.
- meaty 14y agoTo be honest, most of the ifs I write are usually error handling conditions so I sort of agree with the principle. If you've been writing OO code for 10-15 years, you evolve into a model which is pretty if and block free. It just sort of happens one day. It isn't decomposed into millions of classes either - just a data model and some kind of effector such as specification/Builder/visitor. SRP only needs to go as far as method really as well. I don't use functional pattern matching either. If your code reads like math, I expect that its probably done right.
- sp4rki 14y agoI know that this isn't probably the case you speak of when you say "If your code reads like math, I expect that its probably done right.", but I have a huge tendency to minimize code size by converting everything to "math". I converted a few hundred lines of C# code full of if statements made by a junior dev into this (more or less): var minutesToAddToAlert = (timeToAddInMinutes) + (900 * ((timeToAddInMinutes + (currentTimeOfDayInMinutes - 510)) / 540)) + (2880*(((currentTimeOfDayInMinutes + (1440*dayOfTheWeek)) + ((timeToAddInMinutes) + (900 * ((timeToAddInMinutes + (currentTimeOfDayInMinutes - 510)) / 540))) ) / 7200)); var nextAlertDate = currentDate.AddMinutes(minutesToAddToAlert); There is definitely a lot more to it than just this, but I don't like blocks of code that don't fit on my vertical 24 inch screens. So I always end up converting "logic" (if's and else's) to math. It's a pet peeve of mine (as is overusing ternary operators as my own personal way of doing haiku), and while I certainly decrease the vertical size of the code itself, it makes it a hell of a lot harder to understand for everyone else. I then have to write comments explaining what stuff like this does, and those comments can get rather obtuse depending on the time of day. Everyone winces when they see my commits with this message: "Math. Converted X and Y to M-A-T-H. Math. :D"
- meaty 14y agoI'd probably actually break it into TimeSpan arithmetic for the sake of clarity and the fact that it won't require comments or so many brackets: var accumulator = new TimeSpan(); accumulator.Add(TimeSpan.FromMinutes(currentTimeOfDayInMinutes)); accumulator.Add(TimeSpan.FromDays(dayOfTheWeek)); // ... etc - can't be bothered to do the rest... var nextAlertDate = currentDate.Add(accumulator); No magic numbers, no arithmetic and clear to anyone who understands the TimeSpan class (which is the all powerful lord of the fourth dimension in .Net). Oh and I work on a 1280x800 screen on an old ThinkPad T61. If the function doesn't fit on that, I'm concerned :)
- Nitramp 14y agoDid you watch the linked talk by Misko? This is not about getting rid of "if" (as he mentions in the talk), this is about structuring large software systems for maintainability, extensibility, testability, etc.
- jeremiep 14y agoAgreed, I wouldn't call ifs a code smell but rather OOP itself a code smell. In any decent language, if will be an expression, not a statement. There is no difference between if and a function.
- Bootvis 14y agoIn what languages is if a function?
- entropy_ 14y agoLISP maybe?
- jeremiep 14y agoIn Haskell/Ruby/CoffeeScript: a = if b then c else d In Lisp: (setq a (if b c d)) Only Lisp implements them as a function (more precisely, a macro), but to the programmer there's no difference between using if and calling a function. Of course, they don't compile into function calls in assembly code :)
- tarabukka 14y agoIf is an expression in a lot of languages. The syntax you describe is identical to the "ternary statement" in C-style languages.
- jeremiep 14y agoThe ternary is an expression, not a statement :) You can write: a = b ? c : d; in C/C++, you can't write a = if(b) { c; } else { d; }.
- tarabukka 14y agoOh, yeah. My mistake. Although what you describe is a lot more functional in style. You could probably emulate it in a language with higher-order functions.
- revscat 14y ago> And then we end up with unmaintainable systems with a class hierarchy higher than the Empire State. Perhaps, but I would rather have testable code than untestable code that is nothing more than a series of unnecessary, nested if blocks. We have all seen code that has a structure like if (depObj1.isSomething()) { if (depOboj1.getAttr() == CONST1) { // Do something } else if (depObj1.getAttr() == CONST2) { // Do something different } } else if ... { // repeat with minor differences } Most of the time the above structure can be abstracted. Abstracting It also has the benefit of making this code more testable: you don't have to set up many different objects, just one. The OP is correct: in OO languages if statements are a code smell. Like all smells, though, they are not hard and fast rules, but rather indicators that things could probably be done better. No one, certainly not OP, is recommending abandoning if statements. What he is recommending, and I agree with, is that they can and should be avoided, and certainly not turned to as a primary tool in the toolbelt.
- raverbashing 14y agoIt's irrelevant if you're using ifs or different objects, the number of conditions/tests needed for what you are testing is the same. Now, you could make depObj1 of type Obj1 and Obj2 to replace the if, which may simplify testing because the method is "simpler" in Obj1/Obj2/ObjBase but in the end it isn't! Why? Because your test of Obj1/2 has to take care of their dependency to ObjBase. It's usually a dependency hell, needing lots of workarounds to test properly.
- aardvark179 14y agoI might agree if that type of if statement only appeared at one point in the code, but there're normally several scattered around which all have to be updated when obj3 is introduced. Also introducing a class hierarchy often makes it easier to move responsibilities between objects as the code evolves. For example I recently did some work on a graphics library which had a lot of ifs to do with line and fill styles. By changing those into an inheritance hierarchy I found a couple of places where clauses had been missed and so fixed some bugs, but more importantly when we needed to add new fill styles which were more complicated to draw it became very easy to turn the fill style objects into fillers which had all the responsibility for filling paths, not simply setting the graphics state.
- philwelch 14y agoThat's not really fair. The whole point of OO (at least dynamic dispatch) is that the dispatch system itself can evaluate a type condition, so you can rest a lot of weight on that implicit condition rather than having to write your own. If you have too many conditionals, maybe you aren't using that enough. Likewise, if you have list comprehensions or at least higher level functions like map, reduce, and filter, having too many loops is a little questionable. If we wanted to program in assembly, we would be programming in assembly.