How to stay motivated while learning Java?
📣 Announcement
If you want to get better at writing code, I have a Clean Code workshop coming up.
Reply to this email or leave a comment if you are interested and I will send the details.
Java is one of the harder languages to master. If you are learning on your own, you will feel the resistance very early. If you have learned programming by creating a flowchart and then translating it into, Java would seem to work in a bottom-up fashion.
Object-oriented programming follows the bottom-up paradigm.
So you are not wrong to feel like this works in the opposite way you are thinking.
I had the same issue when I switched from C to C++ and Java. It took a while to get used to.
Here are 5 steps you can use to stay motivated while learning Java.
Write the code as if you write in a procedural language
The switch that you have to make in your thinking is hard. So to make it simpler at the start, begin writing the code using the method you know.
You just start with a simple Java Class with the famous main method.
Pour all your procedural language code into this method.
What this does is, it will get you started.
You will also get the satisfaction of getting the code working for you.
Write unit test cases
Once the code is written, it is time to write unit test cases.
If you are looking to become a seasoned programmer in Java, you have to learn to write unit tests.
It always feels like a pain to write unit test cases but you get better at it as you write more.
Remember, writing the procedural code was not the end goal.
You want to become a better Java programmer.
Writing tests will help you to validate when you do the third step.
Break all the logic into simpler methods
Since you have all the tests and logic completed, it is time to refactor the code.
Identify the snippets of code and create methods.
You can use Intellij’s extract method for this.
Re-run your tests to ensure no logic gets broken in this approach.
Identify and group methods and variables into classes
The last step would have given you a clearer picture of the relationships between variables and methods.
You will be able to see what variables and methods can be encapsulated into a class while keeping the remaining code in the main class.
As you create classes, you will also have to refactor your tests as some of the tests will move to the corresponding test class.
Review your code
You have done the major part of your code and now you will feel your code following many of the object-oriented principles.
But your job is not done yet.
Review once more.
You put on the hat of the most dogmatic code reviewer that ever existed in the world.
Find the following things:
There could be pieces of code that can be moved into a separate class.
There could be methods still doing multiple things.
Improve the name of variables now that you can see the exact intention of the variable.
Ask yourself the reason for some piece of code that seems complicated.
Can you simplify it?
Once you are satisfied with what you have done, seek a code review from your colleague/friend/peer.
Taking it home
What this process does is, it will move you out of the frozen state when you try to apply all the concepts of OOPS principles.
Action breeds clarity.
You will remain motivated as you are writing code and seeing it work.
You are also developing, writing tests, and refactoring. All this is the real essence of all software development.
Doing this many times, you will soon start skipping many of the steps and will be able to write object-oriented code right out of the gate.
But you will still follow the three steps in your developer career.
Write code
Write tests
Refactor
📣 Shoutouts of the week
You and your manager: 3 ways to work better together and get what you want: Simple and effective guide to get what you want at work.
It is the purpose that brings meaning to your life: Something to think about on the weekend
Boy scout maintenance, ICAP framework, and cognitive load 💡: Some golden advice on becoming a better programmer and a better teammate.