From: Holden Glova Date: 2002-01-14T15:09:52+09:00 Subject: Re: Small Methods - a ramble -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Mon, 14 Jan 2002 11:28, Ron Jeffries wrote: > I noticed in some code that Chet and I were writing that, as Smalltalkers, > we tend to write really tiny methods. Here's a patch of code to show what I > mean. Pay no attention to what it does, just look at how it looks. > > def filesUnderManagement > names = (Dir.entries(@directoryName).select { | each | ! (/.bak/ =~ > each) }).sort names.reject { |each| FileTest.directory?(sourceName(each)) } > end > > def archiveFiles > Dir.entries(@directoryName + '/archive').reject { | each | /^\./ =~ > each }.sort.reverse end > > def backup > timeString = Time.now.strftime("%Y%m%d%H%M%S") > filesUnderManagement.each { | each | backupFile(each, timeString)} > end > > def backupFile (fileName, timeString) > File.syscopy(sourceName(fileName), > archiveName("#{fileName}.#{timeString}")) end > > def sourceName(fileName) > @directoryName + '/' + fileName > end I wish one of you XP book writers would put an example like this actually in your books when something like small methods is mentioned :) Thanks for the example Ron - I had no idea when you said small, you meant that small, but I like it. Part of the problem I have is, while I'm writing code I'm not exactly sure what I can call every small section of the code. To me that is very hard. This isn't to say that I don't want to write small methods, it just takes me longer to do it. And of course with people like management saying 'go faster!' I definately don't make my methods as small as you have shown above. I do find it very readable though. Same goes for splitting code out into a new class. Can you show some granularity for your classes? I wonder if it is also readable because the code doesn't have excess clutter that I call 'helper' code. Code that is required to assist in your job but isn't actually the code doing the job. I find there is lots of this sort of code in Java. Thanks for the eye-opener. - -- Signed, Holden Glova -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8QnbT+mF116Lw2cQRAl54AKCDQ58T4k/aMdHowWBZecyttq/AGQCeMpae 5ASLIoWW2ofRhY0m2g5rq3o= =Cscc -----END PGP SIGNATURE-----