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

Powered by:

SearchCompliance

  • risk map (risk heat map)

    A risk map, also known as a risk heat map, is a data visualization tool for communicating specific risks an organization faces.

  • 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 ...

SearchSecurity

  • FIDO (Fast Identity Online)

    FIDO (Fast ID Online) is a set of technology-agnostic security specifications for strong authentication. FIDO is developed by the...

  • cryptanalysis

    Cryptanalysis is the study of ciphertext, ciphers and cryptosystems with the aim of understanding how they work and finding and ...

  • Trojan horse (computing)

    In computing, a Trojan horse is a program that appears harmless, but is, in fact, malicious.

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

  • cloud SLA (cloud service-level agreement)

    A cloud SLA (cloud service-level agreement) is an agreement between a cloud service provider and a customer that ensures a ...

  • wear leveling

    Wear leveling is a process that is designed to extend the life of solid-state storage devices.

  • storage area network (SAN)

    A storage area network (SAN) is a dedicated high-speed network or subnetwork that interconnects and presents shared pools of ...

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