Browse Definitions:
Definition

Yoda conditions (Yoda notation)

Contributor(s): Ivy Wigmore

Yoda conditions, also known as Yoda notation, is a way of formatting code that reverses the standard order of an equality conditional statement. The formatting style is named for the Star Wars character whose syntax typically involves an object-subject-verb order instead of the standard English subject-verb-object order: "Patience you must have" for example, rather than "You must have patience."

In programming, Yoda conditions involve swapping the positions of the variable with that of the constant, function or literal in an equality conditional expression.

On Know The Code, Tonya Mork offers the following examples:

Typical conditional structure      Yoda conditions
1   if ( $post_type == 'post' ) {
2   // do stuff
3   }
1   if ( 'post' == $post_type ) {
2   // do stuff
3   }

That format forces an error in the case of certain typos. If, for example, the programmer types an assignment operator (=) in the statement when they intend to type the equality operator (==), Yoda format will force a syntax error. The error prevents the code from running and, because of the error, possibly causing unintended behavior.

According to Tonya Mark, preventing unintended behaviors in that situation is the only advantage to using Yoda conditions and the only reason to use it. The main disadvantage to Yoga conditions is the fact that they make the code more cumbersome to read and make code review more difficult than necessary.

This was last updated in November 2017

Continue Reading About Yoda conditions (Yoda notation)

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

File Extensions and File Formats

SearchCompliance

  • internal audit (IA)

    An internal audit (IA) is an organizational initiative to monitor and analyze its own business operations in order to determine ...

  • pure risk (absolute risk)

    Pure risk, also called absolute risk, is a category of threat that is beyond human control and has only one possible outcome if ...

  • risk assessment

    Risk assessment is the identification of hazards that could negatively impact an organization's ability to conduct business.

SearchSecurity

  • biometrics

    Biometrics is the measurement and statistical analysis of people's unique physical and behavioral characteristics.

  • principle of least privilege (POLP)

    The principle of least privilege (POLP), an important concept in computer security, is the practice of limiting access rights for...

  • identity management (ID management)

    Identity management (ID management) is the organizational process for identifying, authenticating and authorizing individuals or ...

SearchHealthIT

SearchDisasterRecovery

  • business continuity and disaster recovery (BCDR)

    Business continuity and disaster recovery (BCDR) are closely related practices that describe an organization's preparation for ...

  • business continuity plan (BCP)

    A business continuity plan (BCP) is a document that consists of the critical information an organization needs to continue ...

  • call tree

    A call tree -- sometimes referred to as a phone tree -- is a telecommunications chain for notifying specific individuals of an ...

SearchStorage

SearchSolidStateStorage

  • hybrid hard disk drive (HDD)

    A hybrid hard disk drive is an electromechanical spinning hard disk that contains some amount of NAND Flash memory.

Close