Replies: 1 comment 1 reply
-
|
Hi @pavpen, thanks for your feedback! Just as Rust don't force you to name traits a certain way, CGP as a library also don't enforce how you should name a provider or consumer trait. Currently, the convention is that if there is no suitable noun, we add a So feel free to choose whatever naming convention that suites you, especially during exploration phase. You can think about renaming the constructs later on, when your code is mature enough to be shared with others. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, CGP community.
This may be a rather basic newbie question. (I'm slowly learning how to replace lengthy manual code with CGP patterns. Perhaps, a tutorial with small example projects like in The Rust Programming Language book would be helpful.)
I find the style of naming CGP providers as nouns like
ActionPerformer, but the trait they annotate as an adjective phrase likeCanPerformActionto introduce some language, and automation obstacles. For example:WhatToNameThisProvider?UsbDeviceLister: According toContext-Generic Programming Patterns (DRAFT) examples,
this component's provider should be named something like
UsbDeviceLister, except 'lister' in English doesn't have the expected meaning.UsbDeviceEnumerator: I could rename it to, e.g.UsbDeviceEnumerator. However, now the trait name (as well as the trait function name), and the provider trait name are more un-related. (It's harder for someone reading the code to find the trait for the provider, or vice versa).UsbDeviceEnumerator: I could rename the trait toCanEnumerateUsbDevices, and the trait function toenumerate_usb_devices, except that 'list' may be the expected name by API users, and renaming it may also make developers' work slower. (E.g., see'List' under 'Standard Methods' in §Retrieve a collection in the Google API Design Guide.)
UsbDeviceGetter: Other, API design guides may use 'get' instead of 'list' for the whole collection. However,UsbDeviceGetternow has a misleading name, since it sounds like an accessor for a single device.UsbDevicesGetter: We can modify English grammar for the purposes of engineering to distinguish between an accessor for a single resource, and for a collection.Sidebar on whether the above example should be an accessor
There could be some debate on whether the example trait above defines a getter, in which case it may look more like:
However, a getter is often very cheap to execute, and the returned value doesn't usually change unless the API user,
calls a corresponding setter, or other modifying function. Since neither of these are true in this case (listing USB devices typically involves a kernel call, which has to suspend the current thread, and switch context; and devices can be plugged in, and unplugged, etc., which can change the returned list), I prefer to make this explicit by not making this look like an accessor. (This is admittedly my judgment.)
I think part of the issue is that what the CGP Patterns book calls a 'provider trait' seems to correspond more to what a user of a Dependency Injection framework may call an injectable service trait (or interface, if this was in an object-oriented language). (There's a similar issue with the naming of the
cgp_componentannotation macro.)Wouldn't it be simpler to use a style like:
Then the
cgp_componentmacro can automate generating the 'provider trait' (or, 'service trait') name by appendingServiceto the name of the trait (and removing theHas, orCanprefix).As another sidebar, according to examples from
Discuss naming conventions for traits,
traits that express the ability to perform an action are named without the
Canprefix, at least in the standard library. (E.g.,std::hash::Hash, orstd::iter::Extend.) However, I do findwhere Context: CanListUsbDevices + HasErrorTypevery clear to read.Beta Was this translation helpful? Give feedback.
All reactions