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 & HelpSyntax Questions › simple this.question
Page Index Toggle Pages: 1
simple this.question (Read 240 times)
simple this.question
Mar 11th, 2009, 12:01am
 
what's correct?

option 1:

class class1{
     int x;

   class1(int input_x){
           x= input_x;

         }

}


option 2:

class class2{
     int x;

   class2(int input_x){
           this.x= input_x;

         }

}

This "this" is not necessary is it ?

thanks
Re: simple this.question
Reply #1 - Mar 11th, 2009, 12:26am
 
The this isn't necessary in that case no.

However it is in this case:

Code:

class class3
{
int x;
class3(int x)
{
this.x=x;
}
}


So unless you're naming input variables the same as ones define din your class you don't need to use the this. syntax.
Re: simple this.question
Reply #2 - Mar 11th, 2009, 4:02am
 
Thank you John, that is exactly what i'm going trough.
That's something that always get me confused. I mean, should i use the same variable in class's fields and contructor's parameter, or not? Why?
which are the pros and cons of each?
tnks again

Re: simple this.question
Reply #3 - Mar 11th, 2009, 1:21pm
 
It is a pure question of style, here.
Java conventions (not enforced by the compiler!) are to name the classes with an initial upper case letter and the methods (functions) with an initial lower case letter.
Coming from C and C++ world (mostly on Windows), I kept the usage of starting my methods with an initial upper case letter (annoying Java purists! Smiley). Likewise, I prefer to align my braces...

Another convention I kept (but I don't apply it strictly in Processing) is to use a m_ prefix for the members of my classes. This way, even with a dumb editor or when posting my code, I can easily distinguish these members from parameters or local variables.
Some people like to add an underscore at the start or end of parameters.

I think all people can choose whatever convention they are at ease with, as long as they are consistent in their use.

So, you can write:
Code:
// Classical Sun way
class SomeClass
{
int importantData;

SomeClass(int importantData)
{
this.importantData = importantData;
}
}

// Simple alternative
class SomeClass
{
int importantData;

SomeClass(int id)
{
importantData = id;
}
}

// Not uncommon
class SomeClass
{
int importantData;

SomeClass(int _importantData) // or importantData_
{
importantData = _importantData;
}
}

// My way
class SomeClass
{
int m_importantData;

SomeClass(int importantData)
{
m_importantData = importantData;
}
}

Why "should i use the same variable in class's fields and contructor's parameter"
Two reasons:
- Lack of imagination! Wink
- Consistency (strong ties between given data and corresponding member)

Of course, you can call it data, aData, theData, someData or whatever. Choosing same name (with or without a decoration) is fast and no brainer... :-D
Page Index Toggle Pages: 1