Dealing with lots of traits with long names

A colleague of mine just generalized my program using traits. (You can tell I'm not a RealProgrammer™ because I like what he did to my code.) These traits have really long, meaningful names, which I think is a good thing, and there are lots of them. I'd like to find a way to make the code more readable.

Say I have

struct Foo<T: Trait1<Trait2>, U: 'static + Clone + Trait3, V: Trait4<Trait5>, W: 'static + Clone + Trait6{ ... }

and

impl<T: Trait1<Trait2>, U: `static + Clone + Trait3, V: Trait4<Trait5>, W: 'static + Clone + Trait6 { ... }

except each of the trait names, including T, U, V, and W, is 10 characters or more long. For example, the impl statement is 330 characters long.

I could give each of the long names a short alias, but that defeats their purpose. Is there a way to use a newtype or type alias to improve readabiity?

1 Like

Aside from the where keyword (I use the where syntax for any generic types with more than one constraint), I can see options in removing constraints from type definitions and using associated types on the traits instead of generic types. But neither of those are always possible or appropriate.

It is sometimes possible to abbreviate names without losing clarity by finding words that are common to multiple names and abbreviating them each time they're used. Names like DatabaseQuery, DatabaseMutation and DatabaseMutationResult can be shortened to DbQuery, DbMutation and DbMutationResult. But DbQ and DbM would be overly sacrificial.

Good shorthand names work too. Cow seems like a weird name for a type, but when you learn what it means it becomes easy to read. HeapAllocated would be an appropriate name for Box, but someone took the chance and used a generalized term for a specific type. The result is great. InteriorMutabilityWrapper may have preceded Cell. The standard library is an excellent lexicon and it shows that sometimes an analogy is clearer than a literal name (assuming the documentation is good).

5 Likes

Thanks for letting me know that I'm not a compete idiot for not being able to find a way to shorten these statements using newtype or alias. For now I think I'll just break up those very long lines. They'll still read like run-on sentences, but perhaps they'll look like poetry.

1 Like