1. Variable Namespace Best Practice Question

*"Namespace" in Subject line should be "Scope"

Is it better to do this

sequence lines = read_file(myfile) 
sequence line 
for t=1 to length(lines) do 
    line = lines[t] 
... 

or this

sequences lines = read_file(myfile) 
for t=1 to length(lines) do 
    sequence line = lines[t] 
... 

When it comes to standard Euphoria programming practice, which is preferred?

I'm concerned mainly with the declaration of the line variable.

new topic     » topic index » view message » categorize

2. Re: Variable Namespace Best Practice Question

Obviously pre-defining is the most standard way of defining "line" variable. I would also suggest most humbly that since you have already defined "lines", use a different name for the second variable - something like "oneline". It would be less confusing.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Variable Namespace Best Practice Question

euphoric said...

When it comes to standard Euphoria programming practice, which is preferred?

I'm concerned mainly with the declaration of the line variable.

Well, since you asked, I'd say the preferred method would be this:

sequences lines = read_file(myfile) 
for line in lines do 
... 

But that'll have to wait. So I would prefer this:

sequence lines = read_file(myfile) 
for i = 1 to length( lines ) do 
    sequence line = lines[i] 
... 

Performance-wise, there really won't be a difference, because the backend is going to reserve memory and reuse for each assignment.

So you don't incur the penalty of repeated malloc/free calls. And there's an advantage to keeping the variable private to the scope of the for loop.

-Greg

P.S. read_lines() returns separate lines. read_file() returns the whole file as one big string. blink

P.P.S. I think you meant "scope" instead of "namespace"

new topic     » goto parent     » topic index » view message » categorize

4. Re: Variable Namespace Best Practice Question

My initial reply was deleted! Or vanished somehow!

Anyway...

ghaberek said...

I'd say the preferred method would be this:

sequences lines = read_file(myfile) 
for line in lines do 
... 

Agreed! Let's get it!

ghaberek said...
sequence lines = read_file(myfile) 
for i = 1 to length( lines ) do 
    sequence line = lines[i] 
... 

Performance-wise, there really won't be a difference, because the backend is going to reserve memory and reuse for each assignment.

So you don't incur the penalty of repeated malloc/free calls. And there's an advantage to keeping the variable private to the scope of the for loop.

Thank you! That's what I was wanting to know.

ghaberek said...

P.P.S. I think you meant "scope" instead of "namespace"

Indeed.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu