-3

Is there a name for this anti-pattern?

A reference to a class member is being passed to another class method, rather than having the class method set the class member directly.

public class AntiPattern { private bool _someConditionWasMet; private void MethodA() { ... MethodB(ref _someConditionWasMet); ... } private void MethodB(ref bool someConditionWasMet) { if (...) { someConditionWasMet = true; } } } 
7
  • 4
    Why is this an "anti-pattern"? Is this the entirety of the code? Drawing conclusions about the code without the entire context of how it's implemented and how it's used is impossible. Commented Oct 9 at 18:10
  • 1
    MethodB has direct access to _someConditionWasMet, so I don't think a reference to that member should be passed to it. I thought this might be some recognizable anti-pattern with a clever name. But if it's not an anti-pattern at all, that's good to know too. It seems to me to be a pretty convoluted practice. Commented Oct 9 at 18:21
  • 1
    Could very well just be a way to set that member's value in just one place - DRY. Without context, that's impossible to know. Commented Oct 9 at 18:59
  • 2
    I would look at all the callers of MethodB. I feel like this is either: a) daft; b) something quirky with the other invocations of MethodB; or c) all of the above. Commented Oct 9 at 22:58
  • 2
    For me, this looks like an intermediate step of a refacoring, either the "parameters to member refactoring", or its opposite. Maybe the dev was interrrupted in the middle of their work and forgot to complete the refactoing? Anyway not every type of WTF code has a name, otherwise we would drown in anti pattern names. Commented Oct 10 at 8:32

1 Answer 1

4

I’ve had cases where one of three instance members could be modified with identical code. You only want one method. And you don’t want the called method not to have triplicate code. So you pass thd member by reference.

In the situation where there is only one instance member and passing a reference is pointless, I very much doubt it is a pattern.

1
  • More generally, if a caller has access to a member why would ot not delegate that access? Limiting delegation is strange. Commented Oct 9 at 22:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.