Is it best practice to use a trailing comma in function parameters when broken into multiple lines?

In the style guide documentation it says the following:

Trailing comma
Use a trailing comma on the last line in arrays, dictionaries, and enums. This results in easier refactoring and better diffs in version control as the last line doesn’t need to be modified when adding new elements.

Does this also apply when writing functions with parameters with multiline formatting?

Example:

func _init(
			filter: Callable,
			image: Image,
			suffix: String, # <- Should this comma be here?
		) -> void:
## function body

The better diffs in version control still stands true, so this feels like the right choice.

1 Like

It’s personal preference. I use both, and there are no significant consequences either way.

1 Like

I did not know I could do this. So I tried it.

Instead of this:

@rpc("any_peer","call_remote","unreliable_ordered",0)
func send_position(Client_ID:int, Client_Position:Vector3) -> void:
	
	pass

It’s now this:

@rpc("any_peer","call_remote","unreliable_ordered",0)
func send_position(
Client_ID:int, 
Client_Position:Vector3) -> void:
	
	pass

I don’t like the syntax that much. I would only use this function argument syntax style if I have either a lot of arguments and/or have long argument names.

It’s good to know I can do this though.

You can safely ignore the style guide. Format your code however you like, or if you’re in a team, hash it out as a team. Style guides are just opinions, even if they’re often presented as “best practices”.

Format your code however makes it most readable to you. The opinion of someone else who won’t have to read or maintain that code is of passing interest at best; maybe a source of ideas, but not a source of ground truth.

3 Likes