[Spice-devel] Coding style and naming conventions for C++

Frediano Ziglio fziglio at redhat.com
Tue Jan 30 09:47:46 UTC 2018


> 
> Hello everybody,
> 
> I'd like to discuss a few things about the coding style for C++ in
> Spice (looking at the streaming agent atm).
> 
> Trying to keep this short and concise.
> 
> 
> 1. Method names
> Currently the method names are in CamelCase throughout the streaming
> agent. Methods are basically functions attached to a class, I suggest
> we use snake_case to be consistent with the function names.
> 

yes, as we use quite some C would be surely more consistent

> It's rather confusing when you see a call like SomeObject(), which
> looks like a constructor, but you actually find out it's a method call
> from another method of the same class.
> 

there are different libraries that use this style (MFC one I remember
but is a bit unfair as even Windows uses CamelCase for C functions).

> 
> 2. Namespace names
> Although not standard (you may have different experience), usually
> namespaces are lowercase in C++. Also, they are hierarchical, I suggest
> we use that and in streaming agent we change the namespace like so:
> 
> SpiceStreamingAgent -> spice::streamingagent
> 
> or (imho better):
> SpiceStreamingAgent -> spice::streaming_agent
> 
> And stick to this scheme, i.e. lowercase and toplevel namespace
> 'spice', inside it a namespace of the component.
> 

not strong opinions, fine for me.

> 
> 3. Namespace coding style
> 
> a) Let's not use `using namespace ...` ever even in .cpp files (see
> i.e. [1]). In streaming agent we have at the beginning of every .cpp:
> 
> using namespace std;
> using namespace SpiceStreamingAgent;
> 
> For namespace std, "std::" is not a long prefix, clearly expresses the
> identifier is from the standard library and AFAIK most C++ projects use
> it this way.
> 
> For namespace SpiceStreamingAgent, I didn't even know it worked for
> definition of symbols in the namespace. First time I see it, it is very
> unusual. see b).
> 

Not clear what's unusual here.
So for every class having something like spice::streaming_agent::ClassName ?
Sounds a bit long.

For std is fine for me.

> b) Let's keep the following coding style for namespaces, i.e. for
> streaming agent:
> 
> namespace spice {
> namespace streaming_agent {
> 
> THE_CODE
> 
> }} // namespace spice::streaming_agent
> 

Sounds reasonable

> 
> We should add the guidelines to the website next to the C coding style,
> but I have no intention to be exhaustive (see [1] for how long it can
> be), let's add important cases as they come up and just use common
> sense, keep the style of the local code and codereview to keep things
> in check?
> 

Currently there's a docs/spice_style.txt file in spice-server repository
(the website on spice-space uses it).
I don't know how much of a good idea to extend it, maybe some styles
do not fit that well with C++ (like the multi line indentation you
mentioned or the always brackets in the if).

> Lukas
> 
> 
> [1] https://google.github.io/styleguide/cppguide.html#Namespaces

Frediano


More information about the Spice-devel mailing list