rant // OE scope \\ forked out of 500 Rosetta

new topic     » topic index » view thread      » older message » newer message

Forked from Re: rosettacode 500 milestone passed

It seems it all starts here. Pete and Spock have a small side conversation:

said...

Lastly, your scope rules are off, you allow eg:

for i=1 to ~s do  
   int w 
end for 
w = 0  

which in Phix terminates with "w has not been declared" (obvs. fixed by declaring w before the loop)

Pete

I once analysed the distributions of variables declared inside routines. So few private variables are used on average that it seemed to me scopes are pointless inside routines - particularly when realising that routines should be kept short and single purpose. I made the conscious decision to not include them. However I think it is useful to be able to declare variables at the point in code when they become needed, rather than interrupting the typing flow by inserting them at an earlier point in the text. YMMV but I just don't see the need to have any structural scopes inside routines. Perhaps someone could point out just where i got it wrong.

Spock

[/quote]


said...

I once analysed the distributions of variables declared inside routines. So few private variables are used on average that it seemed to me scopes are pointless inside routines - particularly when realising that routines should be kept short and single purpose. I made the conscious decision to not include them. However I think it is useful to be able to declare variables at the point in code when they become needed, rather than interrupting the typing flow by inserting them at an earlier point in the text. YMMV but I just don't see the need to have any structural scopes inside routines. Perhaps someone could point out just where i got it wrong.

Spock

You need micro scopes because certain curmudgeons use only x,y,i,j,k as the only identifiers in source-code.

A language structure is "a feature that starts with a keyword (like procedure, function, for, if, while, ..., and a few others). Each structure extends up to end <keyword>." Structures act like small detours in the natural flow of code execution. Since they are detours it makes sense that they also have some independent properties like: selective code execution, and scope. Structural scope is "the nested scope that corresponds to the extent of the structure."

For example look at the routines in the standard libary; most have parameters and many have locally declared variables. The main program is cleaner because none of these structural scope identifiers mess with the code you write.

In designing a language you have two choices:

  • find a pattern and stick to it
  • invent a "convenient" syntax for every situation

Stick to a pattern The Lisp languages do everything with nested parenthesis; the ultimate in following one pattern. Euphoria languages identify patterns and then stick with them.

Convenient syntax Conventional languages suffer from random features that seem convenient at the time the language was designed. For example the conventional + operator adds numbers but concatenates strings. The problem is that when you add up all of the "convenient" syntax choices you end up with an overall mess.

The balance between pattern and convenience is personal, which explains why there are thousands of programming languages.

In OpenEuphoria the if statement breaks the simple pattern described. Each branch of the if statement creates a new scope. That means the else keyword both starts and ends a region of scope. It would have been simpler if there was just one scope that extended to end if. Is there a good reason for having separate scopes for each branch?

You could observe that the utility of local scopes in structures is: routine > while > if. But, breaking a pattern based on frequency of use is a bad strategy.



said...

Spock said...

I just don't see the need to have any structural scopes inside routines. Perhaps someone could point out just where i got it wrong.

said...

The only two things that spring to mind are (and you did say "kept short and simple") that in an N-00 line routine

if this then  
   -- <you are adding code here>  
else  
   if that then  
      -- loads more code  
   elsif the_other then  
      integer w  
   else  
      integer W  
      -- <then you add more here>  

First, without local mini-scopes, while working up top you forget there is a w 200 lines further down and add another one -> compilation error. Not exactly what I'd call critical either, and that sort of thing can still happen with scopes.

Second, while working down below, you use w when you meant W, or perhaps that you can't rename/remove w without checking all the rest of the code. No matter how you look at it though, mini-scopes cannot be justified in routines that fit on a single screen.

Pete

Having trouble decoding this.

  • ? N-OO
  • ? example shows if you mention "routine"

I'm assuming the value of a mini-scope is to localize a throw-away local variable. The index variable in a for structure is an example of this kind of thinking. Therefore the mini-scope fits just a single screen (otherwise it is not mini).

Is your example suggesting you want to create a global w or a global W from an if branch?

_tom

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu