Returning Boolean Literals
This is a bad code example when a function returns boolean values with if else checking. Boolean is a binary results. Hence, a lot of languages offers return with comparison features.
Bad Codes
Bad Codes
Here is an example of bad codes:
func IsRoot() bool {
id := os.Getuid()
if id <= 0 {
return true
}
return false
}
Better Codes
Better Codes
From the bad codes, we can actually compare the condition straight with the function and return the end result. Hence, the function should be:
func IsRoot() bool {
return os.Getuid() <= 0
}
One line, crisp and clear.
That's all about bad returning Boolean literal codes.