I formatted my code a little differently, but it still worked... I think?

(https://i.gyazo.com/e16166551982c5125017cdf0b2585eea.png)

Firstly I want to say I appreciate Ben going through what we learned slowly at the end of the video, I hope he does that more often throughout the course, helps slow people like myself.

In the link I put above you can see I formatted the whole ObjectName, GetOwner thing differently, that’s how I formatted it during the challenge but it still worked as intended once I got into Unreal’s log.

What I want to know is whether there’s any downside or hidden drawback to the way I did it? Aside from it taking two lines instead of one.

In the world of programming, there’s often more than one way to do things. When writing code we often try to strike a balance between performance, readability, and maintainability. The priority given to these properties usually depends on the type of application.

With regard to the code you shared, I would encourage you to take a look at the actual implementations of the GetName function in UObjectBaseUtility.h. You can see that they essentially do the same thing - one version of the function returns an FString, and another version of the same function (called overloading) takes in a reference to an FString that can be modified (notice that the parameter is not const qualified). It is the latter version that you are calling with your code. Also notice that both functions are decorated with FORCEINLINE, meaning that they will be inlined at compile time (you can read about the pros and cons of inline functions here.

So, the short answer is: in this case it really comes down to personal preference. There’s nothing wrong with your code if it is more readable to you and it performs the intended task. At this stage I would say it is more important that you understand what the code does so you can apply that knowledge in the future.

2 Likes

Privacy & Terms