Allar / ue5-style-guide

An attempt to make Unreal Engine 4 projects more consistent
http://ue5.style
MIT License
5.33k stars 1.15k forks source link

How should I handle name collisions from class variables to function variables and locals? #91

Open namrog84 opened 2 years ago

namrog84 commented 2 years ago

This is an arbitrary example but let's say I have a funciton

void SomeClass::SetTarget(AActor* InTarget){
    Target = InTarget;
    ...
    AActor* localTarget = Target; // is local the right prefix for this?
    ...
}

Traditionally in previous teams I've always named the member variable as m_target in this case but I know that's not recommended. I have seen some instances of Epic using My as a prefix but that sometimes doesn't look right either.

I could rename the function parameter to be (AActor* InTarget)

Then what about if I need a local function variable do I name that localTarget?

I've yet to find consistent unreal specific guidelines for dealing with these 3 scopes since I thought I read that using this-> isn't typically recommended?

namrog84 commented 2 years ago

In the official UE they say don't have shadowed variables but don't offer reasonable suggested change. e.g. https://docs.unrealengine.com/5.0/en-US/epic-cplusplus-coding-standardblueprint-debugging-in-unreal-engine/

Shadowed variables are not allowed. C++ allows variables to be shadowed from an outer scope, but this makes usage ambiguous to a reader. For example, there are three usable Count variables in this member function:

class FSomeClass {
public:
    void Func(const int32 Count) {
        for (int32 Count = 0; Count != 10; ++Count) {
            // Use Count
        }
    }
private:
    int32 Count;
}