common code patterns

Monte Goulding monte at appisle.net
Thu Aug 10 17:28:54 EDT 2017


> On 11 Aug 2017, at 7:02 am, Richard Gaskin via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> Except that's wrong. :)
> 
> I tend to use 0 as the initial value, and increment at the top of the loop:
> 
>  local tIndex
>  put 0 into tIndex
>  repeat for each line tLine in tLines
>      add 1 to tIndex
>      — code
>  end repeat
> 
> I've found two benefits from this habit:
> 
> 1. (minor) Clearer distinction between unincremented and incremented counter.
> 
> 2. (major) I can "next repeat" at any point in the block and know that the counter is appropriately updated.
> 
> Indeed, it was a runaway loop many years ago that set me know this path.

Aha… well it probably depends on what you are using tIndex for but yes your pattern is much more reliable if tIndex is meant to represent the line number. If on the other hand tIndex is meant to only be incremented if you haven’t exited the loop at some point while processing tLine then you may want the other pattern so perhaps both have value as examples but I’m not sure how one would name the two different patterns. I guess you could do this which is probably best:

 local tIndex
 put 0 into tIndex
 repeat for each line tLine in tLines
     — code for ensuring tIndex should be incremented
     add 1 to tIndex
     — code for using tIndex
 end repeat
> 
> 
> 
> All this raises a bigger question:  can we for now provide some sparse minimums and perhaps consider adding more later if truly needed?
> 
> Saves work for you folks, and obviates the need to have what would likely be really long threads here about different coding styles (or whether all possible styles should be supported in Prefs  - "Damnation to the dangling 'thens'!" <g>).

Sparse minimums are fine. I just need a few examples for the moment. Down the track once you all see _why_ I’m asking then we can expand on things a bit ;-)

Cheers

Monte


More information about the use-livecode mailing list