Преглед изворни кода

Add a GDScript style guide recommendation for multiline wrapping

Hugo Locurcio пре 4 година
родитељ
комит
83a9c5e254
1 измењених фајлова са 50 додато и 2 уклоњено
  1. 50 2
      getting_started/scripting/gdscript/gdscript_styleguide.rst

+ 50 - 2
getting_started/scripting/gdscript/gdscript_styleguide.rst

@@ -293,11 +293,59 @@ The only exception to that rule is the ternary operator:
 
    next_state = "fall" if not is_on_floor() else "idle"
 
+Format multiline statements for readability
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When you have particularly long ``if`` statements or nested ternary expressions,
+wrapping them over multiple lines improves readability. Since continuation lines
+are still part of the same expression, 2 indent levels should be used instead of one.
+
+GDScript allows wrapping statements using multiple lines using parentheses or
+backslashes. Parentheses are favored in this style guide since they make for
+easier refactoring. With backslashes, you have to ensure that the last line
+never contains a backslash at the end. With parentheses, you don't have to
+worry about the last line having a backslash at the end.
+
+When wrapping a conditional expression over multiple lines, the ``and``/``or``
+keywords should be placed at the beginning of the line continuation, not at the
+end of the previous line.
+
+**Good**:
+
+::
+
+    var angle_degrees = 135
+    var quadrant = (
+            "northeast" if angle_degrees <= 90
+            else "southeast" if angle_degrees <= 180
+            else "southwest" if angle_degrees <= 270
+            else "northwest"
+    )
+
+    var position = Vector2(250, 350)
+    if (
+            position.x > 200 and position.x < 400
+            and position.y > 300 and position.y < 400
+    ):
+        pass
+
+**Bad**:
+
+::
+
+    var angle_degrees = 135
+    var quadrant = "northeast" if angle_degrees <= 90 else "southeast" if angle_degrees <= 180 else "southwest" if angle_degrees <= 270 else "northwest"
+
+    var position = Vector2(250, 350)
+    if position.x > 200 and position.x < 400 and position.y > 300 and position.y < 400:
+        pass
+
 Avoid unnecessary parentheses
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 Avoid parentheses in expressions and conditional statements. Unless
-necessary for order of operations, they only reduce readability.
+necessary for order of operations or wrapping over multiple lines,
+they only reduce readability.
 
 **Good**:
 
@@ -481,7 +529,7 @@ Use snake_case for file names. For named classes, convert the PascalCase class
 name to snake_case::
 
     # This file should be saved as `weapon.gd`.
-    class_name Weapon    
+    class_name Weapon
     extends Node
 
 ::