Adopt some codified code style standards
Standards for code style are useful for reducing cognitive load when reading code. Currently we don't have a consistent standard
I'm not particularly fussed about what the standards are, but my preference is for:
- Prefix
this.on instance members - No leading underscore for instance variables
- Braces around statement blocks (after
if,else, etc)
Pretty much the default, standard Microsoft StyleCop ruleset. Happy to invert the top two as long as we apply them consistently.
It would be nice if we could check in a StyleCop/ReSharper config file. Is there another suitable formatting tool?
Those are my preferences too, and yes, I'd like that we adopt some standard, whatever it is. The only time I like breaking rule number 3 is for Guard statements (argument validation) which I prefer in a single line to reduce noise
This has come up sporadically in Gitter, glad to see Reuben turned into an issue. :) I think we should consider that one special case is "debugging via reading GitHub source code". At least I'm guilty of doing that when someone asks something and I go spelunking through the code via search.
I remember the latest discussed was using something like StyleCop.Analyzers and also add custome rules and warnings that are specific to Orleans and general to distributed systems programming (like don't do DateTime.Now in the code). That is, slowly turn Orleans into an education tool also, since that also means lower threshold for adoption and perhaps Orleans being adopoted as a teaching and research tool. That's in the gist I remember about the discussions. :) Oh, and from the early days before Gitter there's https://github.com/dotnet/orleans/issues/209#issuecomment-84278624 (we should consider closing it).
As for the specific rules, I'm inclined to use member variables without the underscore, but if marking with something, I'm OK with .this. It would help. I also like to have always braces around statemement blocks and even in that case might put them into a single line. :) Something to consider came also at https://github.com/dotnet/orleans/pull/2303#pullrequestreview-4377187, that is if there should be a difference on using aliase or the actual class for static methods.
Hmm, what else. Not everyone has R# and not everyone likes it (maybe we can recommend some Roslyn package instead?). We could use .editorconfig, I'm happy to make a PR if required.
On the XUnit.NET 2.1 release notes:
Runner utility libraries, that makes it substantially simpler for users who want to leverage xUnit.net to do non-testing activities (for example, to write a Best Practices Analyzer). Sample code: https://github.com/xunit/samples.xunit/blob/master/TestRunner/Program.cs.
Also a useful VS Extensions Trailing Whitespace Visualizer (and remover).
I'm fond of all three suggestions. But I think we should keep the rules simple. So as of rule three, I'd like no exception to guard statements. Simpler rules make it easier to follow , just in my opinion :).
And also, for naming covention, I think it is mostly pretty consistent in Orleans that we use camel case for private member and local varialbles, and camel case with first letter capital for public members. I'd like to add a rule on that.
@veikkoeeva StyleCop.Analyzer looks cool! Just checked it out. But I wasn't so familiar with it. So if a block of code violates the coding style rules, would it throw an compile exception or an compile error? I'm worrying how to enforce the style standard. I'm wandering if we should throw an compile error, otherwise people may forgot to correct their code style or not even aware of our coding standard.
@xiazen I believe it would issue either a warning or a standard compiler error, which can be repurposed mostly like compiler errors and warnings usually (some differences).
We have been following .NET Framework Design Guidelines for the general rules. I don't personally mind seeing occasional braceless conditional unless it is taken as a rule to make use of any language feature to turn code perceivably shorter (I must confess dropping the braces adds some cognitive load for me if it is anything else than a simple guard clause).
In any event, making use of Roslyn analyzers feels to me like the way to go.
The only time I like breaking rule number 3 is for Guard statements (argument validation) which I prefer in a single line to reduce noise
I'm with @jdom here - in such simple cases like argument validation or returning or throwing an exception following a stringent rule just adds unnecessary noise for the sake of just following the rule IMO.
I like all 3 suggestions otherwise.
@sergeybykov if it fits on one line, then no braces, if it would wrap (at 140 chars?) then braces. How about that? I'm easy
Please no 140 chars. 240-300 range would be much better.
Regarding the @jdom's Guard thing I don't like to look at line ends for return statements...so breaking into new lines with braces just raise readability IMO.
Adding this. is where needed only. That is the "default", otherwise just noise. Prefix only when needed, for example ctors:
public Foo(IBar bar)
{
this.bar = bar;
}
@attilah at some point you lose readability. My screen barely fits 240 chars. In the below shot, the cursor is at the 240 char mark.

I can deal with 180.
Well, the point about Guard statements is not because they are short so it's OK. The reasoning is inline with what @veikkoeeva said: cognitive load. Guard statements occurring at the beginning of the method I consider them very easy to reason about it even without braces (actually IMO it's easier, as it's less noise, and very much expected to guard against input). If you have a throw or return statement in the middle of the method, I no longer consider them simple Guard statements, and I would not suggest to put them without braces. Even if it's fits, it is a lot harder to recognize that the method is abruptly ending there via a quick scan of the code.
Regarding member prefixes, my opinion (which I'm happy to forfeit in favor of any standard), is that the this. is very valuable to understand that the method is having side effects (or reading other's side effects when reading a field). It's either that, or using a field name with a prefix such as _ (but of course I prefer this. by orders of magnitude). Effectively having a way to disambiguate whether it's a local variable or a member that you are using, without having to scroll up and down a lot when reading a few lines of code (especially harder to do when looking at a small diff in the GitHub UI).
Should we issue standard compiler error for code which violates style standard? otherwise it will be hard to enforce the code style standard.
Given that the vast majority of lines are much shorter than 140 lines, I am always using a portrait monitor. It wastes a lot less screen area on whitespace.
But it means in VS, I only see about 140 characters per line. So I prefer a limit of about 140 characters as well. I don't really see much benefit of using long lines - it is not difficult to break code into multiple lines.
Cognitive load is a very subjective topic (unless you are actually conducting user studies). To me personally, less space wasted almost always means less cognitive load (because scrolling or scanning through a larger area adds cognitive overhead) - so I prefer dropping braces if the block is a single line only.
@sebastianburckhardt
Cognitive load is a very subjective topic (unless you are actually conducting user studies). To me personally, less space wasted almost always means less cognitive load (because scrolling or scanning through a larger area adds cognitive overhead) - so I prefer dropping braces if the block is a single line only.
I can't resist to offer an extended analysis on my part, within a few phrases and though hand-wavey still. Accepting at face value there's plenty of studies that humans are pattern recognizers ("pattern seeking animals") and categorizers, to me, (I haven't really reflected on this before) in a guard clause seeing the arguments, some standard checks and standard exceptions is all good and well even without braces. In the middle of the function it's not the shortness that makes code readable, on the contrary it's pattern that lacks braces in a language they're used and I go slower over to see if it's a dangling or or not (what are the names of the variables, are there comments, is the intendantion correct, how about the lines a after the first line).
On the same token I like to write a lot comments in proper sentences and punctuation (also commas, colons etc. in code laid out according to the same rules) to the best of my ability. I think for code and comments having roughly twice the line length of optimal prose (or here), so 140 might be good choice.