Re: problems with the scoping in eu4

new topic     » goto parent     » topic index » view thread      » older message » newer message
bill said...
declare  
  integer sum = 0 
begin 
  loop 
    f = ... 
    exit when f = 0 
    sum += f 
    declare                 
      integer x  
    begin	   
     ...                      
    end 
    ... 
  end loop 
end 

In programming you should keep your scopes and methods as small as possible by nature. If a function begins to take on more that a loop or two, then that's a pretty good sign it needs to be split. Rarely have I seen a case in Euphoria that the above is really necessary. For example:

function compute_sum(sequence nums) 
    integer sum = 0 
 
    for i = 1 to length(nums) do 
        sum += nums[i] 
    end for 
 
    return sum 
end function 

i.e. the containing block has always been suitable. We have discussed a block ... end block in the past. That may come in the future, but I personally do not see it as a big priority. Now, I think you're missing the point a bit with declaring values inside of a loop, here is a sample I posted before where this is valuable, and this scenario I run into all the time unlike having problems of the containing scope being too large for my likings as described above in your quote and my example above.

sequence names = { "John Doe", "Jane Doe", "Jim Doe" } 
 
for i = 1 to length(names) do 
    sequence name = names[i] 
    sequence first_name = get_first_name(name) 
    sequence last_name = get_last_name(name) 
     
    -- do a bunch of stuff w/first and last name 
end for 

That is a valid and very common pattern in coding that I have been involved with.

Jeremy

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

Search



Quick Links

User menu

Not signed in.

Misc Menu