# Getter function naming: Consider avoiding nouns

**URL:** https://discourse.julialang.org/t/getter-function-naming-consider-avoiding-nouns/101832
**Category:** General Usage
**Tags:** function, style, accessors
**Created:** [July 20, 2023, 11:37am UTC](https://discourse.julialang.org/t/getter-function-naming-consider-avoiding-nouns/101832 "2023-07-20T11:37:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stemann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stemann/32/4030_2.png) [@stemann](https://discourse.julialang.org/u/stemann)
#### Post date: [July 20, 2023, 11:37am UTC](https://discourse.julialang.org/t/getter-function-naming-consider-avoiding-nouns/101832/1 "2023-07-20T11:37:35Z")

</div>

I am (heavily) considering judging using nouns for (exported) getter accessor functions bad practice.

I’ve found myself writing structs with getter accessors named equal to the names of the fields they are getting, i.e. using nouns (singular and plural form), e.g.:

```julia
module Foos

export Foo,
    colours,
    shape

struct Foo
    colours::Vector{String}
    shape::String
end

colours(f::Foo) = f.colours
shape(f::Foo) = f.shape

end

```

since it seems very nice functional-style at first (e.g. enabling code like `if shape(foo) == "round" # ...`.

But then I often also would like to write code that uses that same nouns as a (local) variable names, e.g.:

```julia
using Foos
using Test

@testset "shape accessor" begin
    f = Foo(["blue", "green"], "round")
    shape = shape(f) # Yuck!
    @test shape == "round"
end

```

which yields the, slightly hard to understand error:

```julia
     Testing Running tests...
shape accessor: Error During Test at test/foo_tests.jl:6
  Got exception outside of a @test
  UndefVarError: `shape` not defined

```

The inverse calling order

```julia
using Foos
using Test

@testset "shape accessor" begin
    shape = "round"
    f = Foo(["blue", "green"], shape)
    actual_shape = shape(f) # Yuck!
    @test actual_shape == shape
end

```

(of course / thankfully) yields a more understandable error:

```julia
shape accessor: Error During Test at test/foo_tests.jl:7
  Got exception outside of a @test
  MethodError: objects of type String are not callable

```

Executing the equivalent in the REPL yields:

```julia
julia> using Main.Foos

julia> f = Foo(["blue", "green"], "round")
Foo(["blue", "green"], "round")

julia> shape = shape(f)
ERROR: cannot assign a value to imported variable Foos.shape from module Main
Stacktrace:
 [1] top-level scope
   @ REPL[9]:1

```

If there is consensus on this, I am pondering whether this might worthy of a note in the style guide…? (even though there are surely lots of functions in Base etc. using (common) nouns for method names…)

Related discussion:

- Preferring property access over accessor methods: [Is accessing the fields of a struct a good julia-style?](https://discourse.julialang.org/t/is-accessing-the-fields-of-a-struct-a-good-julia-style/23623)

---

<div class="post-metadata">

### Author: ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)
#### Post date: [July 20, 2023, 11:54am UTC](https://discourse.julialang.org/t/getter-function-naming-consider-avoiding-nouns/101832/2 "2023-07-20T11:54:22Z")

</div>

Hi @stemann,

I am also struggling with this dilemma since long, but I am tending to use nouns for the getters, i.e., `shape(a)` instead of `getshape(a)`. Then I use `shape_a = shape(a)` for the local variables or e.g. `myshape = shape(a)` if variable `a` has a much longer name. In any, case I also see the point for preferring `getshape`.

---

<div class="post-metadata">

### Author: ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)
#### Post date: [July 20, 2023, 12:53pm UTC](https://discourse.julialang.org/t/getter-function-naming-consider-avoiding-nouns/101832/3 "2023-07-20T12:53:37Z")

</div>

I prefer to use getter methods instead of property access. Often, i define the methods with the same name as the property but with a `of` at the end, like:

```julia
shape = shapeof(thing)
color = colorof(thing)

```
