#Is making lambda functions inside function like this good or bad practice?

34 messages · Page 1 of 1 (latest)

red hill
#
{
    Super::BeginPlay();

    auto AssignParameters = [&]()
    {
        TileBoxExtends = FVector(TileSizeX / 2, TileSizeY / 2, TileSizeY / 2);
        //Cast all room designs.
    };
    for (auto RoomDesign : RoomDesigns)
    {
        ARoomActor* CastedRoom = Cast<ARoomActor>(RoomDesign->GetDefaultObject());
        CastedRooms.Add(CastedRoom);
    }
    
    
    AssignParameters();
    GenerateMap();
    
    if (VisualizeOverlaps || VisualizeVisited)
    { 
        VisualizeTiles();
    }
}```
#

the main idea is whenever I make a little complex logic I want to extract as method but I am %100 sure it will be only used for one function and class itself is very complex already. So in these cases I am doing like this

brazen dirge
red hill
#

what should I do instead

brazen dirge
#

Just pull it out into another method if you don't want to just put that logic in-line in BeginPlay

#

Methods don't have to be called by more than one function, they're just a way to develop building blocks for your program

red hill
#

extracting method? But the class is over 1500 line already and 50 function. And %100 this method will only be used in BeginPlay not other place

#

I can barely navigate to other functions. Still I should extract as method?

brazen dirge
#

this won't significantly impact the size of your file

#

What strategies are you using to navigate your code?

#

What IDE are you using

red hill
#

yeah but it gets hard and harder to navigate. I am using Rider. Ctrl F12 is great to find all the functions easily

#

but over 50 function class is very complex already I don't want to see anymore functions that I only use for one function. GPT4 also suggested this is valid way to handle this scenarios. In C# local functions have totally same idea

#

I could inline to abstract and never bother to see again but that would make it more bad practice

brazen dirge
#

If you think pulling the logic into another method is a bad idea pulling into a lambda really isn't better 😛

#

But at the end of the day do whatever you find readable and clear 👍

red hill
#

so, making lambda functions inside function is Always considered bad practice?

#

in general

brazen dirge
#

Fwiw it sounds like you have bigger issues with this class than this lambda function

red hill
#

😦

brazen dirge
#

Namely it sounds likely that it's trying to do too much

red hill
#

one functionality one main algorithm focus

#

but I guess it's normal a procedural generation algorithm is normal to be defined just in a class

brazen dirge
#

Why does a class that does procedural generation have a BeginPlay method?

red hill
#

casting the classes and assigning to pointer holder initalizing the main method that does all the procedural generation

#

Is this also extremely bad practice?

red hill
# brazen dirge Why does a class that does procedural generation have a `BeginPlay` method?

I Found this. Last answer makes sense and justifies my reason to use Lambda function. https://stackoverflow.com/questions/39730224/helper-functions-lambdas-vs-normal-functions

red hill
brazen dirge
red hill
#

If you won't need the two helper functions somewhere else in your code, then use your lambda method , but if you will call one of them again somewhere in your project avoid writing them each time as lambdas

brazen dirge
#

But anyway, at the end of the day it's not a massive deal just go with whatever you prefer for now

#

There are other things that matter more