04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Hopefully you noticed that the only difference between the good <strong>and</strong> bad code is the<br />

absence of the this keyword at the beginning of the bad code assignments. If you don’t<br />

use the this keyword, the compiler uses the local parameters on both sides of the assignment<br />

operator.<br />

The special keyword this refers to the class itself. Thus, this.meatType is the property,<br />

not the parameter of the same name. As I mentioned, if you don’t use the keyword this,<br />

the compiler will only use the local parameter within the constructor block—so an assignment<br />

like meatType = meatType doesn’t pass a value to the property, but just uselessly<br />

assigns the parameter value back to the parameter—meaning nothing really happens. I<br />

realize that if this is totally new to you, it may seem like an obscure point, but it really isn’t.<br />

Is there any way to avoid using the this keyword? Well, there is, actually. If I had used distinct<br />

property <strong>and</strong> parameter names, I could have avoided using the this keyword. For<br />

example, the following constructor could be safely substituted for the second constructor<br />

in the BurritoRecipe class example:<br />

//basic burrito recipe--add toppings later<br />

BurritoRecipe(String tortFlavor, String bType, String mType){<br />

//initialize properties<br />

tortillaFlavor = tortFlavor;<br />

beanType = bType;<br />

meatType = mType;<br />

}<br />

I still could use the this keyword in front of the properties on the left of the assignments<br />

if I wanted to, but it is not required any longer. Ultimately, you can do your constructor initializations<br />

either way: using the same named properties <strong>and</strong> parameters <strong>and</strong> the this keyword<br />

in front of the properties, or keeping the property <strong>and</strong> parameter names distinct. In<br />

my humble opinion, using the same names for the properties <strong>and</strong> parameters ultimately<br />

makes things simpler, <strong>and</strong> also helps you to see what parameter to assign to what property.<br />

But not everyone would agree with me. When you start looking at other people’s code,<br />

you’ll see this h<strong>and</strong>led both ways—even though I think I’m right.<br />

Methods<br />

After the constructors, there is a long list of methods, in the following style:<br />

int getSize() {<br />

return this.size;<br />

}<br />

void setSize(int size) {<br />

this.size = size;<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

In OOP, there is a convention to structure methods utilizing the get/set prefix appended<br />

to the property names (commonly referred to as get/set methods, or “getters” <strong>and</strong><br />

“setters”), as illustrated in the preceding code. Not all your methods will be getters <strong>and</strong><br />

setters, but generally it is desirable to have a get <strong>and</strong> set method for each property. In pure<br />

OOP (which you’re not totally burdened with here), the idea is that a class’s properties<br />

311<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!