# Naming variables and constructor functions

**URL:** <https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222>\
**Category:** General Usage\
**Tags:** question, style, lisp1\
**Created:** [August 15, 2024, 12:29pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222 "2024-08-15T12:29:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 15, 2024, 12:29pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222/1 "2024-08-15T12:29:28Z")

</div>

This is a style question that has been bugging me for a while so I thought I would ask what others do.

Consider a composite type `Foo`, for which I expose the constructor. This is something that I like to do for non-trivial types, separating the interface from the implementation (the type may have internal fields, and various ways to construct it, I find that this serves me better in the long run).

I like to name variables (fields, function arguments, etc) as `foo`. But then I no longer find it good style to _also_ name the constructor function `foo` (even when possible). Julia is a [lisp-1](https://stackoverflow.com/questions/4578574/what-is-the-difference-between-lisp-1-and-lisp-2), so variables and functions share the same namespace.

My solution is naming the constructor `make_foo` or `calc_foo`, depending on the complexity. `make_foo` is when the constructor just packages up values and does some trivial checking. `calc_foo` is when the CPU gets a workout.

Similarly, this means that whenever I need an accessor function that gives me `foo`s, I name it `get_foo`, not `foo`. An example of bad style is something like

```julia
time = time() # within some local scope

```

which is permitted, but after that say goodbye to `time()`. I find it irksome when something similar happens in my code.

I am curious what others do.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [August 15, 2024, 1:17pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222/2 "2024-08-15T13:17:37Z")

</div>

I assume this is something you have considered already, but what opposes just overloading the constructor instead of `make_foo`?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 15, 2024, 1:18pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222/3 "2024-08-15T13:18:05Z")

</div>

I lean toward this style:

```julia
b = bar()
q = qux()

```

When function bodies are small, the short variable names are understandable and pretty readable. But for longer functions with more complicated logic, you do tend to need longer variable names…

---

<div class="post-metadata">

**Author:** ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)\
**Post date:** [August 15, 2024, 1:24pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222/4 "2024-08-15T13:24:58Z")

</div>

> [@Tamas\_Papp](#):
>
> Consider a composite type `Foo`, for which I expose the constructor. …
> 
> I like to name variables (fields, function arguments, etc) as `foo`. But then I no longer find it good style to _also_ name the constructor function `foo` (even when possible).

Is there a reason you prefer to name your constructor `foo` rather than `Foo`? So long as you leave the default constructor alone you can maintain the separation between internal representation and the interface to construct your `Foo`, or enforce an internal constructor if you prefer.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 15, 2024, 1:32pm UTC](https://discourse.julialang.org/t/naming-variables-and-constructor-functions/118222/5 "2024-08-15T13:32:06Z")

</div>

> [@\_bernhard](#):
>
> what opposes just overloading the constructor instead of `make_foo`?

For larger, more complex projects, I like to consider types (both abstract and concrete) as internal implementation details, not part of the exposed API. Once `Foo` is part of the API, all of its methods are.

Also, if I am refactoring the code, finding a `make_foo` is easier than looking at all instances of `Foo`, which might just be types for dispatch etc.

> [@sbuercklin](#):
>
> enforce an internal constructor if you prefer

I find default constructors (with `Base.@kwdef` when they have too many fields) much easier. Eg if you want the constructor to apply conversions and promotions, you have to write methods for basically boilerplate code.
