REBOL Programming/Design Guide/Think Simple

< REBOL Programming < Design Guide

Yes, think simple, but not simply.

Most beginner scripts are too complicated.

It is more difficult to write something simple and elegant.

Small, simple scripts are easier to read and maintain.

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." -Antoine de Saint-Exupery

For example to return the days in a month you could write something like:

data-block: [
   "January" 31 "February" 28 "March" 31 
   "April" 30 "May" 31 "June" 30 
   "July" 31 "August" 31 "September" 30 
   "October" 31 "November" 30 "December" 31
]
month: {June}
print first skip find data-block month 1

It works .... but is a bit long for what it provides.


This should be easier to follow and maintain:

days-in-month: [
  January 31 February 28 March 31 
  April 30 May 31 June 30 
  July 31 August 31 September 30 
  October 31 November 30 December 31
]
print days-in-month/June 

Or we don't need to define a variable for the month block:


 print select [
  January 31 February 28 March 31 
  April 30 May 31 June 30 
  July 31 August 31 September 30 
  October 31 November 30 December 31
 ] 'June

Now you might decide to make this a function so that it can be re-used or you might have that sneaking suspicion that because you needed this function someone else has already written it.

If you decide to make it a function, you now have the ability to add the leap-year decision.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.