Friday, December 29, 2017

Java: Working with Objects

When programming, it is important to use objects to help reduce the amount of redundant code and make the program faster. It is also useful as reducing redundancies will help organizing the code to make it easier to read and understand, especially when dealing with many different instances, such as multiple users. "The question is, when writing a computer program to deal with accounts, how do I separate my balance variable from your balance variable?" (Burd 162).

On the popular chat service, Discord, I created a program that can have different commands the users  can use to make the program do different things such as showing data or statistics about a user. To make the software simpler, I made a single type of object called Command. Then all commands I make can be based on that generic Command object. This means that if I wanted to substitute one command for another, I can easily do so without changing much of the code if I base everything off the generic object rather than specific commands.









Here's a simple Command object with 3 functions. There is the onCommand function, that I can run with some variables to make the command run it's unique code that varies between commands. There is also the getDesc function that just returns a description about what the command does, and finally the isHidden function that returns whether or not to hide the command from the help menu.
From the example in the book with bank accounts, Burd says that "the Account class... defines what it means to be an Account. In particular, [the code] tells you that each of the Account class's instances has three variables: name, address and balance" (Burd 163). Anything that is an account must have the same three variables no matter what.

Here's a sample command I wrote called ping. When it is run, it will display the program's current ping in the chat. The ping is the amount of latency or time between when it contacts the Discord servers and when the Discord servers can send information back to the program, so the lower the ping, the faster and more responsive the program can be. The sample command "extends", or inherits the basic structure of the generic Command object. Then it has the onCommand function which sends a message back to the user who executed the command with the ping. The getDesc function gives t he description, "Pong!", as the return message from a ping is known as a pong message. And finally, the isHidden function will return false, as the command should not be hidden from the help menu.
Finally, putting this all together, the code gets another object called the CommandManager, which keeps a list of all commands, and it searches for a command based on what the user inputs. If no command is found, it stops there. However, if a command is found, it can run the onCommand function with the proper variables because any code for a command will always have an onCommand function as the generic Command object defines the structure of any command.

What do you think of the simplicity of having a single "parent" object that all "children" objects must inherit traits from? Does it make the code simpler as all child objects are alike, or is it more complex for child objects to be forced to conform to the parent's traits?

Works Cited:
Burd, Barry. Java for Dummies. 7th ed., John Wiley & Sons, 2017.

2 comments:

  1. Hi Josh! I just finished reading your post. It was interesting to learn how objects or commands can help improve the quality of the code. I am just starting to learn more about objects, and I haven't fully grasped their concept, but I believe they can be extremely useful in making a program efficient and neat. What are some of the techniques you employ when implementing certain functions or objects?

    ReplyDelete
    Replies
    1. Hi William! I tend to make a lot of objects to help store information together. For example, if I want to store information about a specific user, I have a user object with different variables such as username, whether or not they're signed in, password, etc. Then I can keep a list of all user objects and have the data all in one place.

      Delete