Jump to content

orginization and nesting of methods

- - - - -

  • Please log in to reply
1 reply to this topic

#1
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Hey everyone

My current task is to connect to a database, download and parse some xml, and then upload it somewhere else. The uploading I am doing in another class, but the downloading and parsing I am keeping together. This means that I will have several methods, and as I was planning my program, I realized theres two ways to go about it. I could either create a method and then incorporate it in the next one, or keep all the methods seperate and combine them in one driver method. What I mean is, say my methods are:

def downloadStuff()
def parseXml()
def sortOutUser()
def sortOutUsers()

I could include the methods together , e.g.

sortOutUsers:
contains sortOutUser

and sortOutUser:
contains parseXml

and parseXml:
contains downloadStuff


so I would run sortOutUsers and in it, it would run all the helpermethods, if you see what I mean.

Or, I could seperate them, and save the data as say instance variables, and then just pass them in. For example,

this.stuff = DownloadStuff()
this.parsedXml = parseXml(stuff)
this.SortedUser = SortUser(parsedXml)

...etc

I think I'm at the codemonkey stage. I'm confident enough in my coding ability to take on pretty much anything, but I'm still unsure of how to code elegantly and cleanly.

Sorry if this doesn't seem to make sense. I'm leaning towards the second way of doing it, but was wondering if theres any code veterans out there can say for sure?

#2
tormaroe

tormaroe

    Newbie

  • Members
  • PipPip
  • 15 posts
Yes, the second approach is best. The reason has to do with coupling. When the methods don't know about each other, they can be used separately. This means they can more easily re-used in similar but not identical situations. But probably more importantly, they can be developed and tested individually.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users