DEV Community

Cover image for Steal Like a Pro: Learn Java by Using Code Examples
John Selawsky
John Selawsky

Posted on

Steal Like a Pro: Learn Java by Using Code Examples

Originally, this article was written and posted on SimpleProgrammer.

Hello, everyone! I am Johnny, senior Java developer and … I am a code thief—indeed, an old offender in this area. But guess what’s the best part: It’s not a crime!

Every software developer is a bit of a code thief, as stealing code is a teaching tool and a time-saver.

In this post I am going to explain what I mean by code “stealing.” In particular, I’ll show you how it helps improve both your Java learning and your code.

Whether you’re a Java beginner or a more advanced programmer, there’s definitely something for you here. After all, even the most experienced of us can always learn something new.

Copying Is Not Always Illegal

Do you know that almost every master painter having their own style started by copying others?

Almost every master whose paintings you recognize at first glance because of their originality began in museums by sketching and copying the popular works of their day. Google, for example, “Dali’s Early Works” … I presume you are unlikely to recognize the future surrealist in them.

Why did painters do this? To learn from professionals, of course. Copying helps to improve your skills while you gradually develop your own style.

The original style is not so important for the programmer, for sure. Moreover, it can be even harmful. Good code is not always original. Sometimes, all it has to do is meet the following conditions:

  • It should solve the task.
  • It should be easy to read and understand.
  • It should be easy to maintain.
  • It should be concise, that is, avoid unnecessary constructions.

“The code should solve the task” is more or less clear (although there are nuances as well: to understand whether the problem is solved correctly is not always easy). But how do you know if your code is understandable, standard, and easy to maintain—especially if you are just starting to learn?

The solution: “Steal” the code from others!

Using someone else’s code examples is the same as learning from masters and more experienced developers.

So, what does this mean?

How does one “steal” code so that it is legal and useful for personal study?

Use Learning Resources and Ideas Made by Professionals

We are lucky enough to live with the internet. You can find useful and practical courses and learning projects there that will help you grow as a programmer.

Some of them even have step-by-step instructions and code templates you can learn from.

When developing a small project, sometimes it’s best to start with professional recommendations. This way, part of the code has already been written, and you can use their prewritten patterns to help build your own code from that.

The first step to coding like the pros is to examine the work of professional programmers.

Game Projects

Mobile gaming is a hugely popular activity, and games account for a large percentage of available apps. It’s very likely that at one point or another, coding games will be something you’ll at least want to try.

A great idea for gaming projects is to create parts of the game and write code based on the code of classic games.

John Carmack, author of influential gaming projects such as Doom, Wolfenstein 3D, and Quake, once said on his Twitter that every beginner should try to recreate classic retro games as coding exercises. The game programming guru called this activity very useful for future developers.
1
using code examples
However, creating even the simplest game can be tough for a complete beginner. Typically, a rookie gets lost and does not know where to start. But again—how lucky we are—the internet with its resources is here to help!

The CodeGym is a portal with well-designed practical Java Core that has a free games section. It proposes, step-by-step, to recreate one of these classic video games—Minesweeper, Snake, Space Invaders, Racers, and others.

The authors of the website created a special light game engine that can be very beneficial to learn. You code the part of the game according to the tutorial, and then the validator checks it. If everything works fine, you go to the next step.

Games are plentifully provided with instructions and code examples. After you complete the game, you can modify it as you like.
2
On the zetcode website you can find a detailed tutorial for Java 2D games, with code examples for classic video games (Sokoban, Snake, PacMan, and others), as well as a useful tutorial for developing your own games.

The only drawback is that the rather old Swing library is used here. However, you can still learn from it. The main thing is to understand the concept. Learn the code, use part of it, and then try to recreate the same games by applying more modern gaming technology to display.

There’s an exciting project called Codingame, where you can participate in the development of a relatively large-scale space game. Part of the code has already been written. Learn it carefully, and then proceed to write your own part according to the assignment.

Thanks to this particular program, it will be much easier for you to create your own games later on. The only drawback is that this project is designed for those who are already relatively confident in programming.

At least, you can rewrite the code from Java books such as Head First Java, Core Java, or any other you use in your learning.

Ask Questions on Forums, and Read the Answers of Others

If something is wrong with your task, or you don’t know what to do to solve a problem you’re dealing with, the first step you should take is to go to Google.

Google will show you the best forums to read code, such as Stack Overflow, some Reddit threads, and JavaRanch.

Let’s model this situation. You have a problem, you’ve gone to Google, and you’ve found a person who has a similar problem.
3
Go to the comments where others have posted a solution and … steal the code! However, a good idea is to try to realize what was wrong and why your internet colleague’s code is right.
4
If you can’t find the answer to your question, then … drumroll … create a new question!

Don’t be afraid of this. To know how to ask in programming means you’re halfway to solving the problem. You may say, it is a dangerous path to look for the solution on the internet every time you’re stuck with a problem.

Well, it can be if you don’t try hard enough. Much more often, rookie programmers stick with their complex tasks for too long.

The result is they lose their motivation.

So be careful.

Try, but not for too long. It’s better to ask on the internet and work on the “stolen” piece of code than it is to insist on solving it yourself and chasing your own tail in vain.

Another great tip is to read the forums, and try to help other people to solve their problems. This exercise not only benefits you for your future job but could possibly get your foot in the door as an expert in your field, whether you feel you are or not.

Learn From Libraries and Existing Classes

When I teach my students to work with IntelliJ IDEA, I ask them to remember the Ctrl+LMB (left mouse button) combination from the very beginning.

If you put the mouse cursor over the class name in your code and use this combination, you will appear in this class code window.

You may see the original code written by one of the Java creators or the Library author. Learn it and steal the style of it!

For example, when my students learn Java Collections, I ask them to create their own version of some Collection with certain changes. The condition is that you can’t use Google, but you can read Java libraries code as much as you like.

Java has a LinkedList class. This is essentially a doubly linked list. I ask my students to write the Singly Linked List class by “stealing” code from a two-linked LinkedList.

And of course, I invite everyone to read the Javadoc carefully. Everything is very well described there.

By the way, using libraries is also in a sense a theft of code: You are not doing what you would have to do if they were not written.

Use the Code From Existing Open Source Projects

One of my students liked Minecraft very much. When he started programming, he began by decompiling Minecraft, getting Java code, and changing it for his own usage.

First, he modified this decompiled code, and later he wrote his own Minecraft clone for Android. Now, he creates Android Games by himself.

We have a Github with millions of different projects on it. If you haven’t realized, Github is one of the most exciting places on the whole internet. If you want to find an interesting project, click Explore and look for popular repositories. You can choose it by type (for example Productivity tools, 3D modelling), by collection, or just by name.
5
For example: I want to create my version of Space Invaders but I am a rookie. By using the explore tab, I can find a Java Space Invaders clone, download it, and try to read the code. Do the same!

What If Everything I Code Is Stealing From the Others?

I am sure the vast majority of modern programmers ask themselves this question again and again. I am talking not only about students or trainees; even professionals have this problem.

The fact is that programming becomes more and more high-level. There are fewer lines of code written by you than written by others that you use. So you may get Impostor Syndrome vibes, an extremely common condition among programmers, where you constantly doubt your own achievements and skills.

To combat this, **write the code from scratch.

Sometimes, I give my students an “hour of code on a piece of paper.” I recommend you do the same once or twice a week. Choose a task that you have solved not so long ago, or if you are brave enough, you can have a new one and solve it in a Notebook app without syntax highlighting and without Google.

You can use even a piece of paper!

This task is not about the syntax. A much more important marker is whether you can create a solution algorithm or not.

Do not be lazy, and take this exercise as a vaccine for the Impostor Syndrome associated with heavy code theft.

Steal Like a Pro

I believe stealing code is a teaching tool and a time-saver. The examples and methods I showed you can greatly benefit your learning.

Stealing code is not a crime, but a method to teach yourself how to code. Every student—even every professional—of every field copies something that has been created by other people. Reusing the code is a natural process for every software developer. They use libraries, components, and open source projects extensively.

So don’t try to reinvent the wheel. Rather, use this wheel to complete your own car model. Keep calm and steal the code with benefit. Good luck with your learning!

Top comments (0)