10

I have found myself wanting to do certain things in my programs only if a variable has changed. I have so far been doing something like this:

int x = 1;
int initialx = x;

...//code that may or may not change the value of x

if (x!=initialx){
    doOneTimeTaskIfVariableHasChanged();
    initialx = x; //reset initialx for future change tests
}  

Is there a better/simpler way of doing this?

6
  • For reference, I found this related (although poorly asked) question: stackoverflow.com/q/3051114/431327 Commented Mar 23, 2011 at 6:22
  • 1
    No. Nothing simpler. You may want to wrap up your code in an object where x lives inside the object and the if(x!=initialx){...} is a method of this object. But this may end up looking more complicated. Commented Mar 23, 2011 at 6:22
  • But that will determine only when script is executed, a variable might change value before this script is executed. Also javascript does not support multithreading, java is different case Commented Mar 23, 2011 at 6:24
  • @ypercube That's exactly what I've been doing so far. It's just I have been teaching myself how to program, and sometimes I wonder what basics or general concepts I might have missed that would be in Chapter 1 of a CS101 textbook. Thanks for the quick response! Commented Mar 23, 2011 at 6:27
  • 2
    Also i would like to suggest question like How to fire event if a variable changes ..lol. Commented Mar 23, 2011 at 6:30

6 Answers 6

9

Since you want to find and perform some action only if the value changes, I would go with setXXX, for example:

public class X
{
    private int x = 1;

    //some other code here

    public void setX(int proposedValueForX)
    {
       if(proposedValueForX != x)
       {
           doOneTimeTaskIfVariableHasChanged();
           x = proposedValueForX;
       }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can use getter/setter with dirty bit associated with each field. mark it dirty if the value is changed through setter, and force user to use setters

2 Comments

Could you explain a little further? This sounds interesting, but I'm new to programming and I haven't used dirty bits before. I'm sure others who come across this question would appreciate it too!
It is simple a bit , which gets set upon some modification on the things you are watching on.
4

another way is use AOP to intercept changing of fields, AspectJ for example, you could have a look a http://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html

Comments

4

Example:

create a variable with the same name with a number.

int var1;
int var2;

if(var1 != var2)
{
//do the code here

var2 = var1;
}

hope this help.

Comments

0

you can also use flag concept, as when ever changing the value of x . assign true for an boolean variable. Keep boolean with default value as false. check by this way.

This way is better than having getters and setters in base of performance, not to have reduntant code of two methods getters and setters.

Comments

0

Assuming you have multiple threads you could create an object monitor and wait for the changed object to wake all blocked threads.

Something like this:

private MyObject myObject;

... other thread ...
while(true) {
  myObject.wait()
  logic you want to run when notified.
}
... other thread ...

... later on ...

myObject.change();
myObject.notifyAll();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.