Adopt naming conventions that don't repeat names #2174
Closed
thomaseizinger
started this conversation in
General
Replies: 2 comments 6 replies
-
I am in favor of this. I followed the same conventions in #2059 and #2076.
I am in favor of enabling this in case we do decide to adopt this convention across the whole repository. |
Beta Was this translation helpful? Give feedback.
3 replies
-
@mxinden what do you think of the following changes in
A potentially more controversial proposal:
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
At the moment, a lot of names within modules and crates of
rust-libp2p
repeat the name of the module they are contained in. For example, thelibp2p-noise
crate exports the typeNoiseConfig
. From the perspective of thelibp2p
meta crate, this types is used as:However, to use this type, one also needs to depend on
libp2p::noise::Keypair
andlibp2p::noise::X25519Spec
.Keypair
andX25519Spec
are too ambiguous to fully import which is why I tend to import the entirelibp2p::noise
module:Now I have to make an annoying trade-off:
NoiseConfig
because - by itself - its name is unambiguous and doesn't clash with any other symbols in scope but introduce an additional line in the use statements of my file.use libp2p::noise;
import but repeat the wordnoise
upon usage:noise::NoiseConfig
. An example of this is here:rust-libp2p/examples/chat-tokio.rs
Lines 68 to 79 in 841b09a
There are many instances of this over the entire codebase (non-exhaustive):
libp2p::floodsub::FloodsubConfig
libp2p::mdns::MdnsEvent
libp2p::request_response::{RequestResponseCodec, RequestResponseMessage}
My proposal would be to aim for the following:
libp2p
crate, i.e. import names should work well if one can assume that people uselibp2p::<protocol>
. Having things be consistent when depending on sub-crates explicitly is of secondary nature.libp2p::<protocol>
module should give you types that are short, yet don't clash. In particular, I think most types within a specific protocol do not need to repeat, which module they are contained in:libp2p::request_response::RequestResponseCodec
should belibp2p::request_response::Codec
, etc.The benefit of such a structure is that it keeps the amount of imports in a file short. It is common practice to have imports at the top of a file in Rust but it is actually annoying because it is far the least interesting part of code I want to look at. I tend to sort things in files from highest to lowest importance, which makes it easier to find things.
Additionally, within contexts where no ambiguous names exist, like within the
libp2p-request-response
crate, one can easily fully-import types likeMessage
orCodec
, reducing the noise-level of the code.For reference, there is actually a clippy-lint for detecting such names: https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions
Beta Was this translation helpful? Give feedback.
All reactions