|Think about Loose Coupling|
GotToBTru's scratchpadby GotToBTru (Parson)
|on Jun 15, 2010 at 13:25 UTC||Need Help??|
OO versus Procedures
One example of the difference is what is called data encapsulation. In an OO program, there (should be) no data outside of a context. In a procedure, I might have a variable called $user_name. Any code I have that can see that variable might also modify it. That can be okay, but it also can be a problem. It can make it hard to determine what part of my code changed something. Also, just because I called it $user_name, I could store ANY string in that variable, just for convenience sake. Heck, in Perl, I could reuse it to store a hash reference if I wanted. Why would I? No idea. But I could.
In an OO program, the user will have a name, but it will be an attribute, "name" of a class, "user". The ONLY way to modify and display the name will be to call the code in the user object to do it. If I want sometimes to display the full name, and sometimes the full name and title, and sometimes just the first name, I write the code to format that in the way I like once, and that functionality is now available to any other program that uses my user object. If I have users in my RPG, or in my music collection program, or in my website business, or whatever. Every time, I get every clever function that I have invented for displaying or using user names without having to copy code from one procedure to another, and without having to remember .. did I do that differently here than there? Will making them the same break something? I finally figure out how to handle names like Beyonce, Dr. Jose Juan Carlos Espinosa del Reyza III, and Seven of Nine. Now, every program knows how to deal with them and will do so consistently.
Okay, couldn't you do this with packages, or by being consistent in your programming? Much of it, you could. But it would not be enforced. At anytime, you could say "I'll do this just once because I'm in a hurry." Using OO, I am using the features of the language to enforce good programming practice. That makes it much easier to share what I've done with others.
XML to Hash
If Perlmonks decides to get more formal in its definition of off-topic, I'd suggest the following:
Link to use as starting point for CSS/JS to color user names to reflect their age relative to me (based on the cb_author id):CSS to display level