Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: RFC on how I'm doing when it comes to writing objects?

by 7stud (Deacon)
on Feb 05, 2013 at 01:09 UTC ( [id://1017036]=note: print w/replies, xml ) Need Help??


in reply to RFC on how I'm doing when it comes to writing objects?

Another bad sign is that you called your class 'Objects' (albeit in the directory Twitter). First of all, class names are singular, e.g. Dog, Cat, DBConnection, etc. If you need to create many objects of your class, then you can store them in an array, perhaps named @dogs, @cats, or @db_connections. Or maybe you want to store your objects in a custom container created from one of your classes named MySpecialContainer--again a singular class name.

Of course, you could quickly change your class name to the singular Object, but then the problem is: you would never name a class Object--because it's non descriptive; it's like naming a variable in your program $var. When there is no obvious name for your class like Dog, Cat, or DBConnection, that might be an indication that you don't need a class at all. If all you want to do is create a bunch of useful functions, then put them in a module called Twitter::MyTwitterFunctions, and forget about classes.

If you do want to stay the course with classes, your code does lend itself to creating a class named Account (or Twitter::Account). Start by defining a new() method that takes arguments such as the account name, password, etc., then think of things you would like to do with an account: tweet() a message? get_total_followers(), etc.? Then write a program that creates an Account object for each of your accounts (maybe reading each account name, password, etc. from a text file), then loop through the Account objects to get the information from each Account object and calculate the totals you want.

You can certainly spin off the work that needs to be done to subroutines, so your final program might look something like this:

use strict; use warnings; use 5.012; use Readonly; use Account; Readonly $SPACE => q{ }; #A single space my @accounts; for my $acct_info ($ACCT_FILE) { push @accounts, Account->new(split $SPACE, $acct_info); } say get_total_followers(@accounts); #etc. #etc.

You could take things a step further and create an AccountsManager class. Its new() method could take an array of Account objects as an argument, and the methods you define in the AccountsManager class could return totals for all the Account objects. You could also define add() and remove() methods for the class to allow you to change which accounts are being managed.

Replies are listed 'Best First'.
Re^2: RFC on how I'm doing when it comes to writing objects?
by Lady_Aleena (Priest) on Feb 05, 2013 at 19:53 UTC

    I have been shown I should get away from the idea of objects for this module since I am producing arrays and hashes instead of other things making this module as it is silly. All of the Twitter functionality is handled quite well in Net::Twitter::Lite. I do not want to recreate the wheel. Thank you for helping me see where I went wrong.

    Have a cookie and a very nice day!
    Lady Aleena
Re^2: RFC on how I'm doing when it comes to writing objects?
by Anonymous Monk on Feb 05, 2013 at 06:22 UTC

    you would never name a class Object

    Hey. A lot of programming languages have a class called Object. Are you suggesting the designers were wrong? :)

      Hey. A lot of programming languages have a class called Object. Are you suggesting the designers were wrong? :)

      I've used at least one of them: ruby. Yes, this:

      @you = grep { ! $_->is_oo_runtime_designer } @everybody

      In the limit, in Smalltalk, everything is an object...:)

      James

      There's never enough time to do it right, but always enough time to do it over...

      you would never name a class Object
      Hey. A lot of programming languages have a class called Object. Are you suggesting the designers were wrong? :)

      That should be read as "@youš would never ..."

      @you = grep { ! $_->is_oo_runtime_designer } @everybody

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-16 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found