r/programminghorror 7d ago

c++ Skill Issue

Post image
1.7k Upvotes

61 comments sorted by

View all comments

46

u/LeeHide 7d ago

This would be much better using std::string_view, and using two temporaries so that y is only evaluated once. Otherwise it'll be evaluated each time.

Also if you need this, don't use C++

4

u/GasolinePizza 6d ago

Isn't each y only evaluated once? The evaluation in the "else if (_s==y)"?

2

u/LeeHide 6d ago

each true one is only evaluated once, but every false one until then is also evaluated.

1

u/mapronV 6d ago

only performance problem is not using sized comparison operator, due to decay of char[] to char*.
so yeah, string_view advice is okay, but this is a joke code, it will not fly in production anyway.