Skip to content

High Cyclomatic Complexity in flags Function #4#10

Merged
UltiRequiem merged 1 commit intobobadilla-tech:mainfrom
Antoineio:fix/flags-function
Mar 14, 2025
Merged

High Cyclomatic Complexity in flags Function #4#10
UltiRequiem merged 1 commit intobobadilla-tech:mainfrom
Antoineio:fix/flags-function

Conversation

@Antoineio
Copy link
Collaborator

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

func resolveFlag[T comparable](longVal, shortVal *T, zero T, longName, shortName string) T {
    if *longVal != zero && *shortVal != zero {
        repeatedFlag(longName, shortName)
    }
    if *longVal != zero {
        return *longVal
    }
    return *shortVal
}

How It Works

Generic Constraint (T comparable)
The comparable constraint ensures that the type T supports == and !=, which is necessary for checking if a flag is set
This works in the same way for int, string, and bool, which are naturally comparable

The function takes pointers to the flag values (longVal and shortVal)
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, and resolveBoolFlag to achieve the same result

Example

If both the short and long flags are provided, an error is thrown

[antoine@fedora lorelai (fix/flags-function)]$ ./lorelai -w 1 --words 1
You cannot pass --words and -w at the same time!

If only one of the flags is provided, no error is thrown

[antoine@fedora lorelai (fix/flags-function)]$ ./lorelai -p 1 --words 1
Quisque
Vulputate pellentesque cras sollicitudin parturient bibendum id id justo et commodo quis sed pharetra orci dignissim eu egestas consequat lorem ex amet leo sapien vel phasellus ridiculous dis leo dolor finibus ut feugiat eget adipiscing diam felis dignissim sed massa dolor consequat vel tortor morbi.

Cyclomatic Complexity

By doing this, we can resolve #4 because we no longer have the complexity of multiples if statement

[antoine@fedora lorelai (fix/flags-function)]$ goreportcard-cli -v
Grade .......... A+ 100.0%
Files ................. 15
Issues ................. 0
gofmt ............... 100%
go_vet .............. 100%
gocyclo ............. 100%
ineffassign ......... 100%
license ............. 100%
misspell ............ 100%
[antoine@fedora lorelai (fix/flags-function)]$ 

Let me know if any adjustments are required 💪

…f resolveFlag function + upgrade go 1.17 -> 1.18
Copy link
Member

@UltiRequiem UltiRequiem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job pal, I will release a new version with these changes.

@UltiRequiem
Copy link
Member

Thanks again for the great PR

@UltiRequiem UltiRequiem merged commit 22c1e74 into bobadilla-tech:main Mar 14, 2025
4 checks passed
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

Successfully merging this pull request may close these issues.

High Cyclomatic Complexity in flags Function

2 participants