Call a block of a method only for the first call of the method

后端 未结 6 1717
后悔当初
后悔当初 2021-01-20 02:54

I have a method and inside this method I have a block:

public void method()
{
   [block instructions]
}

But this method is called twice in

6条回答
  •  时光说笑
    2021-01-20 03:01

    You should probably redesign your method. It's confusing for someone trying to understand your program if a method does something different the second time it's run.

    Is it called from a different place in your program, the second time? If that's the case, the easiest thing to do is to extract the bit that's duplicated into another method.

    method() {
        // Do stuff that's run only the first time
        method2()
    }
    
    method2() {
        // Do stuff that's run both times
    }
    

    If it's called from the SAME place, you should ask yourself why your code is expecting something different to happen the second time.

提交回复
热议问题