Jump to content

Rebol Programming/Design Guide/Think Simple

From Wikibooks, open books for an open world

Yes, think simple, but not simply.

Most beginner scripts are too complicated.

It is more difficult to write something simple and elegant.

  • Strive to keep your code simple.
  • Treat every script as if it were unfinished until you have removed all unnecessary code and data.

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.

  • you don't need to hold the months as strings
  • data-block is not a very descriptive name. Maybe days-in-month would be better
  • the Rebol path notation makes the navigation to the answer direct so you don't need to use the find and worry about where you are in the series after you find the value you sought
  • you don't need to define variables like 'month' for values that can be accessed directly


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.