-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document preferred naming convention for getters/setters in style guide #16770
Comments
In Bio.jl, we use |
As I recall, using |
That's fine with me, that's also the convention used in DataArrays and DataFrames. Though I just noticed NamedArrays follow the @davidavdav Would you be OK with switching to the |
My concern with this naming convention is that it is very easy to shadow
getters with local variables that have the same name. Any thoughts on how
to avoid that?
|
Though I agree name conflicts are often annoying, I'm afraid the getters naming convention is too established to be changed at this point. |
So then we have As an unrelated topic, is this a moment to consider lobbying for a syntax like |
No, the plural is not the question here. The debate is
As you say, that's quite unrelated, so I'd better keep this debate separate. I have no idea when is the best time for this, though. |
Oh, all right then. The question is then to change In the particular case of fieldname |
OK, cool. Only some methods for |
yes, it would be nice if there is a general solution for these cases. Similar problems occur with ambiguities, when two packages extend a method from base in different ways. |
Personally I prefer the e.g. for the linked issue, it was completely unclear to me what |
Another argument is that the unofficial convention in Base seems to be if both |
I am happy enough with the |
While the earlier patterns of use were as I mentioned get: ( ) or get_( ) When paired wirh the All considered, use of get___ with set___ for individual fields of a type (and for implicit/computed/composite entities that inform or enhance a type) is more sanity-preserving and less inviting of incorrectness. If set___ stays, I am using get___ as this keeps coding with Julia nearer the immediacy of expressing design. |
Honestly speaking, I think setter and getter interfaces should be achieved by overloading |
Please keep this issue focused on the naming convention. It's kind of orthogonal whether we recommend using field overloading later: right now there are many setters and getters out in the wild, and we need a convention for them. |
Indeed, having a consistent convention will make it much easier to transition to field overloading later. |
@StefanKarpinski I think you'll have to make the decision if you want this thread to give a result before 1.0. ;-) |
Sometimes, using julia> using DataFrames
julia> data = DataFrame(A=[1,2], B=[3,4])
2×2 DataFrames.DataFrame
│ Row │ A │ B │
├─────┼───┼───┤
│ 1 │ 1 │ 3 │
│ 2 │ 2 │ 4 │
julia> names(data)
2-element Array{Symbol,1}:
:A
:B
julia> names = ["Julia", "R", "Python"]
WARNING: imported binding for names overwritten in module Main
3-element Array{String,1}:
"Julia"
"R"
"Python"
julia> names(data)
ERROR: MethodError: objects of type Array{String,1} are not callable
Use square brackets [] for indexing an Array.
|
Yes, sometimes I wish I would be possible to have variables and functions under the same name, but this can't happen because the value of a variable can be a function. (And it might be confusing to read the code). With the specific |
Name collision between local variables and functions can be avoided by prefixing the module name of function; you can always use |
I'm inclined to think |
There is a point to be made for function names being (based on) verbs, which would indicate to prefer Also, without the |
|
That discussion is likely better to have in #1974. |
I think it would be useful to add a recommendation regarding the naming of getters and setters to the style guide here: http://docs.julialang.org/en/latest/manual/style-guide/#use-naming-conventions-consistent-with-julia-s-base
For getters it looks like the rule is pretty clear: just use the name of the field, e.g.
names(x)
. For setters, I'm not sure we're clear on whethernames!(x)
orsetnames!(x)
is preferred. Cf. this thread. Could we agree on a convention? If that's still undecided, probably better make a quick survey to choose the rule most commonly used in packages.The text was updated successfully, but these errors were encountered: