Most people who get started with Spigot development somehow think it’s a good idea to make everything as short as possible (at least in terms of naming variables). This is considered bad practice for a good reason.
What’s the problem?
Let’s imagine this is your code:
@EventHandler public void onPlaceBlock(BlockPlaceEvent e) { Player p = e.getPlayer(); World w = e.getBlock().getWorld(); if(!w.equals(p.getWorld())) { e.setCancelled(true); p.sendMessage("How did you manage to place a block in another world?! o0"); } }
In this example, it is obvious in line 4 that “w” is the world, and “p” is the player, and “e” is the event, but imagine your code is a bit more complex. In fact, it’s so long that you cannot instantly see where a variable was defined. Now you encounter this line:
if(e == null && p == null) { return; }
But – what is “e”? It could, according to your naming scheme, be either…
- An event
- An Entity
- An Exception
- An Enchantment
- An EnderChest
- An EnderMite
- An Entry<?,?> of a Map
- A PotionEffect, FireworkEffect, EntityEffect, …
- …
Meanwhile, “p” could be
- A Player
- An OfflinePlayer
- A Piglin
- A Permission
- A Potion
- A Painting
- Your Plugin instance
- A Projectile
- A Particle
- …
You might now say but I know that p is always a player! Yeah, maybe you know that NOW. What about the event/entity/exception thing, though? And since someone probably sent this blog post to you, at least other people were confused about your variable names, and that makes it harder for them to help you.
But I’m lazy and don’t want to type “player” everytime!
That’s why every IDE has auto-completion builtin.
I also used to be lazy and used confusing names and was resistant to changing them. Now, when I see my old code, I understand how stupid it was. IDEs however can easily help you. For example, in IntelliJ you can use Shift+F6 to automatically replace all occurrences of “p” to “player”. You should really consider doing this for every of your single-letter variables.
TL;DR:
Variable names like “p”, “e” etc. are bad. Exception: using x, y, z for coordinates, or “i” in a counting loop etc.
Join my Discord Server for feedback or support. Just check out the channel #programming-help
🙂