Code Feedback

I’m looking for code feedback on handling if checks, nullptr, and ensure()

This is how Sam wrote it

bool UMainMenu::Initialize()
{
	bool Success = Super::Initialize();
	if (!Success) return false;

	if (!ensure(Host != nullptr)) return false;
	Host->OnClicked.AddDynamic(this, &UMainMenu::HostServer);

	return true;
}

this is the code I wrote:

bool UMainMenu::Initialize()
{
	if (!Super::Initialize()) return false;

    // Host = nullptr;
	if (ensure(Host))
	{
		Host->OnClicked.AddDynamic(this, &UMainMenu::OnHostClicked);
	}

	return true;
}

I like that when using ensure is logs a call stack of where it happened. I’d never used ensure before. I’m being real nick-picky about some of the checks and how conditions are handled and it probably comes down to personal preference, but I hope to get some feedback. Opinions are welcome as well, doesn’t have to be official standard although that’s preferred.

1 Like

They are pretty much the same. There are arguments to sam’s approach for early exit as it removes indenting thus making the code more readable. Similarly assigning to a variable makes it a little more readable.

Neither implementations actually adhere to the recommended C++ style guidelines as it happens but style can be a personal preference.

If it works for you, that actually is fine.

1 Like

Thanks! I think I’ll go with the early return when possible for readability. I was a big fan of early return vs nesting in the past. So in this example, early return for the Super call, but once we add Join then Join and Host I will nest with if-init type scope thingy instead of an early return.

Privacy & Terms