You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -185,7 +187,7 @@ There are two code smells that should remind you of this rule:
185
187
186
188
***Copy & Paste:** Every time you take your mouse and mark lines with the intention to copy them, you are going to violate this rule. Instead of slightly adjusting copied lines, think about the common pattern between those lines and create a new function.
187
189
188
-
***Repeated `if-else` and `switch-case` statements:** If you are testing the same conditions at different locations (e.g. state variables), you can abstract these differences with Polymorphism and the Strategy Pattern.
190
+
***Repeated `if-else` and `switch-case` statements:** If you are testing the same conditions at different locations (e.g. state variables), you can abstract these differences with Polymorphism and the [Strategy Pattern](https://refactoring.guru/design-patterns/strategy).
@@ -285,33 +306,69 @@ Code should communicate behavior to other humans with lower complexity than the
285
306
286
307
287
308
309
+
### Pick one word for one concept.
310
+
311
+
Each concept should be described by a single word. Do not use `send()`, `write()`, and `transmit()` as synonyms for the same process, such as sending a message over a bus. Same holds true for `handleRequest()`, `processQuery()`, or `manageIncomingPacket()`.
312
+
313
+
Each word should only describe a single concept. Do not use `activate()` for setting a boolean variable and sending an activation packet to another system component. The first procedure is safe and instant, the second could fail, block execution, or run asynchronously. Better call it `sendActivationMsg()`.
314
+
315
+
288
316
289
-
### Subprograms (=Procedure/Function)
317
+
318
+
319
+
320
+
321
+
### Boolean Variables/Functions should have a `is/has/can` prefix.
322
+
Clean code reads like prose and these prefixes achieve clear and concise naming.
323
+
There is no difference between the naming of variables, e.g. `hasElements`, and functions, e.g. `hasElements()`, except that functions can also answer true/false questions regarding arguments, such as `isFile(path)` or `user.hasAccessTo(file)`.
Function examples: `hasElements()`, `isFile(path)`, `user.hasAccessTo(file)` -->
327
+
328
+
* consider the slightly uncommon prefixes `can` to express abilities/possibilities, e.g. `canWalk`.
329
+
* boolean names regarding [Collections](https://en.wikipedia.org/wiki/Collection_(abstract_data_type)) should use `Each/Any` before the prefix, e.g. `isEachLightOn` / `isAnyLightOn`
330
+
* prefer positive forms. Use `isActive` not `isInactive` to avoid confusing negations (`!isInactive`). Same for `hasElements` (not `isEmpty`) and `isEnabled` (not `isDisabled`).
331
+
* Avoid further uncommon prefixes, such as `does`, `are`, `was`, `will`, `should`.
Fun fact: The old [jQuery 1.7](https://code.jquery.com/jquery-1.7.2.js) had a boolean called `doesNotAddBorder`. Now they use `isBorderBox`.
344
+
345
+
346
+
347
+
### Naming Subprograms (=Procedure/Function)
290
348
291
349
Procedures *may* return values, functions always return a value. Methods are subprograms of a class.
292
350
* procedure names should start with a verb. e.g. `syncViews()`, `list.addItem(x)`.
293
-
* function names should describe the result and, if suitable, its type. e.g. `time_ms(), sin(x)`
351
+
* function names should describe the result and, if suitable, its type. e.g. `time_ms(), sin(x), isFile(path)`
294
352
* class methods should not repeat or include the name of the class. Define `Line.length()`, not `Line.getLineLength()`
295
353
296
354
> ⚠ **Caution:** *Single noun subprograms should be pure functions! Never let e.g. `x.length()` change a state.*
297
355
298
356
299
357
300
-
### Types (=Class/Struct/Subtypes)
358
+
### Naming Types (=Class/Struct/Subtypes)
301
359
302
360
* type names should be capitalized nouns. E.g. `Integer`, `Date`, `Line2D`
303
-
* enums/structs are types and named as types without a special prefix/suffix.<br>
361
+
* enums/structs are types and should be named as types without a special prefix/suffix.<br>
304
362
E.g. `enum Color = {RED, GREEN, BLUE}`
305
363
* interface names can start with a capital `I` and can also be adjectives. E.g. `IObservable`
306
364
307
365
308
366
309
-
### Variables
367
+
### Naming Variables
310
368
311
369
* variables with a large scope *should* have long names, variables with a small scope *may* have short names [[CdCm]](#user-content-references).
312
370
* collections (set, array, dict) should have a plural name. E.g. `cars`, `indices`
313
371
* the prefix `n` or `num` should be used for names representing the total number of objects in a collection. E.g. `numCars`
314
-
* boolean variables should start with a `is/has/can/does` prefix (e.g. `isEmpty`, `doesUseIO`).
315
372
* write constant names in capitals. E.g `CFG_TEMPERATURE_MAX = 80.0`
316
373
* prefix global variables with `g_`
317
374
@@ -371,7 +428,11 @@ Noun and adjective pairs with same/similar length:
371
428
</details>
372
429
373
430
374
-
> ⚠ **Avoid inappropriate terms:** Many organizations discourage the use of `master/slave` due to their negative association in different cultures. See [1](https://www.drupal.org/node/2275877) and [2](https://bugs.python.org/issue34605).
431
+
432
+
433
+
434
+
> ⚠ **Avoid inappropriate terms:** Many organizations discourage the use of `master/slave` due to their negative association in different cultures. See [1](https://www.drupal.org/node/2275877) and [2](https://bugs.python.org/issue34605).<br>
435
+
> ⚠ **Avoid useless noise-terms:** E.g. `data`, `info`, `process`. What would be the difference between `Product`, `ProductData`, and `ProductInfo`?
375
436
376
437
377
438
@@ -474,8 +535,8 @@ There are two different interest groups for your code, so please make sure that
474
535
***Developers:** How to compile. Module structure, dependencies, contribution rules, where to contact developers.
475
536
476
537
477
-
### Use a Logging Library
478
-
Provide meaningful logging messages at correct log levels:
Each code file with interfaces (e.g. `.h` files in C) should start with a block comment that briefly explains what this module/class/lib does. However, do not provide author name or changelogs as this info belongs into the [Version Control System](https://www.geeksforgeeks.org/version-control-systems/).
0 commit comments