Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6849455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:59:35+00:00 2026-05-27T00:59:35+00:00

After reading To ternary or not to ternary? and Is this a reasonable use

  • 0

After reading To ternary or not to ternary? and Is this a reasonable use of the ternary operator?, I gathered that simple uses of the ternary operator are generally accepted, because they do not hurt readability. I also gathered that having one side of the ternary block return null when you don’t want it to do something is a complete waste.. However, I ran across this case while refactoring my site that made me wrinkle my nose:

if ($success) {
    $database->commit();
} else {
    $database->rollback();
}

I refactored this down to

$success ? $database->commit() : $database->rollback();

And I was pretty satisfied with it.. but something inside me made me come here for input. Exception catching aside, would you consider this an okay use case? Am I wondering if this is an okay use because I have never done this before, or because it really is bad practice? This doesn’t seem difficult to me, but would this seem difficult to understand for anyone else? Does it depend on the language.. as in, would this be more/less wrong in C, C++, or Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:59:36+00:00Added an answer on May 27, 2026 at 12:59 am

    I would be more inclined to use it in case the two actions are mutually-exclusive and/or opposite (yet related to each other), for example:

    $success ? go_up() : go_down();
    

    For two unrelated actions I would be less inclined to use it, the reason being that there is a higher probability for one of the branches to need expanding in the future. If that’s the case, you will again need to rewrite it as an if-else statement. Imagine that you have:

    $success ? do_abc() : do_xyz();
    

    If at some point you decide that the first branch needs to do_def() as well, you’ll need to rewrite the whole thing to an if-else statement again.

    The more frequent usage of the ternary operator, however, is:

    $var = $success ? UP : DOWN;
    

    This way you are evaluating it as an expression, not as a statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question , my first reaction was that the user is not
After reading this article I made a point that int () yields 0 because
After reading many times this question and its accepted answer How to execute a
After reading some posts like this one: Choose File Dialog It appears that Android
After reading this forum I am not sure which method is best to extract
After reading this post (recommended reading) about not using HTML5Shiv direct from source like
After reading this essay OOP vs ADT , I have not quite well understood
After reading answer to this question: Make "make" default to "make -j 8" I
After reading MSDN-XAML Namespaces and MSDN-Understanding XAML Namespaces , I still do not understand
After reading the Bash man pages and with respect to this post , I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.