sorting with revolution
Posted: Fri Jan 05, 2007 4:06 pm
Oliver kindly provided some code for sorting files in revolution in the last newsletter
It would be good to see that on the revDeveloper wiki or a place where comments can be made
.
The tutorial is obviously for beginner users and all efforts have been made to avoid any level of complexity. With these requirements, that's all good. The division into subroutines is well thought. The functions well commented.
However, a problem something to be careful about when learning to program is to learn good habits, even if this makes the learning a tiny bit more difficult. Habits are easier to learn than unlearn
.
I put a few comments... I welcome users of different levels of expertise to tell me what they think of them.
1) In terms of comments, the only reason to use local on top of each function is to make clear the variables that are being used within that function. Personally, I prefer to follow well accepted approaches to code commenting (javadoc and jsdoc). The advantage of this is that I can do automatic code parsing and automatically extract documentation for my code (jsdoc example). Another advantage is that you can more easily discuss your code with somebody from another programming background. Simply by reading the comments, they can understand what your program is about.
So the first function, for instance would become:
2) Rather than use a local declaration, what I do is make sure I add some data validation code at the start of the function. This helps prevents problems. This also help to clarify the expected value of the variable.
This is particularly needed in this function:
3) Variable naming. Careful variable naming is the difference between spending 3 hours or 5 minutes trying to understand some code that you have written in the past.
if I am correct, pRecurse is a boolean variable. Something which is expected to only take as value either true or false. It helps to add some prefix that clarifies this. pDoRecurse, pIsRecursing. Simlarly, folder is ambiguous. This could refer to the current one or to the full path. Here, this is about the full path... so let's write it pFolderPath. It makes understanding the logic of the code easier.
4) "if not then..." I see that construct in a lot of code within the community. Another version of this is a "if xxxx then" at the start of a function script with an "end if" at the very end of it. Even with indentation, this obscures the code. Then this forces you to keep in mind the limits under which the code is being applied all way through the function. Why not simply deal with the conditions under which the remaining of the function should or should not be applied and then simply completely forget about them. A minor comment is that a negation is always more difficult to process (cognitively speaking) than an affirmation.
If personally prefer to rewrite this like this
Anybody with such tricks for writing better code?
It would be good to see that on the revDeveloper wiki or a place where comments can be made

The tutorial is obviously for beginner users and all efforts have been made to avoid any level of complexity. With these requirements, that's all good. The division into subroutines is well thought. The functions well commented.
However, a problem something to be careful about when learning to program is to learn good habits, even if this makes the learning a tiny bit more difficult. Habits are easier to learn than unlearn

I put a few comments... I welcome users of different levels of expertise to tell me what they think of them.
1) In terms of comments, the only reason to use local on top of each function is to make clear the variables that are being used within that function. Personally, I prefer to follow well accepted approaches to code commenting (javadoc and jsdoc). The advantage of this is that I can do automatic code parsing and automatically extract documentation for my code (jsdoc example). Another advantage is that you can more easily discuss your code with somebody from another programming background. Simply by reading the comments, they can understand what your program is about.
So the first function, for instance would become:
Code: Select all
/**
* Filters the strings "." and ".." from a list
* @param {list} pList. A list with one filename per line.
* @returns A filtered list
*/
function filterDots pList
[...]
end filterDots
Code: Select all
function filterDots pList
--- validation ----------------
if pList is empty then return empty
put pList into tList
--- processing ----------------
filter tList without "."
filter tList without ".."
---
return tList
end filterDots
Code: Select all
function listFiles pFolder, pRecurse
--- validation ----------------
if pFolder is empty then return empty --- you may want to let the user know that the folder is empty.
if there is not a folder pFolder then return empty -- you may want to warn the user that the folder doesn't exist
--- processing ----------------
set the directory to pFolder
[...]
end listFiles
Code: Select all
function listFiles pFolder, pRecurse
4) "if not then..." I see that construct in a lot of code within the community. Another version of this is a "if xxxx then" at the start of a function script with an "end if" at the very end of it. Even with indentation, this obscures the code. Then this forces you to keep in mind the limits under which the code is being applied all way through the function. Why not simply deal with the conditions under which the remaining of the function should or should not be applied and then simply completely forget about them. A minor comment is that a negation is always more difficult to process (cognitively speaking) than an affirmation.
Code: Select all
if tCurrentFiles is not empty then put tCurrentFiles & return after tTotalFiles
Code: Select all
--- condition of exit ------
if tCurrentFiles is empty then return empty
-----------------------
put tCurrentFiles & return after tTotalFiles