1

We've all seen discussions on the ideal length of a method. My favourite litmus test for code quality is to look at the "if" statements, to see whether the values being tested belong to the current class. The further removed the tested value from the context of the test, the stronger the smell of a modelling problem.

So my question is... can anyone fill me in on the origin and the worth of this measure. Is this a recognised metric? Is it possible/desireable to model objects such that all conditional logic tests only class members? What importance would you give to this measure?

4
  • 5
    My favourite litmus test <-- why is it your favorite test? I think if you answer this question you will effectively answer your question here.
    – enderland
    Nov 4, 2015 at 15:24
  • 2
    I've never heard that metric before!
    – pjc50
    Nov 4, 2015 at 15:24
  • 1
    I guess you could see this as a special case of the Law of Demeter, though that doesn't cover things like checking globals. Nov 4, 2015 at 15:28
  • Thank you for law of demeter. Of course the reason I'm asking is because I want views other than my own!
    – bbsimonbb
    Nov 4, 2015 at 15:57

2 Answers 2

4

The use of an if statement is what I call a "bifurcation." It splits the code into two different execution paths, increasing its cyclomatic complexity. That's why examining the if statements is a useful exercise for code analysis.

You can touch other classes without involving if statements, so I'm not sure that the specific combination of if conditions and touching other classes is especially meaningful, other than to identify a specific brand of hell.

1
  • I suppose that if IF is examining other-class properties vis-a-vis method return stuff, then other-class state is (might be) being inferred that should have been encapsulated in said other-class. But that is a big "it depends." Certainly other-class would not be aware of "higher level", composite state - that would be a SRP violation.
    – radarbob
    Nov 4, 2015 at 18:18
1

look at the "if" statements, to see whether the values being tested belong to the current class

What do you mean by value belongs to the current class? Are you encouraging state to be duplicated across objects?

I'd argue the counter argument actually. An object can have public boolean properties or methods that contain state information relevant to other objects to test.

Singletons

Many times singletons (particularly in iOS dev) will have a bool to test that service's availability but it is useful for other objects to test.

if (NetworkManager.sharedInstance().isInternetAvailable())

Data Structures

What about a DTO or custom data structure object:

if (jsonDictionary.count > 0)
if (person.age >= 21)

Let's say the data structures above are being used by a controller (in MVC) what business does a controller have in storing the age of a person when there is an age on the person object.

2
  • 1
    It's difficult to argue from one line of code. There are obviously plenty of objects that have a legitimate interest in the property isInternetAvailable. But when it comes to person.age, I'm not advocating storing age on the controller. I'm asking is it legitimate for the controller to have conditional code that's switched on person.age? Or is it a sign that the controller is doing something that should be done in the person object?
    – bbsimonbb
    Nov 5, 2015 at 14:45
  • Ok I can see what you're saying. I think it depends if you are trying to use pure DTOs or more of an Active Record object w/ navigational methods. If you have a PersonProfileController that controls the UI of person's profile page and need to indicate that they are old enough to drink with (in US) then wouldn't information belong on the person object but have to be checked by the controller?
    – morbidhawk
    Nov 5, 2015 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.