readability-cli 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
guides/csharp-style.md ADDED
@@ -0,0 +1,478 @@
1
+ # C# at Google Style Guide
2
+
3
+ This style guide is for C# code developed internally at Google, and is the
4
+ default style for C# code at Google. It makes stylistic choices that conform to
5
+ other languages at Google, such as Google C++ style and Google Java style.
6
+
7
+ ## Formatting guidelines
8
+
9
+ ### Naming rules
10
+
11
+ Naming rules follow
12
+ [Microsoft's C# naming guidelines](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines).
13
+ Where Microsoft's naming guidelines are unspecified (e.g. private and local
14
+ variables), rules are taken from the
15
+ [CoreFX C# coding guidelines](https://github.com/dotnet/runtime/blob/HEAD/docs/coding-guidelines/coding-style.md)
16
+
17
+ Rule summary:
18
+
19
+ #### Code
20
+
21
+ * Names of classes, methods, enumerations, public fields, public properties,
22
+ namespaces: `PascalCase`.
23
+ * Names of local variables, parameters: `camelCase`.
24
+ * Names of private, protected, internal and protected internal fields and
25
+ properties: `_camelCase`.
26
+ * Naming convention is unaffected by modifiers such as const, static,
27
+ readonly, etc.
28
+ * For casing, a "word" is anything written without internal spaces, including
29
+ acronyms. For example, `MyRpc` instead of ~~`MyRPC`~~.
30
+ * Names of interfaces start with `I`, e.g. `IInterface`.
31
+
32
+ #### Files
33
+
34
+ * Filenames and directory names are `PascalCase`, e.g. `MyFile.cs`.
35
+ * Where possible the file name should be the same as the name of the main
36
+ class in the file, e.g. `MyClass.cs`.
37
+ * In general, prefer one core class per file.
38
+
39
+ ### Organization
40
+
41
+ * Modifiers occur in the following order: `public protected internal private
42
+ new abstract virtual override sealed static readonly extern unsafe volatile
43
+ async`.
44
+ * Namespace `using` declarations go at the top, before any namespaces. `using`
45
+ import order is alphabetical, apart from `System` imports which always go
46
+ first.
47
+ * Class member ordering:
48
+ * Group class members in the following order:
49
+ * Nested classes, enums, delegates and events.
50
+ * Static, const and readonly fields.
51
+ * Fields and properties.
52
+ * Constructors and finalizers.
53
+ * Methods.
54
+ * Within each group, elements should be in the following order:
55
+ * Public.
56
+ * Internal.
57
+ * Protected internal.
58
+ * Protected.
59
+ * Private.
60
+ * Where possible, group interface implementations together.
61
+
62
+ ### Whitespace rules
63
+
64
+ Developed from Google Java style.
65
+
66
+ * A maximum of one statement per line.
67
+ * A maximum of one assignment per statement.
68
+ * Indentation of 2 spaces, no tabs.
69
+ * Column limit: 100.
70
+ * No line break before opening brace.
71
+ * No line break between closing brace and `else`.
72
+ * Braces used even when optional.
73
+ * Space after `if`/`for`/`while` etc., and after commas.
74
+ * No space after an opening parenthesis or before a closing parenthesis.
75
+ * No space between a unary operator and its operand. One space between the
76
+ operator and each operand of all other operators.
77
+ * Line wrapping developed from Google C++ style guidelines, with minor
78
+ modifications for compatibility with Microsoft's C# formatting tools:
79
+ * In general, line continuations are indented 4 spaces.
80
+ * Line breaks with braces (e.g. list initializers, lambdas, object
81
+ initializers, etc) do not count as continuations.
82
+ * For function definitions and calls, if the arguments do not all fit on
83
+ one line they should be broken up onto multiple lines, with each
84
+ subsequent line aligned with the first argument. If there is not enough
85
+ room for this, arguments may instead be placed on subsequent lines with
86
+ a four space indent. The code example below illustrates this.
87
+
88
+ ### Example
89
+
90
+ ```c#
91
+ using System; // `using` goes at the top, outside the
92
+ // namespace.
93
+
94
+ namespace MyNamespace { // Namespaces are PascalCase.
95
+ // Indent after namespace.
96
+ public interface IMyInterface { // Interfaces start with 'I'
97
+ public int Calculate(float value, float exp); // Methods are PascalCase
98
+ // ...and space after comma.
99
+ }
100
+
101
+ public enum MyEnum { // Enumerations are PascalCase.
102
+ Yes, // Enumerators are PascalCase.
103
+ No,
104
+ }
105
+
106
+ public class MyClass { // Classes are PascalCase.
107
+ public int Foo = 0; // Public member variables are
108
+ // PascalCase.
109
+ public bool NoCounting = false; // Field initializers are encouraged.
110
+ private class Results {
111
+ public int NumNegativeResults = 0;
112
+ public int NumPositiveResults = 0;
113
+ }
114
+ private Results _results; // Private member variables are
115
+ // _camelCase.
116
+ public static int NumTimesCalled = 0;
117
+ private const int _bar = 100; // const does not affect naming
118
+ // convention.
119
+ private int[] _someTable = { // Container initializers use a 2
120
+ 2, 3, 4, // space indent.
121
+ }
122
+
123
+ public MyClass() {
124
+ _results = new Results {
125
+ NumNegativeResults = 1, // Object initializers use a 2 space
126
+ NumPositiveResults = 1, // indent.
127
+ };
128
+ }
129
+
130
+ public int CalculateValue(int mulNumber) { // No line break before opening brace.
131
+ var resultValue = Foo * mulNumber; // Local variables are camelCase.
132
+ NumTimesCalled++;
133
+ Foo += _bar;
134
+
135
+ if (!NoCounting) { // No space after unary operator and
136
+ // space after 'if'.
137
+ if (resultValue < 0) { // Braces used even when optional and
138
+ // spaces around comparison operator.
139
+ _results.NumNegativeResults++;
140
+ } else if (resultValue > 0) { // No newline between brace and else.
141
+ _results.NumPositiveResults++;
142
+ }
143
+ }
144
+
145
+ return resultValue;
146
+ }
147
+
148
+ public void ExpressionBodies() {
149
+ // For simple lambdas, fit on one line if possible, no brackets or braces required.
150
+ Func<int, int> increment = x => x + 1;
151
+
152
+ // Closing brace aligns with first character on line that includes the opening brace.
153
+ Func<int, int, long> difference1 = (x, y) => {
154
+ long diff = (long)x - y;
155
+ return diff >= 0 ? diff : -diff;
156
+ };
157
+
158
+ // If defining after a continuation line break, indent the whole body.
159
+ Func<int, int, long> difference2 =
160
+ (x, y) => {
161
+ long diff = (long)x - y;
162
+ return diff >= 0 ? diff : -diff;
163
+ };
164
+
165
+ // Inline lambda arguments also follow these rules. Prefer a leading newline before
166
+ // groups of arguments if they include lambdas.
167
+ CallWithDelegate(
168
+ (x, y) => {
169
+ long diff = (long)x - y;
170
+ return diff >= 0 ? diff : -diff;
171
+ });
172
+ }
173
+
174
+ void DoNothing() {} // Empty blocks may be concise.
175
+
176
+ // If possible, wrap arguments by aligning newlines with the first argument.
177
+ void AVeryLongFunctionNameThatCausesLineWrappingProblems(int longArgumentName,
178
+ int p1, int p2) {}
179
+
180
+ // If aligning argument lines with the first argument doesn't fit, or is difficult to
181
+ // read, wrap all arguments on new lines with a 4 space indent.
182
+ void AnotherLongFunctionNameThatCausesLineWrappingProblems(
183
+ int longArgumentName, int longArgumentName2, int longArgumentName3) {}
184
+
185
+ void CallingLongFunctionName() {
186
+ int veryLongArgumentName = 1234;
187
+ int shortArg = 1;
188
+ // If possible, wrap arguments by aligning newlines with the first argument.
189
+ AnotherLongFunctionNameThatCausesLineWrappingProblems(shortArg, shortArg,
190
+ veryLongArgumentName);
191
+ // If aligning argument lines with the first argument doesn't fit, or is difficult to
192
+ // read, wrap all arguments on new lines with a 4 space indent.
193
+ AnotherLongFunctionNameThatCausesLineWrappingProblems(
194
+ veryLongArgumentName, veryLongArgumentName, veryLongArgumentName);
195
+ }
196
+ }
197
+ }
198
+ ```
199
+
200
+ ## C# coding guidelines
201
+
202
+ ### Constants
203
+
204
+ * Variables and fields that can be made `const` should always be made `const`.
205
+ * If `const` isn’t possible, `readonly` can be a suitable alternative.
206
+ * Prefer named constants to magic numbers.
207
+
208
+ ### IEnumerable vs IList vs IReadOnlyList
209
+
210
+ * For inputs use the most restrictive collection type possible, for example
211
+ `IReadOnlyCollection` / `IReadOnlyList` / `IEnumerable` as inputs to methods
212
+ when the inputs should be immutable.
213
+ * For outputs, if passing ownership of the returned container to the owner,
214
+ prefer `IList` over `IEnumerable`. If not transferring ownership, prefer the
215
+ most restrictive option.
216
+
217
+ ### Generators vs containers
218
+
219
+ * Use your best judgement, bearing in mind:
220
+ * Generator code is often less readable than filling in a container.
221
+ * Generator code can be more performant if the results are going to be
222
+ processed lazily, e.g. when not all the results are needed.
223
+ * Generator code that is directly turned into a container via `ToList()`
224
+ will be less performant than filling in a container directly.
225
+ * Generator code that is called multiple times will be considerably slower
226
+ than iterating over a container multiple times.
227
+
228
+ ### Property styles
229
+
230
+ * For single line read-only properties, prefer expression body properties
231
+ (`=>`) when possible.
232
+ * For everything else, use the older `{ get; set; }` syntax.
233
+
234
+ ### Expression body syntax
235
+
236
+ For example:
237
+
238
+ ```c#
239
+ int SomeProperty => _someProperty
240
+ ```
241
+
242
+ * Judiciously use expression body syntax in lambdas and properties.
243
+ * Don’t use on method definitions. This will be reviewed when C# 7 is live,
244
+ which uses this syntax heavily.
245
+ * As with methods and other scoped blocks of code, align the closing with the
246
+ first character of the line that includes the opening brace. See sample code
247
+ for examples.
248
+
249
+ ### Structs and classes:
250
+
251
+ * Structs are very different from classes:
252
+
253
+ * Structs are always passed and returned by value.
254
+ * Assigning a value to a member of a returned struct doesn’t modify the
255
+ original - e.g. `transform.position.x = 10` doesn’t set the transform’s
256
+ position.x to 10; `position` here is a property that returns a `Vector3`
257
+ by value, so this just sets the x parameter of a copy of the original.
258
+
259
+ * Almost always use a class.
260
+
261
+ * Consider struct when the type can be treated like other value types - for
262
+ example, if instances of the type are small and commonly short-lived or are
263
+ commonly embedded in other objects. Good examples include Vector3,
264
+ Quaternion and Bounds.
265
+
266
+ * Note that this guidance may vary from team to team where, for example,
267
+ performance issues might force the use of structs.
268
+
269
+ ### Lambdas vs named methods
270
+
271
+ * If a lambda is non-trivial (e.g. more than a couple of statements, excluding
272
+ declarations), or is reused in multiple places, it should probably be a
273
+ named method.
274
+
275
+ ### Field initializers
276
+
277
+ * Field initializers are generally encouraged.
278
+
279
+ ### Extension methods
280
+
281
+ * Only use an extension method when the source of the original class is not
282
+ available, or else when changing the source is not feasible.
283
+ * Only use an extension method if the functionality being added is a ‘core’
284
+ general feature that would be appropriate to add to the source of the
285
+ original class.
286
+ * Note - if we have the source to the class being extended, and the
287
+ maintainer of the original class does not want to add the function,
288
+ prefer not using an extension method.
289
+ * Only put extension methods into core libraries that are available
290
+ everywhere - extensions that are only available in some code will become a
291
+ readability issue.
292
+ * Be aware that using extension methods always obfuscates the code, so err on
293
+ the side of not adding them.
294
+
295
+ ### ref and out
296
+
297
+ * Use `out` for returns that are not also inputs.
298
+ * Place `out` parameters after all other parameters in the method definition.
299
+ * `ref` should be used rarely, when mutating an input is necessary.
300
+ * Do not use `ref` as an optimisation for passing structs.
301
+ * Do not use `ref` to pass a modifiable container into a method. `ref` is only
302
+ required when the supplied container needs be replaced with an entirely
303
+ different container instance.
304
+
305
+ ### LINQ
306
+
307
+ * In general, prefer single line LINQ calls and imperative code, rather than
308
+ long chains of LINQ. Mixing imperative code and heavily chained LINQ is
309
+ often hard to read.
310
+ * Prefer member extension methods over SQL-style LINQ keywords - e.g. prefer
311
+ `myList.Where(x)` to `myList where x`.
312
+ * Avoid `Container.ForEach(...)` for anything longer than a single statement.
313
+
314
+ ### Array vs List
315
+
316
+ * In general, prefer `List<>` over arrays for public variables, properties,
317
+ and return types (keeping in mind the guidance on `IList` / `IEnumerable` /
318
+ `IReadOnlyList` above).
319
+ * Prefer `List<>` when the size of the container can change.
320
+ * Prefer arrays when the size of the container is fixed and known at
321
+ construction time.
322
+ * Prefer array for multidimensional arrays.
323
+ * Note:
324
+ * array and `List<>` both represent linear, contiguous containers.
325
+ * Similar to C++ arrays vs `std::vector`, arrays are of fixed capacity,
326
+ whereas `List<>` can be added to.
327
+ * In some cases arrays are more performant, but in general `List<>` is
328
+ more flexible.
329
+
330
+ ### Folders and file locations
331
+
332
+ * Be consistent with the project.
333
+ * Prefer a flat structure where possible.
334
+
335
+ ### Use of tuple as a return type
336
+
337
+ * In general, prefer a named class type over `Tuple<>`, particularly when
338
+ returning complex types.
339
+
340
+ ### String interpolation vs `String.Format()` vs `String.Concat` vs `operator+`
341
+
342
+ * In general, use whatever is easiest to read, particularly for logging and
343
+ assert messages.
344
+ * Be aware that chained `operator+` concatenations will be slower and cause
345
+ significant memory churn.
346
+ * If performance is a concern, `StringBuilder` will be faster for multiple
347
+ string concatenations.
348
+
349
+ ### `using`
350
+
351
+ * Generally, don’t alias long typenames with `using`. Often this is a sign
352
+ that a `Tuple<>` needs to be turned into a class.
353
+ * e.g. `using RecordList = List<Tuple<int, float>>` should probably be a
354
+ named class instead.
355
+ * Be aware that `using` statements are only file scoped and so of limited use.
356
+ Type aliases will not be available for external users.
357
+
358
+ ### Object Initializer syntax
359
+
360
+ For example:
361
+
362
+ ```c#
363
+ var x = new SomeClass {
364
+ Property1 = value1,
365
+ Property2 = value2,
366
+ };
367
+ ```
368
+
369
+ * Object Initializer Syntax is fine for ‘plain old data’ types.
370
+ * Avoid using this syntax for classes or structs with constructors.
371
+ * If splitting across multiple lines, indent one block level.
372
+
373
+ ### Namespace naming
374
+
375
+ * In general, namespaces should be no more than 2 levels deep.
376
+ * Don't force file/folder layout to match namespaces.
377
+ * For shared library/module code, use namespaces. For leaf 'application' code,
378
+ such as `unity_app`, namespaces are not necessary.
379
+ * New top-level namespace names must be globally unique and recognizable.
380
+
381
+ ### Default values/null returns for structs
382
+
383
+ * Prefer returning a ‘success’ boolean value and a struct `out` value.
384
+ * Where performance isn't a concern and the resulting code significantly more
385
+ readable (e.g. chained null conditional operators vs deeply nested if
386
+ statements) nullable structs are acceptable.
387
+ * Notes:
388
+
389
+ * Nullable structs are convenient, but reinforce the general ‘null is
390
+ failure’ pattern Google prefers to avoid. We will investigate a
391
+ `StatusOr` equivalent in the future, if there is enough demand.
392
+
393
+ ### Removing from containers while iterating
394
+
395
+ C# (like many other languages) does not provide an obvious mechanism for
396
+ removing items from containers while iterating. There are a couple of options:
397
+
398
+ * If all that is required is to remove items that satisfy some condition,
399
+ `someList.RemoveAll(somePredicate)` is recommended.
400
+ * If other work needs to be done in the iteration, `RemoveAll` may not be
401
+ sufficient. A common alternative pattern is to create a new container
402
+ outside of the loop, insert items to keep in the new container, and swap the
403
+ original container with the new one at the end of iteration.
404
+
405
+ ### Calling delegates
406
+
407
+ * When calling a delegate, use `Invoke()` and use the null conditional
408
+ operator - e.g. `SomeDelegate?.Invoke()`. This clearly marks the call at the
409
+ callsite as ‘a delegate that is being called’. The null check is concise and
410
+ robust against threading race conditions.
411
+
412
+ ### The `var` keyword
413
+
414
+ * Use of `var` is encouraged if it aids readability by avoiding type names
415
+ that are noisy, obvious, or unimportant.
416
+ * Encouraged:
417
+
418
+ * When the type is obvious - e.g. `var apple = new Apple();`, or `var
419
+ request = Factory.Create<HttpRequest>();`
420
+ * For transient variables that are only passed directly to other methods -
421
+ e.g. `var item = GetItem(); ProcessItem(item);`
422
+
423
+ * Discouraged:
424
+
425
+ * When working with basic types - e.g. `var success = true;`
426
+ * When working with compiler-resolved built-in numeric types - e.g. `var
427
+ number = 12 * ReturnsFloat();`
428
+ * When users would clearly benefit from knowing the type - e.g. `var
429
+ listOfItems = GetList();`
430
+
431
+ ### Attributes
432
+
433
+ * Attributes should appear on the line above the field, property, or method
434
+ they are associated with, separated from the member by a newline.
435
+ * Multiple attributes should be separated by newlines. This allows for easier
436
+ adding and removing of attributes, and ensures each attribute is easy to
437
+ search for.
438
+
439
+ ### Argument Naming
440
+
441
+ Derived from the Google C++ style guide.
442
+
443
+ When the meaning of a function argument is nonobvious, consider one of the
444
+ following remedies:
445
+
446
+ * If the argument is a literal constant, and the same constant is used in
447
+ multiple function calls in a way that tacitly assumes they're the same, use
448
+ a named constant to make that constraint explicit, and to guarantee that it
449
+ holds.
450
+ * Consider changing the function signature to replace a `bool` argument with
451
+ an `enum` argument. This will make the argument values self-describing.
452
+ * Replace large or complex nested expressions with named variables.
453
+ * Consider using
454
+ [Named Arguments](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments)
455
+ to clarify argument meanings at the call site.
456
+ * For functions that have several configuration options, consider defining a
457
+ single class or struct to hold all the options and pass an instance of that.
458
+ This approach has several advantages. Options are referenced by name at the
459
+ call site, which clarifies their meaning. It also reduces function argument
460
+ count, which makes function calls easier to read and write. As an added
461
+ benefit, call sites don't need to be changed when another option is added.
462
+
463
+ Consider the following example:
464
+
465
+ ```c#
466
+ // Bad - what are these arguments?
467
+ DecimalNumber product = CalculateProduct(values, 7, false, null);
468
+ ```
469
+
470
+ versus:
471
+
472
+ ```c#
473
+ // Good
474
+ ProductOptions options = new ProductOptions();
475
+ options.PrecisionDecimals = 7;
476
+ options.UseCache = CacheUsage.DontUseCache;
477
+ DecimalNumber product = CalculateProduct(values, options, completionDelegate: null);
478
+ ```