Jump to content

Why is this unreachable code?


kingbutton

Recommended Posts

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;
	}

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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