Be a Supporter!

Question about public methods

  • 320 Views
  • 6 Replies
New Topic Respond to this Topic
Jigganis
Jigganis
  • Member since: Nov. 7, 2003
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Question about public methods 2008-10-02 14:21:32 Reply

So I'm reading the beginning chapters of my actionscript 3.0 book and I come across a section within the part about instance methods with advice that goes something like this:

"When you're programming a class's methods, make sure the only ones you leave publicly accessible are the ones outside programmers and outside code will need to instruct your object to do something."

And the author uses this analogy to make the point:

"...think of an object as a car,
whose driver is the programmer using the object, and whose manufacturer is the programmer
that created the object's class. To drive the car, the driver doesn't need to
know how a car's engine works. The driver simply uses the gas pedal to accelerate
and the steering wheel to turn. Accelerating the car in response to the driver stepping
on the gas pedal is the manufacturer's concern, not the driver's."

I'm not entirely sure what he means by all this. I'm hoping someone here might be able to help explain... and also make one more thing clear--for whom is this information useful? Is this for professional programmers only, the type who are making commercial applications and programs? Or is it good for lowly beginning programmers like me, too?

Thanks.

Archawn
Archawn
  • Member since: Sep. 9, 2007
  • Offline.
Forum Stats
Member
Level 27
Game Developer
Response to Question about public methods 2008-10-02 18:16:01 Reply

Answer: The author was high while writing that. What book exactly are you reading???

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Question about public methods 2008-10-02 18:25:33 Reply

At 10/2/08 02:21 PM, Jigganis wrote: Or is it good for lowly beginning programmers like me, too?

No it's not something you should worry about if you are a total beginner. Learn the basics first.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Question about public methods 2008-10-02 18:27:12 Reply

At 10/2/08 06:16 PM, Archon68 wrote: Answer: The author was high while writing that. What book exactly are you reading???

What the book says makes perfect sense. Congrats for proving how nooby you are :P.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Deaththe14th
Deaththe14th
  • Member since: Aug. 30, 2007
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Question about public methods 2008-10-02 18:35:50 Reply

If I know my "objects" in actionscript right, an object would be, say, a movieclip.
The "objects" that they're talking about, I think, are for object oriented programming.

I guess what the guy is trying to say there is... hmm... the manufacturer, Adobe (or Macromedia...) have made the object Car.

Now you don't need to know all the code that goes into moving all the little parts of Car around.
All you know is that you're gonna use some code on it.
For example:
Car._x += 5;

I mean, you don't see all the code that actually finds what the current x value is, adds five to it, and then renders it on the bloody screen.
You're just... "pushing the pedals" so to speak.

It's fairly bad analogy for a beginner to try to understand, but I think it makes sense enough...


Disregard that, I eat poop.

Jigganis
Jigganis
  • Member since: Nov. 7, 2003
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Question about public methods 2008-10-02 23:15:19 Reply

Thanks guys, that makes more sense than it did before, that's for sure.

One thing I meant to ask was this: who DOES use this sort of coding stuff? I'm looking for a few examples, just so I can get a clear picture of where this is going to be on the great journey of AS learning.

Moonkey
Moonkey
  • Member since: May. 11, 2007
  • Offline.
Forum Stats
Member
Level 07
Programmer
Response to Question about public methods 2008-10-03 03:51:56 Reply

I'm surprised no one's chimed in with a big academic post about this.

In general, you want classes to be responsible for their own inner workings, and only allow public access in limited ways. As long as they do their job, other classes shouldn't really care about how it's done. There's a couple of reasons for that.

As an example, think of an OrderedList class that you might write. There's a few public methods that you would naturally want it to have - add(item), remove(item), getFirst(), getLast() etc. Outside classes don't need to know about how the list is stored and maintained.
You might decide to store all the items that are added in an array, and you would want that array to be private. If it was public, other classes could get in and manipulate it in ways that could interfere with how you are maintaining the order, or break certain references that you might be keeping internally.
And then later on down the track you might decide that an array is a poor way of handling it and decide to use a linked list instead. All the public methods (add, remove etc) can stay the same. Changing how the data is stored internally won't break any other code because the public access points are still the same.
If you'd made the array public to begin with, the temptation would be there to access it directly from other classes, which means changing to a linked list would break that code.

It's not super important if you're the only person working on a project, but it's good practice and it can save you some headaches from time to time.