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

  • smart contract

    A smart contract, also known as a cryptocontract, is a computer program that directly controls the transfer of digital currencies...

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

  • internal audit (IA)

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

SearchSecurity

  • evil maid attack

    An evil maid attack is a security exploit that targets a computing device that has been shut down and left unattended.  An evil ...

  • Common Body of Knowledge (CBK)

    In security, Common Body of Knowledge (CBK) is a comprehensive framework of all the relevant subjects a security professional ...

  • rootkit

    A rootkit is a program or, more often, a collection of software tools that gives a threat actor remote access to and control over...

SearchHealthIT

  • value-based healthcare

    Value-based healthcare, also known as value-based care, is a payment model that rewards healthcare providers for providing ...

  • health informatics

    Health informatics is the practice of acquiring, studying and managing health data and applying medical concepts in conjunction ...

  • clinical trial

    A clinical trial, also known as a clinical research study, is a protocol to evaluate the effects and efficacy of experimental ...

SearchDisasterRecovery

  • crisis communication

    Crisis communication is a method of corresponding with people and organizations during a disruptive event to provide them with ...

  • Zerto

    Zerto is a storage software vendor that specializes in enterprise-class business continuity and disaster recovery in virtual and ...

  • crisis management plan (CMP)

    A crisis management plan (CMP) is a document that outlines the processes an organization will use to respond to a critical ...

SearchStorage

  • SSD write cycle

    An SSD write cycle is the process of programming data to a NAND flash memory chip in a solid-state storage device.

  • data storage

    Data storage is the collective methods and technologies that capture and retain digital information on electromagnetic, optical ...

  • hard disk

    A hard disk is part of a unit -- often called a disk drive, hard drive or hard disk drive -- that stores and provides relatively ...

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