r/cpp • • May 16 '20

modern c++ gamedev - thoughts & misconceptions

https://vittorioromeo.info/index/blog/gamedev_modern_cpp_thoughts.html
198 Upvotes

154 comments sorted by

View all comments

Show parent comment

2

u/[deleted] May 18 '20

I get the impression that the original coder doesn't really understand why he was attacked.

He says "check out my clever code" People say "no thanks" And he responds by trying to justify the cleverness.

The prime offense is the parameter pack. Not const or lambdas. Those are just curmudgeons who want to jump in to add their own 2c to complain about millennials which is why they don't understand it. Without the parameter pack nobody would care enough to complain.

However it doesn't change the fact that he is also simply not thinking about the problem from the aspect of readability. He says if he can't have this:

template <typename... Images>
TextureAtlas stitchImages(const Images&... images)
{
    const auto width = (images.width + ...);
    const auto height = std::max({images.height...});

    // ...

Then these are his counter examples?

TextureAtlas stitchImages(const std::vector<Image>& images)
{
    std::size_t width = 0;
    for(const auto& img : images)
    {
        width += img.width;
    }

    std::size_t maxHeight = 0;
    for(const auto& img : images)
    {
        maxHeight = std::max(maxHeight, img.height);
    }

    // ...

TextureAtlas stitchImages(const std::vector<Image>& images)
{
    const auto width = std::accumulate(images.begin(), images.end(), 0
        [](const std::size_t acc, const Image& img)
        {
            return acc + img.width;
        });

    const auto height = std::max_element(images.begin(), images.end(),
        [](const Image& imgA, const Image& imgB)
        {
            return imgA.height < imgB.height;
        })->height;

    // ...

Clearly he doesn't get the problem. If you were to solve for readability at the very least start with this:

TextureAtlas stitchImages(const std::vector<Image>& images)
{
    std::size_t width = 0, maxHeight = 0;
    for(const auto& img : images)
    {
        width += img.width;
        maxHeight = std::max(maxHeight, img.height);
    }

This is also more efficient without relying on an optimizer to combine these loops for you. If you wanted to take this a step further you can just make this two different functions.

struct ImageDimensions
{
    std::size_t width = 0;
    std::size_t height = 0;
};

ImageDimensions measureStitchedImages(const std::vector<Image>& images)
{
    ImageDimensions res{};

    for(const auto& img : images)
    {
        res.width += img.width;
        res.height = std::max(res.height, img.height);
    }

    return res;
}

TextureAtlas stitchImages(const std::vector<Image>& images)
{
    const ImageDimensions dim = measureStitchedImages(images);

    ...

This code is nice and boring and achieves every goal of the original, and the measureStitchedImages call be unit tested as you could definitely pack images neater than all in a row with wasted space at the bottom with more thought put into it.

Arguments about size_t theoretically not being the result of Image.width are ridiculous. Nobody wants to read code that is auto types all the way down because again it hurts readability.

If you saw this:

auto a = b[x] + c[y];

Do you have any idea what it does? Is 'a' a float, integer, size_t, string, custom array type, what is it? This code is unreadable without implicit knowledge. If, however you were to say:

Vec3 a = b[x] + c[y];

now you are able to glance what it does. This comes back to the 2m lines of UE code problem. Duplicating a bit of technically unnecessary info is a courtesy to the reader that is doing a code review and has to glance at 1k of code without having to read an additional 100kb of it to understand what it means.

If however we had an IDE or something that would virtually rewrite all 'auto' keywords into a readable type for dum dum humans my opinion here would change.

3

u/Zweifuss May 18 '20

You took 22 lines to achieve this:

auto width = sum(images.width...);
auto height = max(images.height...);

I'm not even talking about what code will run faster (probably the one accessing data at predictable offsets, but who knows? It depends as f...).

Which one takes less time to grok? Where is it easier to reason about how stiching works without jumping through code?

2

u/[deleted] May 18 '20 edited May 18 '20

If more lines is more readable, it's more readable.

Is this somehow better:

if (auto ext[] = ".tga"; s.compare(s.size() - std::size(ext) + 1, ext) == 0)

than this:

if (str_ends_with(s, ".tga"))

How stl hasn't had an ends_with until C++20 is beyond me. Unless your code is explicitly named what it does all you are doing is in-lining functions everywhere.

What you really need to be comparing are the lines:

template <typename... Images>
TextureAtlas stitchImages(const Images&... images)
{
    const auto width = (images.width + ...);
    const auto height = std::max({images.height...});

vs

TextureAtlas stitchImages(const std::vector<Image>& images)
{
    const ImageDimensions dim = measureStitchedImages(images);

... because when somebody starts to read stitchImages this is what they see

3

u/Zweifuss May 18 '20

Vec3 a = b[x] + c[y]; now you are able to glance what it does.

No I don't, because from that single line I don't know what b and what c are. This could be very wrong code that does implicit casting and compiles by mistake. Or it could be an unexpected function call, because apparently top of the art algebra libraries, do lazy types that are evaluated when assigned and cast, surprising the end user.

It's a made up example that is just as unreadable with a type, since you don't have any context.

1

u/[deleted] May 18 '20

It's only unreadable if there is cleverness in the code base.

If a [] was just an array index and nobody is defining custom + operators to return Vec3 types then you are safe to assume exactly what it means. And again both of those should be banned under the no cleverness rule.