generated from nvs2394/gatsby-starter-minimal-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
title: How to name your code follow Uncle Bob way | ||
date: 2022-03-13 | ||
tags: | ||
- CleanCode | ||
- VariableNaming | ||
- FunctionNaming | ||
banner: ./banner.png | ||
--- | ||
|
||
banner: ./banner.png | ||
|
||
⛔ Bad code | ||
|
||
```go | ||
function card() { | ||
return card | ||
} | ||
``` | ||
|
||
🆗 Good code | ||
|
||
```go | ||
function getCard() { | ||
return card | ||
} | ||
``` | ||
|
||
Here the name of the function is self-explanatory, and the variable names give us clues that the cities denote the start and end of something. | ||
|
||
> Rule 1: Use nouns or short phrases with adjectives to name variables | ||
⛔ | ||
```go | ||
let u = {} | ||
``` | ||
|
||
🆗 | ||
|
||
```go | ||
let user = {} | ||
``` | ||
Aim to avoid redundancy in names as much as possible. | ||
|
||
> Rule 2: The names of the booleans must resolve to a yes or no answer | ||
⛔ | ||
```go | ||
if(payment) { | ||
// do something | ||
} | ||
``` | ||
🆗 | ||
```go | ||
if(isValidPayment){ | ||
// do something | ||
} | ||
``` | ||
Booleans have an on/off nature. As such, their names must reflect their character. | ||
|
||
> Rule 3: Use verbs or short phrases with adjectives to name functions or methods | ||
🆗 | ||
```go | ||
function getPaymentMethod() { | ||
// do something() | ||
} | ||
|
||
function fetchWalletBalance(wallet Wallet) { | ||
return wallet.balance | ||
} | ||
``` | ||
*Avoid inconsistencies when using verbs for naming functions and stick to using the same verbs for the same actions.* | ||
|
||
> Rule 4: Use nouns or short phrases with nouns to name classes | ||
⛔ | ||
```javascript | ||
class AppUser { | ||
// some methods/vars | ||
} | ||
``` | ||
🆗 | ||
```javascript | ||
class User { | ||
// some methods/vars | ||
} | ||
``` | ||
*To avoid any confusion, make sure to use nouns and think before adding an extra word to the name.* | ||
|
||
*Does it add any value, or does it make the meaning fuzzier?* | ||
|