Skip to content

Commit a76a96e

Browse files
committed
docs: best practices
1 parent 347529f commit a76a96e

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

GEMINI.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pnpm format
2121

2222
Apps Script supports the V8 runtime, which enables modern ECMAScript syntax. Using these features makes your code cleaner, more readable, and less error-prone.
2323

24-
### 1. `let` and `const`
24+
### `let` and `const`
2525
Use `let` and `const` instead of `var` for block-scoped variables.
2626

2727
* **`const`**: Use for values that should not be reassigned.
@@ -37,15 +37,15 @@ if (true) {
3737
// local is not accessible here
3838
```
3939

40-
### 2. Arrow Functions
40+
### Arrow Functions
4141
Use arrow functions for concise function expressions, especially for callbacks.
4242

4343
```javascript
4444
const numbers = [1, 2, 3];
4545
const squares = numbers.map(x => x * x); // [1, 4, 9]
4646
```
4747

48-
### 3. Destructuring
48+
### Destructuring
4949
Unpack values from arrays or properties from objects into distinct variables.
5050

5151
```javascript
@@ -56,7 +56,7 @@ const coords = [10, 20];
5656
const [x, y] = coords;
5757
```
5858

59-
### 4. Template Literals
59+
### Template Literals
6060
Use template literals for string interpolation and multi-line strings.
6161

6262
```javascript
@@ -69,7 +69,7 @@ const multiLine = `
6969
`;
7070
```
7171

72-
### 5. Default Parameters
72+
### Default Parameters
7373
Specify default values for function parameters.
7474

7575
```javascript
@@ -80,6 +80,23 @@ function greet(name = "Guest") {
8080
greet(); // "Hello, Guest!"
8181
```
8282

83+
### Prefer `for...of` for Iteration
84+
While `forEach` is convenient, `for...of` loops generally offer better performance and more control (e.g., `break`, `continue`) in Apps Script, especially when dealing with large arrays.
85+
86+
```javascript
87+
const numbers = [1, 2, 3];
88+
89+
// Using forEach (less performant for large arrays)
90+
numbers.forEach(num => {
91+
console.log(num);
92+
});
93+
94+
// Using for...of (preferred)
95+
for (const num of numbers) {
96+
console.log(num);
97+
}
98+
```
99+
83100
## Apps Script V8 Runtime
84101

85102
It's important to understand that the Apps Script V8 runtime is
@@ -237,7 +254,7 @@ function greet(name, age) {
237254

238255
### Advanced Patterns
239256

240-
#### 4. Custom Objects (@typedef)
257+
#### 1. Custom Objects (@typedef)
241258
For complex objects, define a type using `@typedef`.
242259

243260
```javascript
@@ -257,7 +274,7 @@ function processUser(config) {
257274
}
258275
```
259276

260-
#### 5. Type Casting
277+
#### 2. Type Casting
261278
Sometimes the type checker cannot infer the type correctly. Use inline `@type` to cast.
262279

263280
```javascript
@@ -267,7 +284,7 @@ const data = JSON.parse(jsonString);
267284
const config = data;
268285
```
269286

270-
#### 6. Arrays and Generics
287+
#### 3. Arrays and Generics
271288
Specify array contents clearly.
272289

273290
```javascript
@@ -280,7 +297,7 @@ function lengths(names) {
280297
}
281298
```
282299

283-
#### 7. Handling `null` and `undefined`
300+
#### 4. Handling `null` and `undefined`
284301
Be explicit if a value can be null.
285302

286303
```javascript

0 commit comments

Comments
 (0)