Correct plugin coding!

Oke, this thread we will talk about how to code your plugins on the correct way. You can request things to be added to the #1 post. Feel free to be critical, as their can be things I understand wrong as well.

  1. **Don't use "new" in events**
  2. Events normally give you all the objects you need to handle them. If you need to make new objects in a event your probably doing it wrong. Lets say you have a "PlayerMoveEvent" and you make a new instance on every move. That would be the same of asking an oven to bake a cake every second.
  3. **Avoid singletons!!!.**
  4. At my view their only may be 1 singleton and that for your main class that holds all your plugin objects. But only do this if you want other plugins to use your objects. But don't let your other classes use your getInstance(). Just give your main class in the constructor on initialization. So this is what we want: ```java class myplugin { Database database; public void setup(){ this.database = new Database(this); } } ```
  5. **List item Don't write your plugin in 1 class!**
  6. You will regret it when you want to update/change your plugin.

Sorry, but this is useless. If you want a coding standard, follow Google’s coding standards which is what the Sponge project as a whole is using and which is required for pull requests.

1 Like

Hmm, Its not really coding standards. Its more tips and tricks to get a plugin that doesn’t lag/break servers. I can’t find anything about events or singletons in it.

Well, if it’s not coding standards, then I agree that this post is useless, because you can’t just generalise everything.
For example, if clicking a sign is supposed to open a GUI, how is one supposed to implement that without constructing a new Packet and sending it to the client?
Events as an abstract concept are complex, which means every problem has to be handled on its own. You can always make mistakes, fatal mistakes, but those should become noticed at least when the developer is testing his/her own plugin.

1 Like

Well it all depends I think, I have seen bukkit plugins that dit new Location on move. Causing the server to get a TPS of 5 when 7 people where on.

In case of a that click event, that is heavily filtered. The new keyword wouldn’t give a problem indeed.

Ya @DarkArcana, can you close and hide this epic fail?

You make good point on the “new” thing. I will see if I can reduce the occurences of new in my plugin move event. Right now I think it creates 2 Vectors, 1 CuboidRegion and 1 Mask or 2 locations.

Do note that if you create it in the event handler and that object never leaves the method, then the JVM can optimize it by allocating it on the stack instead of on the heap. This means that a lot of the speed and optimization concerns are not relevant as long as those objects stay within the method. It is still good practice to reduce allocation and use object pools where applicable, but for a lot of temp objects you don’t need to worry a whole lot.

The technique the JVM uses is called “escape analysis” and has been on by default since java 6u23.

1 Like

As others have said you cannot generalize these things so easily. Instead I would recommend to read Joshua Bloch’s Effective Java which includes 87 best-practice solutions for typical programming challenges (e.g. singletons) and has become one of the standard books for that matter. If you don’t know Bloch, he wrote - among other things - the Java Collection framework and works for Google now.

@ZachBora Premature optimization is the root of all evil. So unless you know (by measuring differences) that the current solution has a negative performance impact that you care about or the code seems stupidly complicated (e.g. creating a new StringBuilder in a loop when you could simply reuse the previous one) or not as clean as you could write it, you should not waste time with optimisation.

People complained about it before, using Timings, telling me my playermove was “omg using so much time”.

Well than was this thread not such a big fail xD.

Yeah, I understand it now.

I’ve been under the impression that the singleton pattern, is an anti-pattern. All of the things you have pointed out should be common knowledge.

I know @snowc0de, just let this thread die ;-;. It was the result of a bad morning with very less sleep at night.

1 Like