We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › which are the diference between these 2 codes
Page Index Toggle Pages: 1
which are the diference between these 2 codes? (Read 1113 times)
which are the diference between these 2 codes?
Apr 20th, 2010, 4:32pm
 
hello , i would like to know which are the difference between these 2 versions of the same code, they behave in the same way:  Is there any benefit using one instead the other? and what does it means private in the second version?
thanks in advance


Code:

Dot moco;

void setup()
{
size(800,400);
smooth();
noFill();
moco = new Dot(random(width),random(height));
}


void draw()
{
background(255);
stroke(0);
strokeWeight(2);
moco.mueve();
print(moco.xx);
//print(moco.getLocation().x);
point(moco.xx, moco.yy);

}


class Dot
{
float xx;
float yy;

Dot(float x, float y)
{
xx = x;
yy = y;
}

void mueve() {
xx = xx+ 1;
}
}


and this is the other version:

Code:

Dot moco;

void setup()
{
size(800,400);
smooth();
noFill();
moco = new Dot(random(width),random(height));

}

void draw()
{
background(255);
stroke(0);
strokeWeight(2);
moco.mueve();
//print(moco.getLocation().x);
point(moco.getLocation().x, moco.getLocation().y);

}

class Dot
{
private PVector d;

Dot(float x, float y)
{
d = new PVector(x,y);
}

void mueve() {
d.x = d.x+ 1;

}

PVector getLocation()
{
return d;
}
}




Re: which are the diference between these 2 codes?
Reply #1 - Apr 20th, 2010, 6:49pm
 
PVectors are cooler than using two floats Smiley . you have the benefits of all methods that processing provide for them.
look here:
http://processing.org/reference/PVector.html

if you set the PVector as private it cant be changed from outside the class.in this case you dont have a set method to change it.
as long you dont work for a big company or the variable can be set without side effect you can let it public and leave getter and setter methods
Re: which are the diference between these 2 codes?
Reply #2 - Apr 20th, 2010, 11:07pm
 
ramin wrote on Apr 20th, 2010, 6:49pm:
if you set the PVector as private it cant be changed from outside the class.

Except that all classes in Processing are internal to the main class, thus can see each other's private variables...
Code:
class A
{
private int x = 5;
}
class B
{
private A a = new A();
void Show()
{
println(a.x);
}
}

void setup()
{
B b = new B();
b.Show();
println(b.a.x);
}

Which enforces your advice: don't worry with private keyword in Processing! Smiley
(Except when making libraries, etc.)
Re: which are the diference between these 2 codes?
Reply #3 - Apr 21st, 2010, 2:45am
 
hello, my question was focused more in the way of access variables, for example in the second example : what about using a function like this?
 PVector getLocation()
 {
   return d;
 }

and accessing the variable in this way?
moco.getLocation().x

whats the difference betweeen this and using the conventional way where i can access the variable in this way:
moco.xx  like in the first example?

Is there any benefit , between moco.getLocation().x instead of using moco.xx

Re: which are the diference between these 2 codes?
Reply #4 - Apr 21st, 2010, 4:51am
 
mh i don't think that there is any benefits with the performance.
the only simple benefit i can just think of now is that you can count the how often you get the variable... but this can also be done anywhere else.
its your decision, you can write a program in a houndred different ways and the result will be the same. the difference might only be that there are differences how easy they are to understand and change.
Re: which are the diference between these 2 codes?
Reply #5 - Apr 21st, 2010, 5:11am
 
getLocation() is what is called a getter, an accessor. setLocation() would be a setter.

These are rarely used in Processing sketches, often short and informal.
They are more used in large Java projects, libraries, etc.
The general advice in OOP (object oriented programming) is to hide all variables (except, perhaps static constants), ie. make them private. Then, you can allow access to some of them with accessors.
The advantage of using foo.setLocation(v) instead of foo.loc = v is multiple:
- You can rename the internal variable without changing the API.
- You can change the type of the internal variable, then the accessor makes the necessary conversion.
- You can check the given value and reject it (in general by throwing an exception) if not suitable (not null, within given bounds, etc.).
- As said, you can do extra work, logging, making stats, etc.
You can also recompute internal variables depending on the changed value, etc.

It is named encapsulation, and allows more freedom in the implementation as long as the API (the contract) is respected.
Page Index Toggle Pages: 1