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

Avoid exception handling for verifying algorithm validity #212

Merged
merged 2 commits into from
Mar 9, 2024

Conversation

ararslan
Copy link
Contributor

@ararslan ararslan commented Mar 5, 2024

Currently, when a Symbol is passed to the Opt constructor or the algorithm_name function, the corresponding Algorithm object is obtained via a Dict lookup inside of a try/catch, where the catch permits throwing a more informative error. The use of exception handling here isn't necessary though; rather than using getindex and catching any resulting exception, we can instead use get with a default value that does not correspond to a valid algorithm and check for equality with that. (Alternatively we could guard the getindex with a haskey but that requires performing the lookup twice.) This change to using get is implemented here.

To avoid duplicating the code that checks a Symbol for validity as an algorithm across multiple functions, I've defined a constructor method Algorithm(::Symbol) that includes this check. This also provides a pleasant simplification of the methods required, since Symbol and Integer inputs can now go through the same Opt and algorithm_name methods.

Currently, when a `Symbol` is passed to the `Opt` constructor or the
`algorithm_name` function, the corresponding `Algorithm` object is
obtained via a `Dict` lookup inside of a `try`/`catch`, where the
`catch` permits throwing a more informative error. The use of exception
handling here isn't necessary though; rather than using `getindex` and
catching any resulting exception, we can instead use `get` with a
default value that does not correspond to a valid algorithm and check
for equality with that. (Alternatively we could guard the `getindex`
with a `haskey` but that requires performing the lookup twice.) This
change to using `get` is implemented here.

To avoid duplicating the code that checks a `Symbol` for validity as an
algorithm across multiple functions, I've defined a constructor method
`Algorithm(::Symbol)` that includes this check. This also provides a
pleasant simplification of the methods required, since `Symbol` and
`Integer` inputs can now go through the same `Opt` and `algorithm_name`
methods.
@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 68.83%. Comparing base (301b759) to head (084b4ba).

Files Patch % Lines
src/NLopt.jl 83.33% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #212      +/-   ##
==========================================
+ Coverage   68.46%   68.83%   +0.37%     
==========================================
  Files           2        2              
  Lines         799      799              
==========================================
+ Hits          547      550       +3     
+ Misses        252      249       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ararslan
Copy link
Contributor Author

ararslan commented Mar 5, 2024

It's weird, I'm a member of JuliaOpt but I can't actually do anything in the org, including request PR reviews. So manually pinging @stevengj for review here.

@stevengj
Copy link
Collaborator

stevengj commented Mar 5, 2024

LGTM.

src/NLopt.jl Outdated Show resolved Hide resolved
Co-authored-by: Steven G. Johnson <[email protected]>
@ararslan
Copy link
Contributor Author

ararslan commented Mar 5, 2024

FYI I also can't merge PRs here

@odow odow merged commit 67acb7a into jump-dev:master Mar 9, 2024
8 checks passed
@ararslan ararslan deleted the aa/only-do-there-is-no-try branch March 11, 2024 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants