Re: Style question: Nicknames for deeply nested objects
- From: Ian <hobson42@xxxxxxxxx>
- Date: Sun, 30 Jan 2011 20:44:10 +0000
On 30/01/2011 17:51, Gerald Britton wrote:
Hi all,Neither. You should tell. Don't ask if you can avoid it.
Today I was thinking about a problem I often encounter. Say that I
have (seems I often do!) a deeply nested object, by which I mean
object within object with object, etc.
x = some.deeply.nested.object.method(some.other.deeply.nested.object.value)
Well, that's extreme but I've worked with code approaching that level
of nested-ness. Now, consider two scenarios:
1. You need to call this thing many times with different arguments, so
you wind up with:
x = some.deeply.nested.object.method(some.other.deeply.nested.object.value1)
y = some.deeply.nested.object.method(some.other.deeply.nested.object.value2)
z = some.deeply.nested.object.method(some.other.deeply.nested.object.value3)
king doesn't care where or how the butter is brought. Neither should your code!
What are you doing with value1, value2 and value3 when you have them anyway? Stuffing them 3 levels deep into something else?
Stop writing procedural code, and write object oriented code instead!
If you you make some tell deeply.nested.object about other.deeply.nested.object it can fetch its own values, but it might be better
to have some tell other.deeply.nested.object about deeply.nested.object to it can issue the correct commands.
Then you tell some to do Somthing by writing
and it all happens "under the covers".
- Prev by Date: Re: Understanding def foo(*args)
- Next by Date: homedir, file copy
- Previous by thread: Re: Style question: Nicknames for deeply nested objects
- Next by thread: Re: Style question: Nicknames for deeply nested objects