REBOL for COBOL programmers
Coding REBOL like COBOL
Date written: September 27, 2012
This page is going to suggest a coding convention that might help ease you into REBOL coding and might help you organize your REBOL programs in ways that are familiar to you.
Longer upper-case data names
For the words that you choose to represent things, words which would be called data-names in COBOL, I am going to suggest that you deviate from the REBOL way of short snappy lower-case names and choose long wordy upper-case names like you would in COBOL. You might find that gives you a more comfortable place to stand when you read your programs. It will help separate your words from the REBOL functions. It will reduce the keyword-reflex when you see words like "data" and "file" that are used as data names instead of reserved words. This probably would be shocking to REBOL experts. That is why there is no feedback link on these pages.
Define your data names if it helps
Let's say you want to check the first character of the first line of a text file for the letter "t" (for the sake of an example). You could code
if "t" = to-string first first read/lines %tempfile.txt [print "yes"]What that code means is to read the file tempfile.txt as a file of lines, which will return a block of lines, then take the first item from that block which will be the first line, then take the first character of that first line, then convert it to a string, then compare it to "t." The above sample would be, I believe, pretty close to the REBOL way of coding that.
I am going to suggest that it would be OK to take a more pedestrian approach and do the following.
;; -- Working data items TEMP-FILE-ID: %tempfile.txt FILE-IN-MEMORY: "" FIRST-LINE: "" FIRST-LINE-FIRST-CHAR: "" ;; -- Load the whole file, get the first byte of the first line FILE-IN-MEMORY: read/lines TEMP-FILE-ID FIRST-LINE: first FILE-IN-MEMORY FIRST-LINE-FIRST-CHAR: to-string first FIRST-LINE if "t" = FIRST-LINE-FIRST-CHAR [print "yes"]Or something similar. Notice that you made words for various parts of the data, namely, the whole file in memory, the first line, the first character. None of that is necessary. You aren't saving that data after you check it, so you don't need these words to hold the values. You also set them to initial values, which is not necessary. Doing that is sort of like defining your data items in WORKING-STORAGE. You don't have to do any sort of data-name definition in REBOL, and there even can be problems in doing so. If you were to mis-spell one of the words so that the spelling in your pseudo-declaration was not the same as the spelling in your "procedure" code, that would not be caught because it would be perfectly legal. That is a bug that could be surprisingly hard to spot because in your mind they would be the same, because in COBOL if they weren't the same you would get a syntax error.
Those two operations of defining data items and initializing them are required in COBOL but not needed in REBOL. I am suggesting that you do them if it helps you organize you thoughts about a program you are writing. This would be a ghastly ugly jaw-dropping bit of inefficiency to a real REBOL programmer. Go ahead and do it, and then let yourself drift away from it as you feel it becoming unnecessary.