What do you think about this identation technique?

I was putting some comments on an old script I have and thought about this technique for using comments:

//These functions make awesome stuff 
    Func1();
    Func2();
    Func3();

Func4();

I think that by using this it would be more clear that the Func4 wasn’t doing awesome stuff instead of this method:

//These functions make awesome stuff 
Func1();
Func2();
Func3();

Func4();

I tend to use regions for this.

Indentation is useful to indicate scope, but not much else.

Regions, or just add another comment to separate the functionality…

// Do some awesome stuff
Func1();
Func2();
Func3();

// Do something else that is different than awesome stuff
Func4();

there are five times when I would use indentation.

  1. When writing code in a scope
public void Func()
{
    //some code here
}
  1. When I’m calling a function with several parameters I would sometimes write them multiline if it would make them read better. similar with the common convention of writing inline arrays into a function
Debug.LogFormat(
    "The GameObject '{0}'  did not have the component '{1}' thus {2} could not target it.",
    target.name,
    componentType.name,
    gameObject.name);
  1. When I’m doing object chaining calls I’ll typcally put each chained call on an indented line. A habit I picked up from writing in jQuery. I typically do this with Linq.
ITargetiable[] sortedTargets = allGameObjectsInRange
       .Select(go=>go.GetComponent<ITargetiable>())
       .Where(t=>t!= null && t.IsTargetable)
       .OrderBy(t=>t.GetTargetPriority())
       .ToArray();
  1. Ternary Operators. To me it just looks clean indenting them like so and became habit back when I wrote in AVISynth where Ternarys were literally the only control structure you had for making a decision. It didn’t support “if statments” or any loop strutcures, just ternarys and recursive calls.
Color fgc_default = GUI.contentColor;
Color fgc_Invalid = new Color(1.0f,0.5f,0.5f);

bool fieldValid = !string.IsNullOrEmpty(serializedProperty.stringValue);

GUI.contentColor = (fieldValid)
    ?fgc_default
    :fgc_Invalid;

EditorGUILayout.PropertyField(serializedProperty);

GUI.contentColor = fgc_default;

5)Grouping Functions, typically those in EditorGUILayout. I usually indent the code inbetween the begin and end as it helps me keep a cognitive track on where the code will be drawn it also helps me spot any semantic typos with thelayout

EditorGUILayout.BeginHorizontal();

    EditorGUILayout.BeginVertical();
        //Column A Code
    EditorGUILayout.EndVertical();

    EditorGUILayout.BeginVertical();
        //Column B Code
    EditorGUILayout.EndVertical();

    EditorGUILayout.BeginVertical();
        //Column C Code
        EditorGUI.BeginDisabledGroup(booleanValue);
            // Disabled Group Code
        EditorGUI.EndDisabledGroup();

    EditorGUILayout.EndVertical();

EditorGUILayout.EndHorizontal();

As per grouping a set of functions together, I’m with BoredMormon. Regions are better suited for this.it gives a distinct area where the awesome functions are and it something that easily collapsible by the IDE

2 Likes