godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.95k stars 3.23k forks source link

GDScript - Spacing between methods #2505

Closed aaronfranke closed 5 years ago

aaronfranke commented 5 years ago

In many GDScript projects, I have seen several ways to separate functions. Sometimes one line:

func _ready():
    print("ready")

func _process(delta):
    print("process")

But sometimes two:

func _ready():
    print("ready")

func _process(delta):
    print("process")

I have also seen functions separated by 3 or more lines. I think that we should probably have no more than 2, but what should the policy be between 1 and 2 lines? Situationally? User discretion? Encourage one over the other? Disallow one or the other entirely?

We should probably decide on something and put it in the style guide.

NathanLovato commented 5 years ago

We've used both at GDquest, but use 2 now. It makes the code a lot easier to read, as you may have empty lines inside of methods or between variable definitions. It's instantly clear that 2 line gaps separate methods. From the GDquest gdscript guidelines:

Always leave 2 blanks lines between methods to visually distinguish them and other code blocks.

The Python pep8 recommends 2 lines between functions or classes inside python modules for the same reason (and inside a class 1 line between methods). To me, it has a practical benefit. Especially for people reading the docs, the clearer and the more visually organized, the easier it is to read.

Calinou commented 5 years ago

I agree with recommending 2 blank lines between methods. My projects currently separate methods with one blank line only, but I'll gladly switch to 2 if this recommendation gets added to the style guide.

Like in Python, we should also advise using only one consecutive blank line in a method body.

aaronfranke commented 5 years ago

What about before methods?

var x

func test():

vs

var x

func test():