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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}