Wednesday, December 6, 2017

Word of the Day: Yoda conditions (Yoda notation)

Word of the Day WhatIs.com
Daily updates on the latest technology terms |December 6, 2017
Yoda conditions (Yoda notation)
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' ) {
1 if ( 'post' == $post_type ) {
2 // do stuff
2 // do stuff
3 }
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.

Quote of the Day

"How much ancient code from legacy languages do you come across in dealing with enterprise IT? Are you afraid to tinker with it?" - Joel Shore

 

Trending Terms

syntax
code review
Infrastructure as Code
low-code/no-code development platform
BizDevOps
code

 
Learning Center

Infrastructure as code benefits those willing to learn
Infrastructure as code benefits an IT organization in daily management, as well as audit tasks, but it's only achieved with hard work and new skills.

No-code/low-code project management best practices
Taking on no-code/low-code management can be overwhelming. Expert Zach Flower discusses best practices for low-code development platforms.

DevOps testing: Never trust the world outside the enterprise
DevOps testing requires a culture shift that puts an appropriate focus on security. How do security pros address the risks posed by infrastructure as code?

How a tester can contribute to the code review process
You don't need to know how to code to contribute during code review. Expert Matthew Heusser walks you through the code review process.

Low-code platforms move the BizDevOps trend forward
Low-code platforms allow business leaders to build applications alongside developers, bolstering the BizDevOps trend in the year ahead.

Writing for Business

The code __________________________.
a. needs to be rewritten badly
b. badly needs to be rewritten
Answer

 

Stay In Touch
For feedback about any of our definitions or to suggest a new definition, please contact me at: mrouse@techtarget.com

 

Visit the Word of the Day Archives and catch up on what you've missed!

 

FOLLOW US

TwitterRSS
About This E-Newsletter
This e-newsletter is published by the TechTarget network. To unsubscribe from Whatis.com, click here. Please note, this will not affect any other subscriptions you have signed up for.
TechTarget

TechTarget, Whatis, 275 Grove Street, Newton, MA 02466. Contact: webmaster@techtarget.com

Copyright 2016 TechTarget. All rights reserved.

No comments: