Jump to content

Why is this unreachable code?


Recommended Posts

Posted
public enum State {
		LIGHT, COOK, BANK, WAIT;
	}

	public State getState() {

		RS2Object fire = objects.closest("Fire");

		if (!inventory.contains("Tinderbox") // NO TINDERBOX?
				|| inventory.onlyContains("Tinderbox") // ONLY TINDERBOX?
				|| !inventory.contains("Filler")) { // YOU'RE DONE COOKING?
			
			return State.BANK;
		} else if (fire != null) { //	HAHAAHAAAA WE DISCOVVEREEEDDD FIREEEE
			return State.COOK;
		} else {
			return State.LIGHT; // aww we have no fire :( BUT WEEEE SHALL MAKEEE ITT
		}
		return State.WAIT;
	}

 

Posted (edited)
1 hour ago, superuser said:

} else if (fire.exists() && fire != null) {
  		return State.COOK;
} else if (!fire.exists() && fire == null) {
		return State.LIGHT;
} else {
  		return State.WAIT;
}

on top of what elliot already said, you should also check to see if the fire exists

I laughed :boge:

Thanks

Edited by FrostBug
  • Like 1
Posted
8 hours ago, superuser said:

what did you laugh at? did i fuck something up? i posted from my phone but the code looks fine to me

edit: you are very welcome though you fucking legend

Well, calling any method on a null reference will throw a NullPointerException; and exists() is a method.

So the irony of doing something like

!fire.exists() && fire == null

can be appreciated, since it can never pass, and will throw errors whenever there's no fire.

Posted
12 hours ago, Prolax said:

rickrolled_profile.png.0d61af42cf6423bf7

 

So I'm confused what I'm supposed to get out of this.

public State getState() {

		RS2Object fire = objects.closest("Fire");

		if (!inventory.contains("Tinderbox") // NO TINDERBOX?
				|| inventory.onlyContains("Tinderbox") // ONLY TINDERBOX?
				|| !inventory.contains("Filler")) { // YOU'RE DONE COOKING?
			
			return State.BANK;
			
		} else if (fire.exists() && fire != null) { //	HAHAAHAAAA WE DISCOVVEREEEDDD FIREEEE
			return State.COOK;
		} else if (!fire.exists() && fire == null) {
			return State.LIGHT;
		} else {
			return State.WAIT;
		}
		
	}

Did what you said, and it says Dead Code. Also I've never used exists() before.

What's the difference in exists() and != null ?

Posted (edited)
On 8/30/2017 at 9:06 PM, superuser said:

} else if (fire.exists() && fire != null) {
  		return State.COOK;
} else if (!fire.exists() && fire == null) {
		return State.LIGHT;
} else {
  		return State.WAIT;
}

on top of what elliot already said, you should also check to see if the fire exists

He doesnt need the fire.exists() the else if (fire!=null) should work fine :)

As far as i am aware, the fire can never be null if it exists :think:

Edited by Ollie7xP
Posted
1 hour ago, dreameo said:

Read the API and if it doesn't help you out, just have a separate file were you can test behavior of certain methods.  

Um, of course I read the api. 

 

exists

boolean exists()

Checks whether this Entity still exists or not.

Returns:

Whether this Entity still exists or not.

 

Still don't see how's that different from != null . Seems exactly the same to me?

 

Plus that isn't really the main question. Main question is why is my code unreachable and how do I fix it? 

Because the response earlier is giving me a Dead Code.

Posted
15 minutes ago, FrostBug said:

Stop being silly; all of you.

You should under no circumstance use exists() in this scenario.. It's an expensive method, and the fire will always exist if it isn't null since he just retrieved the instance.

You seem to understand the situation, does this look good to you?

I'm trying to use enums in my codes now to make them look cleaner.

public State getState() {

		RS2Object fire = objects.closest("Fire");

		if (!inventory.contains("Tinderbox") // NO TINDERBOX?
				|| inventory.onlyContains("Tinderbox") // ONLY TINDERBOX?
				|| !inventory.contains("Filler")) { // YOU'RE DONE COOKING?
			
			return State.BANK;
			
		} else if (myPlayer().isAnimating()) { // YOU'RE DOING STUFF???
			return State.WAIT;
		}
		else if (fire != null) { // OH LOOKIEE HERE, A FIREEE?
			return State.COOK;
		} else {
			return State.LIGHT; // NO FIREE?? Simple. JUST MAKE ONE!
		} 
		
	}

 

Just now, liverare said:

Your question has been answered.

But additionally, make fire a field variable. Otherwise, you're going to have to re-find the fire when you come to cooking the fire.

 

Sorry I'm dumb. The only answer I saw was the response from superuser, and it was giving me a dead code. The other thing was Frostbug explaining something but I didn't understand it.

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...