I like the fact that there is a convention to name classes with a %
suffix, because it helps differentiating instances from the higher-order class.
(define ingredient%
(class object%
(init-field name taste price color)
(super-new)))
(define (total-price ingredients)
(for/sum (ingredient ingredients)
;; here "ingredient" stands for the instance, not the class ingredient%
(get-field price ingredient)))
In the other hand, when I need a struct, I'm struggling to find names for instances that don't collide with the struct name.
(struct ingredient (name taste price color))
(define (total-price ingredients)
(for/sum (igrdt ingredients)
;; igrdt is definitely a bad name
(ingredient-price igrdt)))
;; it looks even worse in arguments
(define (taste igrdt)
(match (ingredient-taste igrdt)
(('good) "It's good!")
(('bad) "Yuk...")
(else "It's something.")))
So I started using a suffix for structs too: §
.
The symbol is easily accessible on my keyboard, alas maybe not on a standard QWERTY one, so I guess I'll change it to $
later on.
(struct ingredient§ (name taste price color))
(define ingredient (ingredient§ "Carrot" 'good 1.0 'orange))
(displayln (format "I'm making a ~a cake." (ingredient§-name ingredient)))
Whatever the symbol, I find that using a suffix for structs keeps you free to choose clear instance names instead of having to remove random vowels from your variables.
Another option would be to use %
as a prefix for structs and as a suffix for classes, so that struct accessors stay readable: (%ingredient-name ingredient)
.
Judging from a quick GitHub research: https://github.com/search?p=1&q=struct-out+language%3Aracket&type=Code
It looks like everyone is pretty much using structs as-is (except a few students using CamelCase because they're used to it).
So what can I do to differentiate instances from a struct type? Is it even relevant? Am I thinking to far? Should I ask this on Software Engineering?
The naming convention is that struct
s don't have a special notation. This may be due to struct
being more 'scheme like' than classes, units, signatures, components, etc. but I could be wrong.
There's nothing wrong with inventing something for struct
similar to classes, units, etc. But it will arbitrary and personal and therefore it should probably be documented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With