There are a lot of statements where we can use any one of, conditional operator or if
..else
.
So in those statements, which one is the best from a performance point of view?
There are a lot of statements where we can use any one of, conditional operator or if
..else
.
So in those statements, which one is the best from a performance point of view?
Don't worry about performance. Use which ever is more clear to read.
Never ever think about performance. Premature optimizations are the spawn of satan and the root of all evil in hell and earth.
The only time you should worry about performance is if your users/customers complain about it. And then only fix the feature that they complain about.
The thing with developing is to make the code as readable as possible. Doing that makes it a whole lot easier to fix bugs and performance issues compared with code that you've tried to optimizie for perfomance.
As for perfomance, it's usually not regular code such as if
clauses that hurt performance. It's the network, database operations, alghoritms, parsing strings etc.
Use "?" for assigning a default value to an empty null or otherwise unusable variable.
Use if .. else .. for everything else.
"Programs are meant to be read by humans and only incidentally for computers to execute" - Donald Knuth
So, always if...else. Unless you find a place where ? helps understanding your code.