Thursday, November 30, 2017

Java: Classes and Code Organization

I have been programming in Java for over 6 years now, but because I was never formally taught the language, there are likely some things that I do that aren't part of the official standards, or things that may cause the code to be inefficient or hard to work with. In Java for dummies by Barry Burd, the basics of Java are covered as well as more complex topics and how to keep the code organized and effective without becoming a nuisance to work on.

Small sample program that is
easy to understand and work with.
Output of above program.
When learning programming, people start with very basic programs that don't do much. In these small programs, inefficiencies and confusing code don't matter very much, but as the program grows and gets increasingly complex, these small problems add up making future changes more difficult and causing the program to be slow. "For years, companies were buying prewritten code, only to discover that the code didn't do what they wanted it to do... Their programmers dug deep into the program files, changed variable names, moved subprograms around, reworked formulas and generally made the code worse" (Burd 198). This emphasizes the importance of keeping the code neat and easy to work with from the start because if the code is difficult to understand, even the programmer will eventually be unable to figure out what's going on.

More complex part of a program that
determines what code to run next.
As shown in the picture on the right, code can quickly become more complex and difficult to understand.

To organize this code, Java has classes which are used to put related code all in one file, while keeping less relevant code in another file. This helps with organization, similarly to how a book is divided into chapters.
Code all put into a single class
without being properly organized.
 



On the left is part of the original code when all of it is in a single class. As you can see, it's long and confusing with so much there that you can't easily see all of it at once.
Simplified code that is properly organized
and distributed throughout multiple classes.

However, below that image is a simplified version where I have put the code responsible for loading add-ins into a separate class. It is now easy to see that the single
Code for "loadJars()" method shown above.
function, known as a method in Java, "loadJars()" will load the add-ins from the other class.

Below that is a third image, which shows the code that is run when the "loadJars()" method is used. This is much easier to understand and won't result in as much confusion down the road. As you can see, even the "loadJars()" method contains another method, "loadJar(f, false)" that loads each specific file.

By putting methods within other methods, the code is even neater and simpler to understand! This is known as object-oriented programming, where "all the functionality that's associated with an [object] is collected inside the code for the [Object] class" (Burd 170).






What do you think of the organization used in modern, object-oriented programming languages? Does it make the code look neater and less confusing, or is the condensed form easier to follow as the code doesn't jump around?

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

4 comments:

  1. Hi Josh! You really emphasized the importance of keeping code organized and accessible. As a new programmer with no prior experience, I can use this information to help me edit and write code efficiently while keeping it neat and tidy! I have little experience in the coding field, but I think that the organization of the code can sometimes seem confusing when it is condensed, since I prefer seeing each individual part. How do you personally organize your code? Is there a specific way you do it or is it situational?

    ReplyDelete
    Replies
    1. Hi William! I like to make sure that the code is all in separate places based on what it is for. For example, all code that keeps track of files is in one place, while the code that keeps track of the users will go in another place. Then if there is an issue with the users, I can narrow down the cause of the problem faster!

      Delete
  2. Hi josh! I have always been interested in programming and this post really helped me! I don't have any experience in stuff like this but i have always wanted to learn about it. The way you explained classes and codes made it really easy to understand and i found it fascinating.

    ReplyDelete
    Replies
    1. Hi Kasyn! Programming is very easy to begin learning! There are lots of great tutorials online and in the libraries that make it a fun and easy thing to learn, not to mention very useful! Good luck!

      Delete