Search Unity

Group blocks of code without methods

Discussion in 'Scripting' started by Dextozz, Oct 11, 2019.

  1. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    I have a block of code that's being repeated 2 times inside of a single method. I could create another method to hold that code and reuse it, but it would require many parameters and it wouldn't really work well. Here's an example of what I have now:
    Code (CSharp):
    1. private void foo()
    2. {
    3.     for (int i=0; i<n; i++)
    4.     {
    5.         // Block of code
    6.     }
    7.    
    8.     // Same block of code
    9. }
    The code basically converts a few lists to arrays. I'm filling up 4 lists with values up to a certain point. After that point, I just convert those lists to arrays. It's very simple but takes a few lines of code. If I created a method for this block of code I'd have to pass in 4 lists as parameters and still somehow clear their values (so they can be refilled again).

    Anyhow, is there a way to create a 'local' function without parameters? I hear of lambda expressions but I'm not sure if they can be of any help here.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Unless all 4 list are required at the same time, you only need a method that takes a list and performs on that list, then you can call it 4 times, passing in only the data you need at the time you need it.

    Clearing list is easy. List already have a way to clear themselves with .Clear();

    There isn't anything wrong with using more lines of code to do something if it's clear and readable. Normally you just don't want redundant code. We could probably help you better if you showed a bit more of what you are currently doing.
     
    Last edited: Oct 12, 2019
  3. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Local functions exist in C# 7. See here. They are like other functions in that they take arguments, but they can use values from the scope they're defined within without those having to be passed.
     
    Yoreki likes this.
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Depending, a small class is often good for helper functions on shared variables. fooHolder has the foo function, except using member variables for the lists. Then your private helper functions have free access to them. It's mostly good when the lists are persistent.

    But a "lambda" function (an anonymous function, in computer terms) will work. They capture the scope where you create them. Here f1 can see and modify local n:

    Code (CSharp):
    1. void testLambda() {
    2.   int n=5; // this n is increased by f1, twice
    3.   System.Action<int> f1 = (x) => { n+=x; };
    4.   f1(2); print(n); // 7
    5.   f1(6); print(n); // 13
    6. }
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Note that lambda functions will cause a heap allocation, because anonymous functions have to be turned into delegates (whereas local functions do not). Therefore you should avoid creating them in functions that are called frequently, such as Update.