C# Dynamo Nodes - optimising Multireturn Node

Hi,

My task here is to add to each node additional single Boolean output that will say True if data was exported. Multiline Node outputs are a bit more complex.
I can define code in 2 lines with a single output and 12 lines with the additional output. So there are 10 additional lines for this extra information.
Is there a way to get it optimised???

/// <summary>
/// Gets TAS 3D Zone Name
/// </summary>
/// <param name="Zone">TAS Zone</param>
/// <returns name="Name">Zone Name</returns>
/// <search>
/// TAS, Zone, zone, Zone, zone, name, Name
/// </search>
public static string Name(Zone Zone)
{
    return Zone.pZone.name;   // when I connect zones returns list of Names
}

/// <summary>
/// Gets TAS 3D Zone Name and Boolean
/// </summary>
/// <param name="Zones">TAS Zone</param>
/// <returns name="Name">Zone Name</returns>
/// <returns name="bool">True/False</returns>
/// <search>
/// TAS, T3D, zone, Zone, zone, name, Name
/// </search>
[MultiReturn(new[] { "zone", "bool" })]
public static Dictionary<string, object> Name(List<Zone> Zones)
{
    List<string> outzones = new List<string>();
    foreach (Zone zone in Zones)
    {
        outzones.Add(zone.pZone.name);

    }
    bool outbool = false;
    if (outzones.Count != 0)
    {
        outbool = true;
    }
    return new Dictionary<string, object>
    {
        { "zone", outzones },
        { "bool", outbool }
    };
}

Aside from the obvious, like condensing your brackets, you could output the outbool using a ternary operator and eliminate the need for a new variable + if statement

return new Dictionary<string, object>
{
    { "zone", outzones },
    { "bool", outzones.Count != 0 ? true : false }
};
1 Like

Thanks, @Thomas_Mahon Thomas there is good improvement,
We have now 2 lines against 8 lines,
I am wondering if this is the limit or can be even more improved?

    /// <summary>
    public static string Name(Zone Zone)
    {
        return Zone.pZone.name;   // when I connect zones returns list of Names
    }

    /// <summary>

    [MultiReturn(new[] { "zone", "bool" })]
    public static Dictionary<string, object> Name(List<Zone> Zones)
    {
        List<string> outzones = new List<string>();
        foreach (Zone zone in Zones)
        {
            outzones.Add(zone.pZone.name);
        }
        return new Dictionary<string, object>
        {
            { "zone", outzones },
            { "bool", outzones.Count != 0 ? true : false }
        };
    }

Thats about as good as you’re going to get it. Code readability and efficiency boils down to the semantics of the language. You might think this code is cumbersome, but when writing other functions, like for example using LINQ methods and lambda expressions, you can write really efficient code. Ultimately it depends on what it is one is trying to do.

@Thomas_Mahon thanks for your help and support,
I got a new tip today which is already an improvement. In approach two we gain useful output but I have to define List, loop etc… I will apply this to some nodes as code one is so simple and short. Thanks again,
Lets is if there will be an improvement in Dynamo 2023 :slight_smile: …

It’s about readability and efficiency @Michal_Dengusiak. Shorter code (less lines) doesn’t actually make for better code. It sometimes becomes unreadable. You could for example do this:

[MultiReturn(new[] { "zone", "bool" })]
public static Dictionary<string, object> Name(List<Zone> Zones)
{
    List<string> outzones = Zones.Select(x => x.pZone.name).ToList();
    return new Dictionary<string, object>
    {
        { "zone", outzones},
        { "bool", outzones.Count != 0 ? true : false }
    };
}

This is using Linq so make sure to add the appropriate using statement.

Again, this is not “better”, as better would be very objective. As a matter of fact I would say that for someone reading your code I believe that the original for loop is easier to read.

2 Likes

Thanks, @Konrad_K_Sobon I agree that for me this is less readable, I will stick with my version…
ps. for me it is interesting that with single line output I do not have to introduce loop,

you can think of select statement like list.map in dynamo - iterates over a collection applying a function, and returning a list containing the result of all function calls.

2 Likes