forked from FuelLabs/sway
-
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.
Sway Ref: Style Guide single use variables (FuelLabs#4664)
- Loading branch information
Showing
7 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
2 changes: 2 additions & 0 deletions
2
docs/reference/src/code/language/style-guide/intermediate_variables/.gitignore
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,2 @@ | ||
out | ||
target |
9 changes: 9 additions & 0 deletions
9
docs/reference/src/code/language/style-guide/intermediate_variables/Forc.toml
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,9 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "intermediate_variables" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } | ||
std = { path = "../../../../../../../sway-lib-std" } |
29 changes: 29 additions & 0 deletions
29
docs/reference/src/code/language/style-guide/intermediate_variables/src/lib.sw
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,29 @@ | ||
library; | ||
|
||
#[allow(dead_code)] | ||
fn update_state() -> u64 { | ||
// Used for context in the following function | ||
42 | ||
} | ||
|
||
#[allow(dead_code)] | ||
// ANCHOR: contextual_assignment | ||
fn contextual_assignment() { | ||
let remaining_amount = update_state(); | ||
// code that uses `remaining_amount` instead of directly calling `update_state()` | ||
} | ||
// ANCHOR_END: contextual_assignment | ||
|
||
#[allow(dead_code)] | ||
fn update_state_of_vault_v3_storage_contract() -> u64 { | ||
// Used for context in the following function | ||
42 | ||
} | ||
|
||
#[allow(dead_code)] | ||
// ANCHOR: shortened_name | ||
fn shortened_name() { | ||
let remaining_amount = update_state_of_vault_v3_storage_contract(); | ||
// code that uses `remaining_amount` instead of directly calling `update_state_of_vault_v3_storage_contract()` | ||
} | ||
// ANCHOR_END: shortened_name |
19 changes: 19 additions & 0 deletions
19
docs/reference/src/documentation/language/style-guide/intermediate-variables.md
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,19 @@ | ||
# Intermediate Variables | ||
|
||
An intermediate variable, or a temporary variable, is a variable that is typically used once. In most cases we avoid creating intermediate variables; however, there are cases where they may enrich the code. | ||
|
||
## Contextual Assignment | ||
|
||
It may be beneficial to use an intermediate variable to provide context to the reader about the value. | ||
|
||
```sway | ||
{{#include ../../../code/language/style-guide/intermediate_variables/src/lib.sw:contextual_assignment}} | ||
``` | ||
|
||
## Shortened Name | ||
|
||
In the cases of multiple levels of indentation or overly verbose names it may be beneficial to create an intermediate variable with a shorter name. | ||
|
||
```sway | ||
{{#include ../../../code/language/style-guide/intermediate_variables/src/lib.sw:shortened_name}} | ||
``` |