High Cyclomatic Complexity in flags Function #4#10
Merged
UltiRequiem merged 1 commit intobobadilla-tech:mainfrom Mar 14, 2025
Merged
High Cyclomatic Complexity in flags Function #4#10UltiRequiem merged 1 commit intobobadilla-tech:mainfrom
UltiRequiem merged 1 commit intobobadilla-tech:mainfrom
Conversation
…f resolveFlag function + upgrade go 1.17 -> 1.18
UltiRequiem
approved these changes
Mar 14, 2025
Member
UltiRequiem
left a comment
There was a problem hiding this comment.
Good job pal, I will release a new version with these changes.
Member
|
Thanks again for the great PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello 👋
I'm opening this PR to resolve #4
In this PR i did an upgrade to Go 1.18, i did this in order to be able to use generics feature. Generics allow us to write more flexible and reusable code without duplicating logic for different types
For more information, you can see : https://go.dev/doc/tutorial/generics
Changes
To improve how we handle command-line flags, i implemented the following generic function
How It Works
Generic Constraint (
T comparable)The comparable constraint ensures that the type
Tsupports==and!=, which is necessary for checking if a flag is setThis works in the same way for
int,string, andbool, which are naturally comparableThe function takes pointers to the flag values (
longValandshortVal)It also takes
zero, which represents the default value for the given type (0 for int, "" for string, false for bool)This helps determine whether a flag has been explicitly set by the user
If both the long and short flags are set (not equal to zero), an error is triggered using
repeatedFlag()This function centralizes flag conflict resolution, making the code more maintainable and easily extendable
Without generics, we need separate functions like
resolveIntFlag,resolveStringFlag, andresolveBoolFlagto achieve the same resultExample
If both the short and long flags are provided, an error is thrown
If only one of the flags is provided, no error is thrown
Cyclomatic Complexity
By doing this, we can resolve #4 because we no longer have the complexity of multiples
ifstatementLet me know if any adjustments are required 💪