Over a million developers have joined DZone.

Are You a Yoda Programmer?

DZone 's Guide to

Are You a Yoda Programmer?

· Java Zone ·
Free Resource

When reading the code of my clients I often see many funny constructs. They are not wrong, just funny. Recently I spotted a new trend in some code I read. When I read it aloud to myself, it sounded like Yoda was speaking :-)

For instance

if(0 < myObject.theValue() ) {


When I try to understand foreign code, I usually kind of read it aloud to myself. Maybe not literally, but at least inside my head. Code like the above reads like

 "if 0 is smaller than or equal to the myObject.theValue(), then do XYX".

If you were to describe the business rule used inside this if-statement, you would probably have written it the other way around:

"If the myObject.theValue() is larger than 0, then do XYZ".

This way, the "myObject.theValue()"  is emphasized as the base of the rule, rather than the value 0, as is more the case in the first version.

The code version to make the code read more like version 2, would be

if(myObject.theValue() > 0) {


Or better yet, wrap the comparison inside a single-line method, explaining the business rule better:

if(hasValue(myObject)  { ... }


further explanation of business rule.

private boolean hasValue() {
return myObject.theValue() > 0;


Such single-line methods are inlined at runtime by the JVM anyways, so there is no performance overhead associated with it.

In my experience we read our code many, many more times than we write it. Thus, it makes sense in my opinion to invest a little energy in making it easy to read and understand.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}