Current location: Home> Grok Tutorial> How to use spaces and indentation reasonably in Grok code?

How to use spaces and indentation reasonably in Grok code?

Author: LoRA Time:

In Grok code, the rational use of spaces and indentation can help enhance the readability and maintainability of the code. The following is a detailed introduction to how to use it:

1. Basic indentation rules

1.1 Language-specific indentation specifications

 # Python: 4 spaces indent (never use tab characters)
def example_function():
    if condition:
        print("Correct indent")

# Java/JavaScript: 2 or 4 spaces public class Example {
    public static void main(String[] args) {
        if (condition) {
            System.out.println("correct indent");
        }
    }
}

1.2 Clear instructions to Grok

 When generating Python code:
- Strictly use 4 spaces indentation - Do not mix tabs and spaces - Continue line indentation with an extra level (8 spaces)

2. Spaces around operators

2.1 Standard operator spacing

 # Correct example x = (a + b) * (c - d)

# Tips for Grok:
"When generating code, a space is left on both sides of the operator, except for power operations"
result = a**2 + b**2 # Power operators are tightly connected

2.2 Special circumstances handling

 # Slicing operation without spaces correct = list[1:3]

# The default parameter equal sign of the function does not add spaces to def func(param=default):

3. Spaces in function and class definitions

3.1 Function spacing

 # Two blank lines between functions def function_one():
    pass


def function_two(): # There are two blank lines here pass

3.2 Class definition specification

 class MyClass:
    """Class document string"""

        def __init__(self):
        self.attribute = value

Tips for Grok:

 When generating class code:
1. A blank line after the class document string 2. A blank line between methods 3. Directly followed by the attribute definition after __init__

4. Parameter list and call spaces

4.1 Function definition

 # Def function(param1, param2):

4.2 Function calls

 # Correct call method result = calculate_total(items, discount=0.1)

# Avoid this result = calculate_total( items, discount = 0.1 )

5. Indentation of multi-line structure

5.1 Multi-line format for list/dictionary

 # Correct multi-line indentation config = {
    'name': 'value',
    'long_parameter': 'another_value',
    'nested': {
        'inner': 'data'
    }
}

5.2 Chain method call

 # The correct way result = (query.filter(condition)
          .group_by('category')
          .order_by('date'))

Tips for Grok:

 When generating multi-line structure:
1. The elements in brackets are aligned vertically 2. One element per line 3. The ending brackets are a separate line

6. Indentation of conditional statements and loops

6.1 Complex conditional format

 # Multi-line conditional statement if (long_condition_1 and
        long_condition_2 or
        special_case):
    handle_condition()

6.2 Indentation in a loop

 # List comprehension line break results = [
    transform(x)
    for x in collection
    if condition(x)
]

7. Format of import statement

7.1 Standard import grouping

 # 1. Standard library import os
import sys

# 2. Third-party library import numpy as np

# 3. Local module from . import utils

8. Comments indentation specification

8.1 In-line comments

 x = x + 1 # Compensation boundary condition

8.2 Block Comments

 # This loop handles the following situations:
# 1. Data Cleaning# 2. Outlier Filtering for item in collection:
    process(item)

9. Exception handling format

9.1 Standard exception handling

 try:
    risky_operation()
except SpecificError as e:
    handle_error(e)
except (Error1, Error2) as e:
    handle_multiple(e)
Finally:
    cleanup()

10. Full prompt template for Grok

 Please generate [language] code to comply with the following spaces and indentation rules:
1. Indentation: [4 spaces/2 spaces/tab]
2. Operator: Spaces on both sides [Yes/No], except [Specific Operator]
3. Function: [number of empty lines] after definition, [space rules] after parameter list
4. Multi-line structure: [Alignment], [Maximum Line Width]
5. Comments: [In-line/block comment] format requirements 6. Import: [Group Requirements], [Blank Line Separation]