What to do when object has lots of attributes?

N00bie question. I'm working on a Class that has lots of attributes that
can be set or get. I've been using attr_accessor but am wondering if
there's a better way to store data. At this point I'm looking at two or
three dozen attributes and being extended as needed.

Thoughts?

Leam

···

--
Mind on a Mission <http://leamhall.blogspot.com/>

N00bie question. I'm working on a Class that has lots of attributes that

can be set or get. I've been using attr_accessor but am wondering if
there's a better way to store data. At this point I'm looking at two or
three dozen attributes and being extended as needed.

Sounds suspicious at first sight. Normally you don't have that many
attributes.

But that is a statistical characteristic of classes, not a hard rule. It
depends.

Knowing more about the class could help giving more concrete feedback. Coud
you paste some code? What is the class modelling? How does it look like?

···

On Mon, Nov 14, 2016 at 7:37 PM, leam hall <leamhall@gmail.com> wrote:

Hey Xavier, the current class is:
https://github.com/LeamHall/CT_Character_Generator/blob/master/lib/Character.rb

I'm in the middle of adding another large number of attributes, and will
probably continue to add them. Hmm...I wonder if I can use hashes for
related data and cut down on the attr_accessor clutter.

···

On Mon, Nov 14, 2016 at 1:45 PM, Xavier Noria <fxn@hashref.com> wrote:

On Mon, Nov 14, 2016 at 7:37 PM, leam hall <leamhall@gmail.com> wrote:

N00bie question. I'm working on a Class that has lots of attributes that

can be set or get. I've been using attr_accessor but am wondering if
there's a better way to store data. At this point I'm looking at two or
three dozen attributes and being extended as needed.

Sounds suspicious at first sight. Normally you don't have that many
attributes.

But that is a statistical characteristic of classes, not a hard rule. It
depends.

Knowing more about the class could help giving more concrete feedback.
Coud you paste some code? What is the class modelling? How does it look
like?

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

If this is for a game, the characters traits could be broken up into sub
groups and you could use structs or hashes in composition of the character
class. For example, all physical attributes could be in a "physical_traits"
struct, and skills could be in a skills struct. You then have accessors for
the structs in the character. You already seem to be doing this for
"Careers". Just extend it to the other attributes. You could then have
something like:

c = Character.new
# ...
character.physical.gender
character.skills.agility
character.bio.name
character.inventory.cash

# etc

···

On Mon, Nov 14, 2016 at 11:01 AM leam hall <leamhall@gmail.com> wrote:

On Mon, Nov 14, 2016 at 1:45 PM, Xavier Noria <fxn@hashref.com> wrote:

On Mon, Nov 14, 2016 at 7:37 PM, leam hall <leamhall@gmail.com> wrote:

N00bie question. I'm working on a Class that has lots of attributes that
can be set or get. I've been using attr_accessor but am wondering if
there's a better way to store data. At this point I'm looking at two or
three dozen attributes and being extended as needed.

Sounds suspicious at first sight. Normally you don't have that many
attributes.

But that is a statistical characteristic of classes, not a hard rule. It
depends.

Knowing more about the class could help giving more concrete feedback.
Coud you paste some code? What is the class modelling? How does it look
like?

Hey Xavier, the current class is:
https://github.com/LeamHall/CT_Character_Generator/blob/master/lib/Character.rb

I'm in the middle of adding another large number of attributes, and will
probably continue to add them. Hmm...I wonder if I can use hashes for
related data and cut down on the attr_accessor clutter.

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

This is certainly the case for most OO data structures in this sort of environment. However, you are only replacing a multitude of attributes, with a multitude of sub-classes - it just makes it look simpler to get access to them, and provides more readable code. However, it is still vital to design the structures carefully as later changes can lead to an awful lot of work!

···

On 14/11/2016 7:06 PM, Raj Sahae wrote:

If this is for a game, the characters traits could be broken up into sub groups and you could use structs or hashes in composition of the character class. For example, all physical attributes could be in a "physical_traits" struct, and skills could be in a skills struct. You then have accessors for the structs in the character. You already seem to be doing this for "Careers". Just extend it to the other attributes. You could then have something like:

c = Character.new
# ...
character.physical.gender
character.skills.agility
character.bio.name <http://character.bio.name>
character.inventory.cash

# etc

On Mon, Nov 14, 2016 at 11:01 AM leam hall <leamhall@gmail.com > <mailto:leamhall@gmail.com>> wrote:

    On Mon, Nov 14, 2016 at 1:45 PM, Xavier Noria <fxn@hashref.com > <mailto:fxn@hashref.com>> wrote:

        On Mon, Nov 14, 2016 at 7:37 PM, leam hall <leamhall@gmail.com > <mailto:leamhall@gmail.com>> wrote:

            N00bie question. I'm working on a Class that has lots of
            attributes that can be set or get. I've been using
            attr_accessor but am wondering if there's a better way to
            store data. At this point I'm looking at two or three
            dozen attributes and being extended as needed.

        Sounds suspicious at first sight. Normally you don't have that
        many attributes.

        But that is a statistical characteristic of classes, not a
        hard rule. It depends.

        Knowing more about the class could help giving more concrete
        feedback. Coud you paste some code? What is the class
        modelling? How does it look like?

    Hey Xavier, the current class is:
    https://github.com/LeamHall/CT_Character_Generator/blob/master/lib/Character.rb

    I'm in the middle of adding another large number of attributes,
    and will probably continue to add them. Hmm...I wonder if I can
     use hashes for related data and cut down on the attr_accessor
    clutter.

    -- Mind on a Mission <http://leamhall.blogspot.com/&gt;
    Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
    <mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
    <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

No virus found in this message.
Checked by AVG - www.avg.com <http://www.avg.com>
Version: 2016.0.7859 / Virus Database: 4664/13405 - Release Date: 11/13/16

--
Patrick Bayford Tel : 020 8265 8376 E-mail : pbayford@talktalk.net