[Tutor] Function Programming Style

Alan Trautman ATrautman@perryjudds.com
Mon Jun 30 14:26:01 2003


> In addition, if you combine all of your set functions into on gigantic
> function it will be much harder to debug and/or add features.

>After reading your message serveral times: do you recommend smaller
>set/get functions or not? Maybe my initial mail lacked some
>informations.

Sorry I guess I didn't make it clear enough. Monday and all. Yes, I
recommend the small functions as a building block for larger ones.

For Examples time function in pseudo code:

Class time:
	__init__(self):
		hour = 0
		minute = 0
		second = 0

	def setHour(self, hour)
		if hour is between 1 and 12:
			self.hour = hour
		else:
			some error thing

Do the same for minute and second.

However, you might also create a function called set time to do all three at
once:

def setTime(hour, min, sec):
	setHour(hour)
	setMinute(min)
	setSec(sec)

For when you want to set all three and you do not have to rewrite the error
checking and you can combine them in any combination desired or add
additional error checking for the higher level function.


>I know Python does not need get/set but this is a common way of
>accessing variables even if they "private", isn't it?

Yes very common especially in microsoft environoments. Many formal textbooks
say it should always be done. 

I am writing a GUI application which sets some meta informations on
files. There is a class for the information and one for the GUI. The
GUI just calls fileinfo.set_date(), fileinfo.set_url() and so on.

>I now believe, this is the way to go: small, simple functions to
>fullfill the KISS paradigm.
 
I agree and one of the great things about Python is that all of the
functionality does not have to be embedded in the GUI so you can build a
fully functional non-GUI version first and attach the GUI later to split the
testing so you can determine if it is a GUI or program error.

Kai
-- 
* mail kai.weber@glorybox.de
  web http://www.glorybox.de
  pgp 0x594D4132

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor