Home

Pragmatically Programming

Programming and life are two very different things, right? When you do programming, it's either yes it works or no it doesn't. If it doesn't, you go and fix it. You program something, and there is always one way to do it.

As it turns out, this is not actually the case. Programming and life are actually very similar. When you do programming, there might be two ways to do something. And, when having a team of programmers, it is even more important that you code things CORRECTLY. Sometimes, you might get the end result you wished for, but your code is incredibly messy that anyone trying to edit it would be completely lost in it all, and even you yourself might find it difficult to fix problems. One good example of correct programming is going by the DRY principle. DRY stands for Don't Repeat Yourself. While this may seem like a hard principle to get wrong, you may be finding yourself copy-and-pasting code or writing similar methods. The opposite of DRY code is WET code, which stands for We Enjoy Typing. Don't write WET code. Whenever possible, write methods and use variables.

For example, when writing a Minecraft plugin, you would probably want the text "[CoolPlugin]" (or whatever the name of your plugin is) before each message it sends to the server, like "[CoolPlugin] ScarabCoder left the game". You could make it do something like:

server.sendMessage("[CoolPlugin] Player left the game")

However, you will find you are repeating yourself if you write this way. For every server message you want to send, you will be writing "[CoolPlugin] ". Sure, it might do what you want, but it'd be really, really messy. Imagine the horror you would have to go through when you wanted to change your plugin name from "CoolPlugin" to "AwesomePlugin". Search-and-replace? You would probably still miss something, or probably replace the wrong text. Instead, use the obvious answer; variables. That way, if you wanted to change the plugin name, you could just make a global variable (a variable that is accessible from any part of the program. Languages like Java use this) that would equal to "[CoolPlugin]":

pName = "[CoolPlugin] "
server.sendMessage(pName + "Player left the server")
server.sendMessage(pName + "Player joined the server")

See how much simpler that is? But wait, you are still repeating yourself! You are writing "pName + " before each server message. It's time to create a method. Methods allow you to do things like "sendServerMessage(message)". You can accept inputs, and it is really easy. Not only that, but you could also do something like "logAlert(message)" or "logError(message)", which would send a server message in different colors (red for error, orange for alert, etc). Example:

method logError(message){
  server.sendMessage(color.red + pName + message)
}
logError("server crashed!")

Woooaah! You just cut down the amount of characters you are using by a ton, and made it so much easier to configure your plugin. If, for example, you ever wanted to add another prefix to your errors, you can add that simply in the method.

 

This also means you shouldn't be writing "hacky" code anymore. Hacky code is code that you are using shortcuts or code that might be a bit sketchy and may break with newer versions. You shouldn't be writing this kind of code, and make sure you keep it far away. The temptation to take small shortcuts like this without even checking for another way gets strong, believe me.