Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we simplify this? #45

Open
poul250 opened this issue Dec 5, 2020 · 3 comments
Open

Can we simplify this? #45

poul250 opened this issue Dec 5, 2020 · 3 comments

Comments

@poul250
Copy link

poul250 commented Dec 5, 2020

It looks verdy difficult

public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range, std::string argumentName, std::string message)
{
auto messageBuilder = [&]() -> std::string { return message; };
ArgumentInRange(root, argumentValue, range, argumentName, messageBuilder);
}
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range, std::string argumentName)
{
auto messageBuilder = [&]() -> std::string { return std::string("Argument value [").append(Platform::Converters::To<std::string>(argumentValue)).append("] is out of range ").append(Platform::Converters::To<std::string>(range)).append(1, '.'); };
ArgumentInRange(root, argumentValue, range, argumentName, messageBuilder);
}

Why do we create lambdas, recursively descend into other functions, sums a bunch of lines to check that an element is in the range? And why do we throw an exception if the element is not in the range, when we can return a boolean value? Can we simplify this?

@uselessgoddess
Copy link
Member

We can write noexcept

@Konard
Copy link
Member

Konard commented Jul 27, 2021

The reason lambdas are used is lazy execution. We do not need to calculate the string until the exception is thrown.
This method called Ensure::Always::ArgumentInRange the reason it exists is to ensure the condition is met. If it is not, the exception is thrown. You can always use range.Contains method if you would like to check it yourself. This method returns boolean. If you need to throw an exception - use Ensure::Always::ArgumentInRange, if you don't want to use exceptions, just don't use this method.

@uselessgoddess
Copy link
Member

The reason lambdas are used is lazy execution. We do not need to calculate the string until the exception is thrown.
This method called Ensure::Always::ArgumentInRange the reason it exists is to ensure the condition is met. If it is not, the exception is thrown. You can always use range.Contains method if you would like to check it yourself. This method returns boolean. If you need to throw an exception - use Ensure::Always::ArgumentInRange, if you don't want to use exceptions, just don't use this method.

Use std::string_view with static string messages for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants