1. Variable Namespace Best Practice Question
- Posted by euphoric (admin) Jul 25, 2019
- 1137 views
- Last edited Jul 26, 2019
*"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.
2. Re: Variable Namespace Best Practice Question
- Posted by Bhupen1277 Jul 25, 2019
- 1108 views
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.
3. Re: Variable Namespace Best Practice Question
- Posted by ghaberek (admin) Jul 25, 2019
- 1100 views
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.
P.P.S. I think you meant "scope" instead of "namespace"
4. Re: Variable Namespace Best Practice Question
- Posted by euphoric (admin) Jul 26, 2019
- 1064 views
My initial reply was deleted! Or vanished somehow!
Anyway...
I'd say the preferred method would be this:
sequences lines = read_file(myfile) for line in lines do ...
Agreed! Let's get it!
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.
P.P.S. I think you meant "scope" instead of "namespace"
Indeed.