-
Notifications
You must be signed in to change notification settings - Fork 447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: Standardizing C++ Style #139
Comments
To add some more comments about Sergey Zubkov's rant: we should also note that the Google Style Guide is constantly being revised, and as Sergey himself pointed out, with the recent revisions, many of the "deal breakers" he listed no longer applies:
Currently, the key areas where the conventions used by Jsonnet differ from the Google Style Guide that are not formatting-related are:
Feel free to correct me if there are additional fundamental differences. I am ambivalent about (1) and would be fine with keeping exceptions since removing them would be non-trivial. For (2), I think the Google style guide has merit and that is better to make it clear whether a function argument is mutable. This is the same reason why Microsoft implemented SAL annotations. |
When you have exceptions you often need RAII, which used to be used to assist the GC but isn't any more I think (except in STL containers and such). I'm probably using unique_ptr or something like that in there somewhere as well. The "no exceptions" rule in Google is born out of having too much old code and doesn't apply here. Removing exceptions from the code would lead to a lot of boilerplate error propagation, which IMO outweighs the supposed benefits. At any rate I think we should stick to basic visual style like whitespace and naming conventions and not opinionated stuff like "exceptions are bad mmk" I don't feel that strongly about mutable references in function arguments, although I don't buy the argument in favor of them at all. The function signature is always immediately available (unlike some languages), and in the case of foo(x) I have to know whether x is a pointer (unless we also use hungarian notation like p_x), so it's not apparent just from looking at the call with no context that it cannot write to a param. In general the effect of a function can be arbitrary so this very thin convention doesn't buy me much. I've also was never surprised by a non-const & despite writing probably about 200kloc of C++ over the last 10 years. SAL annotations are a real "effects system" and much more powerful. They're probably too bureaucratic for our purposes. If we were writing / validating drivers it'd be different, and MS also has some cool tech for that. When it comes to function / variable names, I use:
Basically that's a classic K&R-like C style but extended to C++ in a java-like fashion, to distinguish between OO and procedural styles. So there's a lot of high-level information encoded in the names (but not hungarian notation). I've never liked the "everything is an object" concept that underpins Java, so I use procedural style unless objects are a natural fit. Whitespace:
Misc:
|
The latest version of the style guide does not say anything about forbidding RAII. I only see one mention of RAII and it is in a line that states that, as you said, exception safety requires RAII. In fact, there is definitely commonly-used Google C++ code that makes use of RAII, such as Mutex. As mentioned above, I agree that getting rid of exceptions in Jsonnet would not be worthwhile. FWIW, the reason why the the exception ban itself exists was due to actual bugs in the C++ compiler in the past. That said, in general, I am not convinced that exceptions in a non-GC language such as C++ is a good idea in general. There are plenty of reasons why exceptions in C++ are not a good idea and why new static-typed languages such as Go and Rust have decided against using exceptions. One thing I should clarify is that the Google style guide's rule against using mutable references in function arguments is to make it clear at the call site whether an argument is mutable or not. This is why the style guide points out that the problem with mutable references is that "they have value syntax but pointer semantics." There are some proposed solutions that I can point you to offline for allowing mutable references but at the same time still making the mutability explicit at the call site. In any case, these are my suggestions and my explanation for why I prefer the Google style guide. One of the main points I am trying to make is that we should not be so quick to dismiss a particular style as "terrible," especially since the style guide in its current form is still the product of a decade's worth of experience and battle-testing. Having used the style guide on a daily basis in my job, I can definitely appreciate the merits and reasoning behind the style guide. Finally, given that the Google C++ style guide is the one used for projects such as MapReduce, BigTable, Colossus, TensorFlow, as well as many other parts of the most advanced search stack in the world, I think it is safe to say that it is not such a terrible style guide after all. :) Of course, as the creator of Jsonnet, you certainly have the final say, and I respect and support the decision that you make. I've opened separate bugs to write the style guide doc (#142) and add a .clang-format (#143). |
* String token end location off-by-one error
This is a follow-up to the discussion in: #138
As @mikedanese mentioned, we should standardize on a .clang-format. Currently, the C++ style used by Jsonnet is not codified, and it is a good idea to standardize on a style guide and .clang-format.
To recap the discussion so far:
I am in favor of adopting the Google C++ Style Guide to be consistent with most other Google open source projects and to encourage more adoption internally, though @sparkprime brings up the reasons against it discussed here and prefers codifying the existing style.
Another style guide that we may consider is the LLVM Style Guide.
The text was updated successfully, but these errors were encountered: