C++ Newbie - Please judge my C++ code.

Any attempt to improve speed of any piece of code without profiling it first.
Worrying about function speed when you don’t know how many millions of times it will be called is also premature optimization.

The term was probably coined by Donald Knuth. You can google it.

No, that’s readability and that one is done specifically to reduce maintenance cost.

You’re appear to be trying to turn that into semantic argument by shifting meaning of “optimization”. Please don’t do that. In this context “optimizations” refers to “performance optimization” only.

The reason why premature optimization is bad is because person performing it wastes development time without getting tangible benefit. Humans make mistakes. If you think that “this will be slow”, but you haven’t measured it, then you’re wrong by default.

Anything that is part of the language is not “my thing”. It is part of the language. Feature available in language takes precedence over feature available in framework, which takes precedence over feature created by you, unless there is a good reason. Type like FString and FName has a good reason to use it. However, that does not necessarily apply to FMath functions.

Now, what is a good reason to avoid round() function from cmath?

The first person for which code readability is important is you, because in few weeks or months you’ll forget what this subroutine is doing. Then it is your team. “Other unreal devs” only enter the picture if you’re planning to share the code OR preparing engine patch for submission.


I don’t like the tone this thread is taking. Is the point where we start insulting each other due to having different programming opinions?

I am having a very hard time understanding what that code is doing, is there a name for what is happening?

You can have opinions all you want. The fact remains UE4 uses a customized C++ implementation. If you include cmath then it shows only one thing: you haven’t truely arrived in UE4 yet. There’s no problem with that unless you give tips to to other UE4 devs and are being adamant about your solution being better than those already in UE4. At that point I think someone has to at least mention that this isn’t the way that most UE4 devs do it.

Hmm… They are pretty much the same thing. STL is just an old parent of what “The C++ Standard Library” is today, the std namespace. Old ppl still call it STL and they have no intention to change it seems. Std still makes use of Stl containers and all that stuff.

It’s a lambda expression.

Also, code duplication isn’t good as pointed out already… But as always, everybody ignores the fact that excessive indirections can hit your overall game performance; splitting everything into functions isn’t great idea either.
When games, huge as they are nowadays, are calling 80.000~300.000 functions every frame, there’s a performance hit in there. So you don’t necessarily wan’t everything formatted to a generic function call.
I don’t see any problem with the way he wrote his original code then; at the end of day if he doesn’t need to create a function call for it so be it.

Lambdas, “auto” keyword and uniform intiialization syntax.

No, this is false and misleading.

“Customized C++ implementation” would require custom compiler. There’s no company in the world insane enough try to do something like that for their product, especially with multiple platforms to support. C++ is the wrong language for this kind of stunt.

UE4 acts pretty much in the same way as Qt with its metaobject system. Few extra preprocessor directives that are handled in special fashion and generate code behind the scenes. There’s nothing special aside from that. If you meant build system, then build system is not part of the language.

Anyway, I’m done with this, since there’s no point in discussing it further.

At the end of the day, everyone has a point:

  • Balance readability/maintainability vs performance
  • Don’t do premature optimization. Do some profiling before optimizing anything.
  • Use UE4 libraries over the standard ones (i.e., FMath). There is a good reason for it: UE4 Libraries You Should Know About - Unreal Engine
  • If you’re aiming for high perfomance code, avoid using blueprint nodes on those. And convert blueprints to C++ code.
  • Attend DevCon such as GDC talk and learn from the other experts. No single developer has figured everything out anyways.
  • And oh, write clean and quality code. Your teammate/co-workers will love you for it.
  • And some things that I’ve missed.

Fine, I guess I worded it wrong but I don’t know how else to say it (or you’re just arguing semantics). UE4 uses a lot of custom types, macros, its own reflection system, its own build system, its own naming conventions etc. That’s why you don’t use a lot of stuff from the standard library. Epic has created their own engine-specific library instead and the bottom line is you should use it.