0

I am writing a function that returns 1 if a pattern is matched. Else it should return 0. Should I avoid the else statement and just return 0 as the last line? It won't affect the logic of the function, but what is the recommended way? Is there any site/book I can follow for such tips?

proc is_bus_net { net } {
    set net_name [perc::name $net -fromTop]
    if { [regexp {(\S+\)[\d+\]$} $net_name match first_name ]} {
        puts "matched $net_name => $first_name"
        return 1
    }
    return 0
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Alan Saldanha
  • 87
  • 2
  • 8
  • The else statement is a tool we have to write code but most of the time it is useless and you should avoid using it. – Vishal Beep Jun 27 '22 at 07:28
  • 1
    You can visit this sites for more info - [this](https://dev.to/darlantc/avoid-using-else-write-a-better-code-12l9#:~:text=Conclusion,you%20should%20avoid%20using%20it.) and [this](https://www.codecademy.com/forum_questions/526868e5548c352551000f60) – Vishal Beep Jun 27 '22 at 07:34
  • Use `else` if it makes sense for your code. Otherwise don't. It's not very important to do so when the first arm of the `if` is a terminal (e.g., ends in `return`). – Donal Fellows Jun 27 '22 at 14:05

1 Answers1

0

Is it necessary to write else part in every if condition?

Else statement is not always needed but it makes the code more readable.