Rev Mentor

Tips, tricks, news & commentary for Revolution developers 
Filed under

scripting

 

Get the parent group of a control

By being binary (staying within the Rev engine as much as possible) you can extract the parent group of any object with just a few lines of code and NO repeat loops or complicated, incrementing offset functions.

Here's what you need to do:

  1. First get the long name of the object.
  2. Put all references to a group within the long name on a separate line.
  3. Replace " of group " with a return and "group " to do this.
  4. Filter out all lines but the ones starting with "group ".
  5. If, after the filtering, there are still lines, then you have a parent group.
  6. The last line is the "parent" of the object, otherwise, it has none.
That'll do it.

 

Loading mentions Retweet
Filed under  //   lesson   scripting  
Posted by Jerry Daniels 

Comments [0]

Scripting - it all starts with a name

Get the Flash Playerto see this player.
(download)

The first step to writing great Revolution scripts is often relegated to after-thought. I'm referring here to the naming of things. What's the big deal?

The true value of your code is determined more by an endless series of small naming decisions than any other single act.


NAME IT WHAT IT IS

I strongly recommend naming a thing exactly what it is--based on what it does or has done to it. That means using "it" repeatedly in a script would be verboten. Why? Because "it" tells us absolutely nothing about the data to which it points. 

The same principle applies to using "x" or "i" as an index variable in a repeat loop. What does the var "i" tell you?

repeat with i = 1 to n
   put i && the date into line i of fld 1
end repeat

Wouldn't the following tell you more:

repeat with theLineNo = 1 to theNoLines
   put theLineNo && the date into line theLineNo of \
      fld "list of dates"
end repeat

This makes your code self-documenting and much easier for you and your antecedents to read. It also establishes a higher market value for your code, app or company, provided you follow your own standards consistently.

 

NAMING A VARIABLE

When Dan Winkler invented HyperTalk (the basis for Revolution's scripting language) he would call a variable that contained the today's date the following: theDateToday. He would not have used tDate.

Scripters started prefixing vars with single char abbreviations to indicate what type of var they represented: t = temporary; g = global; l = local; a = array; k = constant. A worthy goal and I agree with much of it, actually--just not the business with temp vars. Using "the" as a prefix makes the code more readable, IMO.

My rationale goes as follows: gratuitous use of single character pre or postfixes serve to make the scripting language more codified and cryptic than it needs to be. I suggest using only as much mumbo-jumbo as absolutely necessary. Prefixing a global with a "g" makes sense, if you think you need to use globals, that is.

Some folks name the parameters of a function, command or system message handler with a lower case "p" preceding them. Why? No one can answer this. "So I can tell if it's a parameter." What difference would that make if it was a param? None. It's decoder ring stuff. The priesthood. Ignore its call to your nescient adolescent ego. You've probably got a wife and kid. What do you have to prove?

Since Revolution eliminates the need for data typing, I see no need to include the type of data to which a var points. I wouldn't use "available_boole", but I would use "isAvailable" because it reads very nicely in an "if" statement like this:

if isAvailable then
   doReorder
end if

I think you can see where I'm going with whole line of reasoning. Proper naming can not only create readable code, but it makes for maintainable, saleable code.

 

NAMING A HANDLER

Why not name a custom handler with the same criteria in mind? Keep in mind how the name of a handler will look as you call it from another script.

This handler...

function todaysDate
   return the date
end todaysDate

...will look like this when you call it:

 

put todaysDate() into theDate

function stockIsAvailable
   put the number of lines in fld "stock" is not empty \
      into haveStock
   return haveStock
end stockIsAvailable

Name a variable or handler properly, and it works for you. Otherwise, you end up working for it.

Loading mentions Retweet
Filed under  //   code standards   coding standards   lesson   scripting   video  
Posted by Jerry Daniels 

Comments [0]

Innovative filtering in lists

Get the Flash Playerto see this player.
(download)

Innovative use of filter command and lineoffset function makes it fast and easy to navigate a list strategically.

Loading mentions Retweet
Filed under  //   lesson   scripting   video  
Posted by Jerry Daniels 

Comments [0]

Scripting: being binary

Lecture on getting the most out of the Revolution engine by using fewer text call-backs to it.

Get the Flash Playerto see this player.
(download)

Loading mentions Retweet
Filed under  //   lesson   scripting  
Posted by Jerry Daniels 

Comments [2]