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. |
| Writing for Business | The code __________________________. a. needs to be rewritten badly b. badly needs to be rewritten Answer | |
No comments:
Post a Comment