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.
- configs/pyrefly.toml +11 -0
- configs/ruff.toml +20 -0
- guides/Rguide.md +109 -0
- guides/cppguide.md +5713 -0
- guides/csharp-style.md +478 -0
- guides/docguide-style.md +845 -0
- guides/go-guide.md +483 -0
- guides/htmlcssguide.md +1022 -0
- guides/javaguide.md +1189 -0
- guides/jsguide.md +3775 -0
- guides/jsoncstyleguide.md +1376 -0
- guides/objcguide.md +2386 -0
- guides/pyguide.md +3709 -0
- guides/shellguide.md +1343 -0
- guides/tsguide.md +3662 -0
- guides/vimscriptguide.md +356 -0
- readability.py +755 -0
- readability_cli-0.4.0.dist-info/METADATA +192 -0
- readability_cli-0.4.0.dist-info/RECORD +22 -0
- readability_cli-0.4.0.dist-info/WHEEL +4 -0
- readability_cli-0.4.0.dist-info/entry_points.txt +2 -0
- readability_cli-0.4.0.dist-info/licenses/LICENSE +21 -0
guides/go-guide.md
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
<!--* toc_depth: 3 *-->
|
|
2
|
+
|
|
3
|
+
# Go Style Guide
|
|
4
|
+
|
|
5
|
+
https://google.github.io/styleguide/go/guide
|
|
6
|
+
|
|
7
|
+
[Overview](index) | [Guide](guide) | [Decisions](decisions) |
|
|
8
|
+
[Best practices](best-practices)
|
|
9
|
+
|
|
10
|
+
<!--
|
|
11
|
+
|
|
12
|
+
-->
|
|
13
|
+
|
|
14
|
+
{% raw %}
|
|
15
|
+
|
|
16
|
+
**Note:** This is part of a series of documents that outline [Go Style](index)
|
|
17
|
+
at Google. This document is **[normative](index#normative) and
|
|
18
|
+
[canonical](index#canonical)**. See [the overview](index#about) for more
|
|
19
|
+
information.
|
|
20
|
+
|
|
21
|
+
<a id="principles"></a>
|
|
22
|
+
|
|
23
|
+
## Style principles
|
|
24
|
+
|
|
25
|
+
There are a few overarching principles that summarize how to think about writing
|
|
26
|
+
readable Go code. The following are attributes of readable code, in order of
|
|
27
|
+
importance:
|
|
28
|
+
|
|
29
|
+
1. **[Clarity]**: The code's purpose and rationale is clear to the reader.
|
|
30
|
+
1. **[Simplicity]**: The code accomplishes its goal in the simplest way
|
|
31
|
+
possible.
|
|
32
|
+
1. **[Concision]**: The code has a high signal-to-noise ratio.
|
|
33
|
+
1. **[Maintainability]**: The code is written such that it can be easily
|
|
34
|
+
maintained.
|
|
35
|
+
1. **[Consistency]**: The code is consistent with the broader Google codebase.
|
|
36
|
+
|
|
37
|
+
[Clarity]: #clarity
|
|
38
|
+
[Simplicity]: #simplicity
|
|
39
|
+
[Concision]: #concision
|
|
40
|
+
[Maintainability]: #maintainability
|
|
41
|
+
[Consistency]: #consistency
|
|
42
|
+
|
|
43
|
+
<a id="clarity"></a>
|
|
44
|
+
|
|
45
|
+
### Clarity
|
|
46
|
+
|
|
47
|
+
The core goal of readability is to produce code that is clear to the reader.
|
|
48
|
+
|
|
49
|
+
Clarity is primarily achieved with effective naming, helpful commentary, and
|
|
50
|
+
efficient code organization.
|
|
51
|
+
|
|
52
|
+
Clarity is to be viewed through the lens of the reader, not the author of the
|
|
53
|
+
code. It is more important that code be easy to read than easy to write. Clarity
|
|
54
|
+
in code has two distinct facets:
|
|
55
|
+
|
|
56
|
+
* [What is the code actually doing?](#clarity-purpose)
|
|
57
|
+
* [Why is the code doing what it does?](#clarity-rationale)
|
|
58
|
+
|
|
59
|
+
<a id="clarity-purpose"></a>
|
|
60
|
+
|
|
61
|
+
#### What is the code actually doing?
|
|
62
|
+
|
|
63
|
+
Go is designed such that it should be relatively straightforward to see what the
|
|
64
|
+
code is doing. In cases of uncertainty or where a reader may require prior
|
|
65
|
+
knowledge in order to understand the code, it is worth investing time in order
|
|
66
|
+
to make the code's purpose clearer for future readers. For example, it may help
|
|
67
|
+
to:
|
|
68
|
+
|
|
69
|
+
* Use more descriptive variable names
|
|
70
|
+
* Add additional commentary
|
|
71
|
+
* Break up the code with whitespace and comments
|
|
72
|
+
* Refactor the code into separate functions/methods to make it more modular
|
|
73
|
+
|
|
74
|
+
There is no one-size-fits-all approach here, but it is important to prioritize
|
|
75
|
+
clarity when developing Go code.
|
|
76
|
+
|
|
77
|
+
<a id="clarity-rationale"></a>
|
|
78
|
+
|
|
79
|
+
#### Why is the code doing what it does?
|
|
80
|
+
|
|
81
|
+
The code's rationale is often sufficiently communicated by the names of
|
|
82
|
+
variables, functions, methods, or packages. Where it is not, it is important to
|
|
83
|
+
add commentary. The "Why?" is especially important when the code contains
|
|
84
|
+
nuances that a reader may not be familiar with, such as:
|
|
85
|
+
|
|
86
|
+
* A nuance in the language, e.g., a closure will be capturing a loop variable,
|
|
87
|
+
but the closure is many lines away
|
|
88
|
+
* A nuance of the business logic, e.g., an access control check that needs to
|
|
89
|
+
distinguish between the actual user and someone impersonating a user
|
|
90
|
+
|
|
91
|
+
An API might require care to use correctly. For example, a piece of code may be
|
|
92
|
+
intricate and difficult to follow for performance reasons, or a complex sequence
|
|
93
|
+
of mathematical operations may use type conversions in an unexpected way. In
|
|
94
|
+
these cases and many more, it is important that accompanying commentary and
|
|
95
|
+
documentation explain these aspects so that future maintainers don't make a
|
|
96
|
+
mistake and so that readers can understand the code without needing to
|
|
97
|
+
reverse-engineer it.
|
|
98
|
+
|
|
99
|
+
It is also important to be aware that some attempts to provide clarity (such as
|
|
100
|
+
adding extra commentary) can actually obscure the code's purpose by adding
|
|
101
|
+
clutter, restating what the code already says, contradicting the code, or adding
|
|
102
|
+
maintenance burden to keep the comments up-to-date. Allow the code to speak for
|
|
103
|
+
itself (e.g., by making the symbol names themselves self-describing) rather than
|
|
104
|
+
adding redundant comments. It is often better for comments to explain why
|
|
105
|
+
something is done, not what the code is doing.
|
|
106
|
+
|
|
107
|
+
The Google codebase is largely uniform and consistent. It is often the case that
|
|
108
|
+
code that stands out (e.g., by using an unfamiliar pattern) is doing so for a
|
|
109
|
+
good reason, typically for performance. Maintaining this property is important
|
|
110
|
+
to make it clear to readers where they should focus their attention when reading
|
|
111
|
+
a new piece of code.
|
|
112
|
+
|
|
113
|
+
The standard library contains many examples of this principle in action. Among
|
|
114
|
+
them:
|
|
115
|
+
|
|
116
|
+
* Maintainer comments in
|
|
117
|
+
[`package sort`](https://cs.opensource.google/go/go/+/refs/tags/go1.19.2:src/sort/sort.go).
|
|
118
|
+
* Good
|
|
119
|
+
[runnable examples in the same package](https://cs.opensource.google/go/go/+/refs/tags/go1.19.2:src/sort/example_search_test.go),
|
|
120
|
+
which benefit both users (they
|
|
121
|
+
[show up in godoc](https://pkg.go.dev/sort#pkg-examples)) and maintainers
|
|
122
|
+
(they [run as part of tests](decisions#examples)).
|
|
123
|
+
* [`strings.Cut`](https://pkg.go.dev/strings#Cut) is only four lines of code,
|
|
124
|
+
but they improve the
|
|
125
|
+
[clarity and correctness of callsites](https://github.com/golang/go/issues/46336).
|
|
126
|
+
|
|
127
|
+
<a id="simplicity"></a>
|
|
128
|
+
|
|
129
|
+
### Simplicity
|
|
130
|
+
|
|
131
|
+
Your Go code should be simple for those using, reading, and maintaining it.
|
|
132
|
+
|
|
133
|
+
Go code should be written in the simplest way that accomplishes its goals, both
|
|
134
|
+
in terms of behavior and performance. Within the Google Go codebase, simple
|
|
135
|
+
code:
|
|
136
|
+
|
|
137
|
+
* Is easy to read from top to bottom
|
|
138
|
+
* Does not assume that you already know what it is doing
|
|
139
|
+
* Does not assume that you can memorize all of the preceding code
|
|
140
|
+
* Does not have unnecessary levels of abstraction
|
|
141
|
+
* Does not have names that call attention to something mundane
|
|
142
|
+
* Makes the propagation of values and decisions clear to the reader
|
|
143
|
+
* Has comments that explain why, not what, the code is doing to avoid future
|
|
144
|
+
deviation
|
|
145
|
+
* Has documentation that stands on its own
|
|
146
|
+
* Has useful errors and useful test failures
|
|
147
|
+
* May often be mutually exclusive with "clever" code
|
|
148
|
+
|
|
149
|
+
Tradeoffs can arise between code simplicity and API usage simplicity. For
|
|
150
|
+
example, it may be worthwhile to have the code be more complex so that the end
|
|
151
|
+
user of the API may more easily call the API correctly. In contrast, it may also
|
|
152
|
+
be worthwhile to leave a bit of extra work to the end user of the API so that
|
|
153
|
+
the code remains simple and easy to understand.
|
|
154
|
+
|
|
155
|
+
When code needs complexity, the complexity should be added deliberately. This is
|
|
156
|
+
typically necessary if additional performance is required or where there are
|
|
157
|
+
multiple disparate customers of a particular library or service. Complexity may
|
|
158
|
+
be justified, but it should come with accompanying documentation so that clients
|
|
159
|
+
and future maintainers are able to understand and navigate the complexity. This
|
|
160
|
+
should be supplemented with tests and examples that demonstrate its correct
|
|
161
|
+
usage, especially if there is both a "simple" and a "complex" way to use the
|
|
162
|
+
code.
|
|
163
|
+
|
|
164
|
+
This principle does not imply that complex code cannot or should not be written
|
|
165
|
+
in Go or that Go code is not allowed to be complex. We strive for a codebase
|
|
166
|
+
that avoids unnecessary complexity so that when complexity does appear, it
|
|
167
|
+
indicates that the code in question requires care to understand and maintain.
|
|
168
|
+
Ideally, there should be accompanying commentary that explains the rationale and
|
|
169
|
+
identifies the care that should be taken. This often arises when optimizing code
|
|
170
|
+
for performance; doing so often requires a more complex approach, like
|
|
171
|
+
preallocating a buffer and reusing it throughout a goroutine lifetime. When a
|
|
172
|
+
maintainer sees this, it should be a clue that the code in question is
|
|
173
|
+
performance-critical, and that should influence the care that is taken when
|
|
174
|
+
making future changes. If employed unnecessarily, on the other hand, this
|
|
175
|
+
complexity is a burden on those who need to read or change the code in the
|
|
176
|
+
future.
|
|
177
|
+
|
|
178
|
+
If code turns out to be very complex when its purpose should be simple, this is
|
|
179
|
+
often a signal to revisit the implementation to see if there is a simpler way to
|
|
180
|
+
accomplish the same thing.
|
|
181
|
+
|
|
182
|
+
<a id="least-mechanism"></a>
|
|
183
|
+
|
|
184
|
+
#### Least mechanism
|
|
185
|
+
|
|
186
|
+
Where there are several ways to express the same idea, prefer the one that uses
|
|
187
|
+
the most standard tools. Sophisticated machinery often exists, but should not be
|
|
188
|
+
employed without reason. It is easy to add complexity to code as needed, whereas
|
|
189
|
+
it is much harder to remove existing complexity after it has been found to be
|
|
190
|
+
unnecessary.
|
|
191
|
+
|
|
192
|
+
1. Aim to use a core language construct (for example a channel, slice, map,
|
|
193
|
+
loop, or struct) when sufficient for your use case.
|
|
194
|
+
2. If there isn't one, look for a tool within the standard library (like an
|
|
195
|
+
HTTP client or a template engine).
|
|
196
|
+
3. Finally, consider whether there is a core library in the Google codebase
|
|
197
|
+
that is sufficient before introducing a new dependency or creating your own.
|
|
198
|
+
|
|
199
|
+
As an example, consider production code that contains a flag bound to a variable
|
|
200
|
+
with a default value which must be overridden in tests. Unless intending to test
|
|
201
|
+
the program's command-line interface itself (say, with `os/exec`), it is simpler
|
|
202
|
+
and therefore preferable to override the bound value directly rather than by
|
|
203
|
+
using `flag.Set`.
|
|
204
|
+
|
|
205
|
+
Similarly, if a piece of code requires a set membership check, a boolean-valued
|
|
206
|
+
map (e.g., `map[string]bool`) often suffices. Libraries that provide set-like
|
|
207
|
+
types and functionality should only be used if more complicated operations are
|
|
208
|
+
required that are impossible or overly complicated with a map.
|
|
209
|
+
|
|
210
|
+
<a id="concision"></a>
|
|
211
|
+
|
|
212
|
+
### Concision
|
|
213
|
+
|
|
214
|
+
Concise Go code has a high signal-to-noise ratio. It is easy to discern the
|
|
215
|
+
relevant details, and the naming and structure guide the reader through these
|
|
216
|
+
details.
|
|
217
|
+
|
|
218
|
+
There are many things that can get in the way of surfacing the most salient
|
|
219
|
+
details at any given time:
|
|
220
|
+
|
|
221
|
+
* Repetitive code
|
|
222
|
+
* Extraneous syntax
|
|
223
|
+
* [Opaque names](#naming)
|
|
224
|
+
* Unnecessary abstraction
|
|
225
|
+
* Whitespace
|
|
226
|
+
|
|
227
|
+
Repetitive code especially obscures the differences between each
|
|
228
|
+
nearly-identical section, and requires a reader to visually compare similar
|
|
229
|
+
lines of code to find the changes. [Table-driven testing] is a good example of a
|
|
230
|
+
mechanism that can concisely factor out the common code from the important
|
|
231
|
+
details of each repetition, but the choice of which pieces to include in the
|
|
232
|
+
table will have an impact on how easy the table is to understand.
|
|
233
|
+
|
|
234
|
+
When considering multiple ways to structure code, it is worth considering which
|
|
235
|
+
way makes important details the most apparent.
|
|
236
|
+
|
|
237
|
+
Understanding and using common code constructions and idioms are also important
|
|
238
|
+
for maintaining a high signal-to-noise ratio. For example, the following code
|
|
239
|
+
block is very common in [error handling], and the reader can quickly understand
|
|
240
|
+
the purpose of this block.
|
|
241
|
+
|
|
242
|
+
```go
|
|
243
|
+
// Good:
|
|
244
|
+
if err := doSomething(); err != nil {
|
|
245
|
+
// ...
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
If code looks very similar to this but is subtly different, a reader may not
|
|
250
|
+
notice the change. In cases like this, it is worth intentionally ["boosting"]
|
|
251
|
+
the signal of the error check by adding a comment to call attention to it.
|
|
252
|
+
|
|
253
|
+
```go
|
|
254
|
+
// Good:
|
|
255
|
+
if err := doSomething(); err == nil { // if NO error
|
|
256
|
+
// ...
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
[Table-driven testing]: https://go.dev/wiki/TableDrivenTests
|
|
261
|
+
[error handling]: https://go.dev/blog/errors-are-values
|
|
262
|
+
["boosting"]: best-practices#signal-boost
|
|
263
|
+
|
|
264
|
+
<a id="maintainability"></a>
|
|
265
|
+
|
|
266
|
+
### Maintainability
|
|
267
|
+
|
|
268
|
+
Code is edited many more times than it is written. Readable code not only makes
|
|
269
|
+
sense to a reader who is trying to understand how it works, but also to the
|
|
270
|
+
programmer who needs to change it. Clarity is key.
|
|
271
|
+
|
|
272
|
+
Maintainable code:
|
|
273
|
+
|
|
274
|
+
* Is easy for a future programmer to modify correctly
|
|
275
|
+
* Has APIs that are structured so that they can grow gracefully
|
|
276
|
+
* Is clear about the assumptions that it makes and chooses abstractions that
|
|
277
|
+
map to the structure of the problem, not to the structure of the code
|
|
278
|
+
* Avoids unnecessary coupling and doesn't include features that are not used
|
|
279
|
+
* Has a comprehensive test suite to ensure promised behaviors are maintained
|
|
280
|
+
and important logic is correct, and the tests provide clear, actionable
|
|
281
|
+
diagnostics in case of failure
|
|
282
|
+
|
|
283
|
+
When using abstractions like interfaces and types which by definition remove
|
|
284
|
+
information from the context in which they are used, it is important to ensure
|
|
285
|
+
that they provide sufficient benefit. Editors and IDEs can connect directly to a
|
|
286
|
+
method definition and show the corresponding documentation when a concrete type
|
|
287
|
+
is used, but can only refer to an interface definition otherwise. Interfaces are
|
|
288
|
+
a powerful tool, but come with a cost, since the maintainer may need to
|
|
289
|
+
understand the specifics of the underlying implementation in order to correctly
|
|
290
|
+
use the interface, which must be explained within the interface documentation or
|
|
291
|
+
at the call-site.
|
|
292
|
+
|
|
293
|
+
Maintainable code also avoids hiding important details in places that are easy
|
|
294
|
+
to overlook. For example, in each of the following lines of code, the presence
|
|
295
|
+
or lack of a single character is critical to understand the line:
|
|
296
|
+
|
|
297
|
+
```go
|
|
298
|
+
// Bad:
|
|
299
|
+
// The use of = instead of := can change this line completely.
|
|
300
|
+
if user, err = db.UserByID(userID); err != nil {
|
|
301
|
+
// ...
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
```go
|
|
306
|
+
// Bad:
|
|
307
|
+
// The ! in the middle of this line is very easy to miss.
|
|
308
|
+
leap := (year%4 == 0) && (!(year%100 == 0) || (year%400 == 0))
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Neither of these are incorrect, but both could be written in a more explicit
|
|
312
|
+
fashion, or could have an accompanying comment that calls attention to the
|
|
313
|
+
important behavior:
|
|
314
|
+
|
|
315
|
+
```go
|
|
316
|
+
// Good:
|
|
317
|
+
u, err := db.UserByID(userID)
|
|
318
|
+
if err != nil {
|
|
319
|
+
return fmt.Errorf("invalid origin user: %s", err)
|
|
320
|
+
}
|
|
321
|
+
user = u
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
```go
|
|
325
|
+
// Good:
|
|
326
|
+
// Gregorian leap years aren't just year%4 == 0.
|
|
327
|
+
// See https://en.wikipedia.org/wiki/Leap_year#Algorithm.
|
|
328
|
+
var (
|
|
329
|
+
leap4 = year%4 == 0
|
|
330
|
+
leap100 = year%100 == 0
|
|
331
|
+
leap400 = year%400 == 0
|
|
332
|
+
)
|
|
333
|
+
leap := leap4 && (!leap100 || leap400)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
In the same way, a helper function that hides critical logic or an important
|
|
337
|
+
edge-case could make it easy for a future change to fail to account for it
|
|
338
|
+
properly.
|
|
339
|
+
|
|
340
|
+
Predictable names are another feature of maintainable code. A user of a package
|
|
341
|
+
or a maintainer of a piece of code should be able to predict the name of a
|
|
342
|
+
variable, method, or function in a given context. Function parameters and
|
|
343
|
+
receiver names for identical concepts should typically share the same name, both
|
|
344
|
+
to keep documentation understandable and to facilitate refactoring code with
|
|
345
|
+
minimal overhead.
|
|
346
|
+
|
|
347
|
+
Maintainable code minimizes its dependencies (both implicit and explicit).
|
|
348
|
+
Depending on fewer packages means fewer lines of code that can affect behavior.
|
|
349
|
+
Avoiding dependencies on internal or undocumented behavior makes code less
|
|
350
|
+
likely to impose a maintenance burden when those behaviors change in the future.
|
|
351
|
+
|
|
352
|
+
When considering how to structure or write code, it is worth taking the time to
|
|
353
|
+
think through ways in which the code may evolve over time. If a given approach
|
|
354
|
+
is more conducive to easier and safer future changes, that is often a good
|
|
355
|
+
trade-off, even if it means a slightly more complicated design.
|
|
356
|
+
|
|
357
|
+
<a id="consistency"></a>
|
|
358
|
+
|
|
359
|
+
### Consistency
|
|
360
|
+
|
|
361
|
+
Consistent code is code that looks, feels, and behaves like similar code
|
|
362
|
+
throughout the broader codebase, within the context of a team or package, and
|
|
363
|
+
even within a single file.
|
|
364
|
+
|
|
365
|
+
Consistency concerns do not override any of the principles above, but if a tie
|
|
366
|
+
must be broken, it is often beneficial to break it in favor of consistency.
|
|
367
|
+
|
|
368
|
+
Consistency within a package is often the most immediately important level of
|
|
369
|
+
consistency. It can be very jarring if the same problem is approached in
|
|
370
|
+
multiple ways throughout a package, or if the same concept has many names within
|
|
371
|
+
a file. However, even this should not override documented style principles or
|
|
372
|
+
global consistency.
|
|
373
|
+
|
|
374
|
+
<a id="core"></a>
|
|
375
|
+
|
|
376
|
+
## Core guidelines
|
|
377
|
+
|
|
378
|
+
These guidelines collect the most important aspects of Go style that all Go code
|
|
379
|
+
is expected to follow. We expect that these principles be learned and followed
|
|
380
|
+
by the time readability is granted. These are not expected to change frequently,
|
|
381
|
+
and new additions will have to clear a high bar.
|
|
382
|
+
|
|
383
|
+
The guidelines below expand on the recommendations in [Effective Go], which
|
|
384
|
+
provide a common baseline for Go code across the entire community.
|
|
385
|
+
|
|
386
|
+
[Effective Go]: https://go.dev/doc/effective_go
|
|
387
|
+
|
|
388
|
+
<a id="formatting"></a>
|
|
389
|
+
|
|
390
|
+
### Formatting
|
|
391
|
+
|
|
392
|
+
All Go source files must conform to the format outputted by the `gofmt` tool.
|
|
393
|
+
This format is enforced by a presubmit check in the Google codebase.
|
|
394
|
+
[Generated code] should generally also be formatted (e.g., by using
|
|
395
|
+
[`format.Source`]), as it is also browsable in Code Search.
|
|
396
|
+
|
|
397
|
+
[Generated code]: https://docs.bazel.build/versions/main/be/general.html#genrule
|
|
398
|
+
[`format.Source`]: https://pkg.go.dev/go/format#Source
|
|
399
|
+
|
|
400
|
+
<a id="mixed-caps"></a>
|
|
401
|
+
|
|
402
|
+
### MixedCaps
|
|
403
|
+
|
|
404
|
+
Go source code uses `MixedCaps` or `mixedCaps` (camel case) rather than
|
|
405
|
+
underscores (snake case) when writing multi-word names.
|
|
406
|
+
|
|
407
|
+
This applies even when it breaks conventions in other languages. For example, a
|
|
408
|
+
constant is `MaxLength` (not `MAX_LENGTH`) if exported and `maxLength` (not
|
|
409
|
+
`max_length`) if unexported.
|
|
410
|
+
|
|
411
|
+
Local variables are considered [unexported] for the purpose of choosing the
|
|
412
|
+
initial capitalization.
|
|
413
|
+
|
|
414
|
+
<!--#include file="/go/g3doc/style/includes/special-name-exception.md"-->
|
|
415
|
+
|
|
416
|
+
[unexported]: https://go.dev/ref/spec#Exported_identifiers
|
|
417
|
+
|
|
418
|
+
<a id="line-length"></a>
|
|
419
|
+
|
|
420
|
+
### Line length
|
|
421
|
+
|
|
422
|
+
There is no fixed line length for Go source code. If a line feels too long,
|
|
423
|
+
prefer refactoring instead of splitting it. If it is already as short as it is
|
|
424
|
+
practical for it to be, the line should be allowed to remain long.
|
|
425
|
+
|
|
426
|
+
Do not split a line:
|
|
427
|
+
|
|
428
|
+
* Before an [indentation change](decisions#indentation-confusion) (e.g.,
|
|
429
|
+
function declaration, conditional)
|
|
430
|
+
* To make a long string (e.g., a URL) fit into multiple shorter lines
|
|
431
|
+
|
|
432
|
+
<a id="naming"></a>
|
|
433
|
+
|
|
434
|
+
### Naming
|
|
435
|
+
|
|
436
|
+
Naming is more art than science. In Go, names tend to be somewhat shorter than
|
|
437
|
+
in many other languages, but the same [general guidelines] apply. Names should:
|
|
438
|
+
|
|
439
|
+
* Not feel [repetitive](decisions#repetition) when they are used
|
|
440
|
+
* Take the context into consideration
|
|
441
|
+
* Not repeat concepts that are already clear
|
|
442
|
+
|
|
443
|
+
You can find more specific guidance on naming in [decisions](decisions#naming).
|
|
444
|
+
|
|
445
|
+
[general guidelines]: https://testing.googleblog.com/2017/10/code-health-identifiernamingpostforworl.html
|
|
446
|
+
|
|
447
|
+
<a id="local-consistency"></a>
|
|
448
|
+
|
|
449
|
+
### Local consistency
|
|
450
|
+
|
|
451
|
+
Where the style guide has nothing to say about a particular point of style,
|
|
452
|
+
authors are free to choose the style that they prefer, unless the code in close
|
|
453
|
+
proximity (usually within the same file or package, but sometimes within a team
|
|
454
|
+
or project directory) has taken a consistent stance on the issue.
|
|
455
|
+
|
|
456
|
+
Examples of **valid** local style considerations:
|
|
457
|
+
|
|
458
|
+
* Use of `%s` or `%v` for formatted printing of errors
|
|
459
|
+
* Usage of buffered channels in lieu of mutexes
|
|
460
|
+
|
|
461
|
+
Examples of **invalid** local style considerations:
|
|
462
|
+
|
|
463
|
+
* Line length restrictions for code
|
|
464
|
+
* Use of assertion-based testing libraries
|
|
465
|
+
|
|
466
|
+
If the local style disagrees with the style guide but the readability impact is
|
|
467
|
+
limited to one file, it will generally be surfaced in a code review for which a
|
|
468
|
+
consistent fix would be outside the scope of the CL in question. At that point,
|
|
469
|
+
it is appropriate to file a bug to track the fix.
|
|
470
|
+
|
|
471
|
+
If a change would worsen an existing style deviation, expose it in more API
|
|
472
|
+
surfaces, expand the number of files in which the deviation is present, or
|
|
473
|
+
introduce an actual bug, then local consistency is no longer a valid
|
|
474
|
+
justification for violating the style guide for new code. In these cases, it is
|
|
475
|
+
appropriate for the author to clean up the existing codebase in the same CL,
|
|
476
|
+
perform a refactor in advance of the current CL, or find an alternative that at
|
|
477
|
+
least does not make the local problem worse.
|
|
478
|
+
|
|
479
|
+
<!--
|
|
480
|
+
|
|
481
|
+
-->
|
|
482
|
+
|
|
483
|
+
{% endraw %}
|