Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: data structure question

by daxim (Chaplain)
on Aug 30, 2013 at 11:49 UTC ( #1051610=note: print w/replies, xml ) Need Help??


in reply to data structure question

Don't. That's the "God object" anti-pattern.

Keep the variables separate, but move them into the smallest possible scope. That is to say, define them in the function/block where they are needed, not everything at once at the top.

Replies are listed 'Best First'.
Re^2: data structure question
by SuicideJunkie (Vicar) on Aug 30, 2013 at 14:06 UTC

    It does have its uses however.

    In my game, I have a $universe hash, containing all the ships and maps and other info. The ships contain their component layout and stats, and the components contain their shapes and stats.

    When calling functions, I only pass the sub-structure that is needed, but when it comes time to save the game or reload from disk, it only requires a single call to storable or yaml which never has to change. Much simpler than having a growing set of independent things to save.

    There are, of course, still plenty of local variables in small scopes, but anything that has to outlive the program itself goes into my $universe.

      it only requires a single call to storable

      That's the justification for this amazingly sub-optimal design? ಠ_ಠ The hash key names are tight coupling, the location of the "sub-structure" is an implicit contract. That's badly reinventing the facilities of high-level programming Perl/ecosystem provides you with. It's perfectly possible to have a universe object that refers to other objects, but the design would be much more robust against changes.

        The substructures are just unblessed object references to keep it simple. :)

        I'm not sure what you mean by worrying about the key names or location of the substructure... each portion of the code concerns itself with one layer and knows where it keeps its own things. The mapview has a 2d hash with ships in it, and doesn't care about the details. The shipview has a ship ref with components in it, and doesn't worry about the details. The aim screen has a single component ref and is at the bottom level of detail (its also stacked on top of a mapview so you can see what you're aiming at).

        Its not an *enforced* separation, but I like the straps on my straightjackets to be loose :)

Re^2: data structure question
by Anonymous Monk on Aug 30, 2013 at 11:51 UTC
    thanks, that was an encouraging answer!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051610]
help
Chatterbox?
[Eily]: very useful default translation :P
[Eily]: BTW LanX, you should try typing a few random chars at the beginning of each message. This will prevent expansion :P
[LanX]: qwiud you sthink so?
[LanX]: zxwqbd good idea! :)
LanX embraces his new habit spqopiwjdnq
[ambrus]: qQUkZTmHTuKxStGT- BzTIK9gdudif7TkTLI t3mnF144UaAZjkknXY 8nN-QM19wHBsTrp5vB lEYU_Kksa7X1RIBB4x EWLD5X7SW3jGX5ryfN OMn_yL5FTdQxzjhtyX mKN9sjUCzBNHK5Rrp0 S2WMUvIb1i9aZFgjtq VR0GH1bjPMvm1G16iz hBqc1U6toPd4FbJOFj VsOeT745AN1_pO88rD SRAYKtBZwCZedESZmN mvutrOTHiSNwflB- pRfn_k
[Eily]: so far it seems to work
Your Mother reminds the monks they should be grateful not to share an office, lest they be subjugated to constant inanities like, "Czech please!"
[LanX]: what's strange is that the "Cowboy you said this already" message is missing #dqiwd
[LanX]: YM: BTW learn to mute your humanity

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2017-03-27 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.