In case you don’t know what the ternary operator is and want a thorough explanation you can read this article.
If you want a quick refresher, the ternary operator (?) is a very compact way to do an assignment that depends on an if-else statement. For example
string result = ""; if (DoSomething()){ result = "Do Something worked"; } else { result = "Do Something didn't work"; }
Can be summarized using the ternary operator to
string result = DoSomething() ? "Do Something worked" : "Do Something didn't work";
The first time I saw this in my early programming days I honestly didn’t like it. I would even admit that I thought other people used it in their code so they could feel “cool”.
As I’ve gotten more experienced in programming (and lazy) it has definitely won a place in my heart. You don’t have to type as much which makes code smaller and a lot easier to read.
What are your thoughts? Do you use this operator or would you rather just do a if-else?