Jump to content

inventory vs getInventory()


FyredUp

Recommended Posts

I've seen in multiple places instances of code that appears to perform identically given the objects are accessed differently. Off the top of my head there are: inventory and getInventory(), npcs and getNpcs(), objects and getObjects().

I'm just curious what the functional difference is between these two cases of accessing something? What should I prioritize using and why?

Link to comment
Share on other sites

After looking into it a bit, given the scripts know all the information about the player, I don't see any reason why getX() is any better than X. In the MethodProvider class, the variables are public and the getters simply return those variables. I would understand using a getting if the class did not extend MethodProvider, but the scripts do extend it. I would argue the *only* reason to use getters is in case X is no longer in the same space, but I don't see that changing.

Link to comment
Share on other sites

There are more resons.

 If you work in the industry you find out people are id10ts and you should write code that holds peoples hands. Getter and setter play an  important role by giving people access to data in a class. All java best  Practices say to keep data  private and to make getters and setters when  Appropriate.  When you make it so people can do what ever they want with data it could break your Internal code.  

You could do npcs=null and that would break thing  Internally.

Link to comment
Share on other sites

Always use getters when its available. Getters and Setters can be doing alot more under the hood than just returning a variable or just setting it to a different value. Take a look at this thread.

In it, the settings variable was not returning the correct value of the run energy, but when he switched it to use the getter it worked.

 

Link to comment
Share on other sites

The getter methods return values contained within a private static field in the MethodProvider class. Due to it being a static field it belongs to the class, rather than an instance of the class.

If you were to call .inventory on an instance of a MethodProvider, you would not get the properties from the static field within the MethodProvider class as you would with the getter.

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...