Skip to content

Commit

Permalink
Sway Ref: Style Guide single use variables (FuelLabs#4664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen authored Jul 13, 2023
1 parent e184eb9 commit c54b5d0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/reference/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
- [Pattern Matching](./documentation/language/style-guide/pattern-matching.md)
- [Comments](./documentation/language/style-guide/comments.md)
- [Getter Functions](./documentation/language/style-guide/getters.md)
- [Intermediate Variables](./documentation/language/style-guide/intermediate-variables.md)

## Common Operations

Expand Down
8 changes: 8 additions & 0 deletions docs/reference/src/code/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ name = 'interface'
source = 'member'
dependencies = ['std']

[[package]]
name = 'intermediate_variables'
source = 'member'
dependencies = [
'core',
'std',
]

[[package]]
name = 'letter_casing'
source = 'member'
Expand Down
1 change: 1 addition & 0 deletions docs/reference/src/code/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"language/style-guide/letter_casing",
"language/style-guide/pattern_matching",
"language/style-guide/struct_shorthand",
"language/style-guide/intermediate_variables",
"language/traits/associated-consts",
"language/variables",
"misc/advanced-concepts/enum_advanced",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
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" }
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
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}}
```

0 comments on commit c54b5d0

Please sign in to comment.