# Choosing good names

**URL:** <https://racket.discourse.group/t/choosing-good-names/2900>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [May 3, 2024, 8:33pm UTC](https://racket.discourse.group/t/choosing-good-names/2900 "2024-05-03T20:33:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendrikboom3](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hendrikboom3/32/2748_2.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)\
**Post date:** [May 3, 2024, 8:33pm UTC](https://racket.discourse.group/t/choosing-good-names/2900/1 "2024-05-03T20:33:05Z")

</div>

Choosing good names is said to be one of the most difficult things to so in programming.  
I'm looking for some advice here.

Let's say I want to talk about an onion in a Racket program. It's somewhat obvious to call it

```scheme
onion

```

But onions are data structures, specifically, a struct:

```scheme
(struct onion (layer pungency))

```

Now this gives us two uses for 'onion'. It's a structure, and a constructor, and a constructee. In Typed Racket it will likely be a type as well.  
Clearly. I need a naming convention to distinguish the multiple meanings.  
Has anyone figured out a natural way to name such things?  
And what notation should one use in comments to show one is talking about the things called onion in the program rather than just referring to natural-world root vegetables?

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [May 3, 2024, 11:33pm UTC](https://racket.discourse.group/t/choosing-good-names/2900/2 "2024-05-03T23:33:36Z")

</div>

HtDP approximately uses “the onion constructor” “the onion structure type” and “an instance of onion” (or longer “an instance of the onion structure type”).

---

<div class="post-metadata">

**Author:** ![cadence](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/cadence/32/998_2.png) [@cadence](https://racket.discourse.group/u/cadence)\
**Post date:** [May 6, 2024, 6:22am UTC](https://racket.discourse.group/t/choosing-good-names/2900/3 "2024-05-06T06:22:56Z")

</div>

> [@hendrikboom3](#):
>
> I need a naming convention to distinguish the multiple meanings.

I found myself wanting to give instances the same name as the struct often enough that I created my own (_very_ unofficial) style to distinguish between structs and instances. I suffix `^` to struct names. So I would do:

```scheme
(struct onion^ (layer pungency)) ; struct is named onion^
(define onion (onion^ 3 'strong)) ; instance is named onion
(println (onion^-pungency onion)) ; both can be used together

```

It works well for me.

> [@hendrikboom3](#):
>
> And what notation should one use in comments to show one is talking about the things called onion in the program rather than just referring to natural-world root vegetables?

I just write `onion` in the comments. The surrounding context of the code and the comments generally makes it pretty clear what I mean. If I confuse myself, I've probably picked a bad name for the variable, so I rename it.

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [May 6, 2024, 3:02pm UTC](https://racket.discourse.group/t/choosing-good-names/2900/4 "2024-05-06T15:02:57Z")

</div>

I've tried both:

1. `onion` for the struct name and `the-onion` or `an-onion` for an instance name.

2. Typed Racket style `Onion` for the struct name and then just `onion` for an instance.

I don't love either. But if I solo bikeshed on stuff like this for _too_ long, I try to slap myself. 😄

Often I do 1, `onion` struct, and when the local context is small/obvious, I'll name an instance variable `o` or even `v`. (To me, `(define (peel-the-onion o) ___)` is clear enough. YMMV.)

* * *

Aside from style, it's important to realize that Racket will happily let you name the struct `onion`, and shadow that with a variable named `onion`. If you _think_ you've locally defined an `onion` variable, but you didn't, you won't get an undefined variable error using `onion`. But eventually at runtime your code will misbehave thinking it's using a struct instance value, but instead gets a constructor procedure value. Depending on proximity, this can be tricky to figure out and fix. Ask me sometime. 😄

So there's a good reason to pick different names, beyond style.

On a non-trivial project with multiple people, I might try to get consensus on something like `Onion` and `onion`, or whatever the team prefers.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [May 6, 2024, 6:16pm UTC](https://racket.discourse.group/t/choosing-good-names/2900/5 "2024-05-06T18:16:17Z")

</div>

Even `(define (peel o) ___)` in `onion.rkt` with `(provide (contract-out [peel (-> onion?___)]))` is reasonable for me, but the rest of the comments on bikeshedding are well-said. The closest to convention I've seen is `the-onion`/`an-onion`.
