CodeGym/Java Blog/Random/10 ways to improve your code, proven through personal exp...
Roman B.
Level 35

10 ways to improve your code, proven through personal experience

Published in the Random group
Hello everyone in the CodeGym community! 10 ways to improve your code, proven through personal experience - 1Today we're going to talk about code quality. Yes, dear friends. Nobody's perfect. Everyone realizes at some point that the code could be better... But what to do in this situation? At a minimum, start researching this issue. But you're already here, which means the topic must interest you, so let's go. Today we'll describe ways you can make your code better and cleaner. So you won't be ashamed of your current code in the future! :) All these methods will help a programmer to become a good programmer.10 ways to improve your code, proven through personal experience - 2

1. If you want to improve your code, read someone else's

If you want to dramatically improve your programming skills, you need to... read code written by other programmers. Believe me or don't. But if you take the risk, I promise: you will be rewarded for the time spent. For example, don't read on about how HashMap, ArrayList, LinkedList, etc. work. Instead, read their source code and figure it out yourself. Here is a list of classes to read:
  • The most frequently asked questions in interviews are about HashMap. You can kill two birds with one stone: you will understand the code and gain the knowledge you need.
  • The same thing is true about ArrayList. There's nothing complicated, but the source code is really worth reading and understanding.
  • String is an excellent example. Understand why it is immutable.
  • AtomicInteger is a cool class: it defines atomic operations on Integer objects.
  • After that, well, we could list every class, one after another :)
More seriously, you should read the code from Pivotal. The folks there have written the most in-demand ecosystem in the Java world. Their code is definitely worth reading. I recommend that you start with Spring Core. Reading unfamiliar source code is difficult but rewarding work. :)

2. Follow code conventions

10 ways to improve your code, proven through personal experience - 3Coding conventions are a set of guidelines created by development teams. They include guidelines for coding style and techniques for every aspect of the code. These conventions may be written for the entire company or for a specific project. Coding conventions are generally specific to each programming language and cover file organization, indentation, comments, declarations, operators, spaces, naming conventions, programming techniques and principles, programming rules, best practices for architecture, and so on. The main benefit of certain standards is the fact that the code looks the same and is written in the same style. This makes it more readable and helps programmers understand code written by another programmer. If coding standards are followed and applied consistently throughout the development process, it will be easier in the future to maintain and extend your code, refactor it, and resolve integration conflicts. Coding conventions are important to programmers for a number of reasons:
  • 40-80% of the cost of software goes to its maintenance,
  • Hardly any software is maintained by its author throughout its life,
  • Coding conventions improve the readability of source code by allowing programmers to understand new code more quickly.
To be honest, I'm a big fan of coding standards. To me, it makes sense to spend time discussing and arguing about them, since it is a valuable activity that will save you time and effort in the future. Code conventions should be reviewed frequently. Rules can be changed from "required" to "optional" and vice versa. If some of them do not work as expected, they should be revised or removed from the guidelines.

3. Use code reviews

A code review is one of the most powerful tools for code improvement.10 ways to improve your code, proven through personal experience - 4Why? Because the code will be looked at by those experts who did not write it. And a fresh look is very useful. And a code review is often what helps prevent writing downright terrible code. I know that code reviews are not always possible, because you need to find another person who is willing to do one. But that doesn't mean you should skip using this tool. Quite the opposite: code reviews are a reason to find like-minded people who also need to improve the quality of their code. By the way, who will stop you from finding them here on CodeGym? In a place where everyone wants to be a programmer.

4. Write unit tests

My favorite technique for improving code is definitely writing unit tests. The more of them you write, the better. In computer programming, unit testing is a software development process in which the smallest testable piece of source code, called a unit, is tested individually and independently to see if it works as expected. This will help you discover failures in your algorithms and/or logic before you release your code. Because unit testing requires your code to be structured appropriately, the code must be split into smaller, more focused functions. Each is responsible for a single operation on a dataset, rather than large functions that perform several different operations (the single responsibility principle says hello...). The second benefit of writing well tested code is that you can avoid breaking code when making small changes to existing functionality. When the unit tests fail, they will tell you that something was written incorrectly. At first glance, the development time spent writing unit tests looks like an extra cost. However, unit tests will save time on debugging in the future. This should be a step-by-step process. So let's more forward with a smile — we'll write tests for every method and class :D

5. Use tools to improve code quality

There is no developer who has never made a mistake. Typically, the compiler catches syntax and arithmetic problems and displays the stack trace. But some problems may still surface that the compiler does not catch. For example, improperly implemented requirements, incorrect algorithms, incorrectly structured code, or some other potential problem that the community knows from experience. The only way to catch errors like this is to ask a more senior developer to review your code, right? But this approach is not a panacea and won't change much. For every new developer on the team, you should have an extra pair of eyes looking at his/her code. Luckily, there are many tools out there that can help you control the quality of your code. I've used Checkstyle, PMD, FindBugs, and SonarQube in my work on various projects. And there are others as well. They are all typically used to analyze code quality and generate some useful reports. Very often these reports are published by continuous integration servers such as Jenkins.

6. Write simple and straightforward code

10 ways to improve your code, proven through personal experience - 5Always write simple, understandable, and logical code. People tend to write complicated code in order to prove they can do it. Simple and logical code always works well, leads to fewer problems, and is more extensible. Good code is the best documentation. If you think to add a comment, ask yourself: "How can I improve the code so that this comment is not necessary?" — Steve McConnell.

7. Read the documentation

10 ways to improve your code, proven through personal experience - 6One of the most important habits of good programmers is to read a lot of documentation. Whether its specifications, JSRs, API docs, tutorials, or something else, reading documentation helps you build a solid foundation for your best programming. Last but not least, don't compare yourself to others. Comparing yourself to others will only lead to negative feelings and unhealthy competition. Each person has his or her own strengths and weaknesses. This means it is important to know them and work with them. Take inventory of yourself — list your strengths and work on them. Programming is a real pleasure: enjoy it.

"One man's constant is another man's variable."

Alan J. Perlis

8. Follow interesting bloggers

Thousands of enthusiasts around the world work with and write about the same technologies. Blogs are often written by the programmers themselves, and most of them share personal opinions and experiences. Through blogs, you can see different perspectives on the same technology. You can see both good and bad technologies on blogs. At a minimum, read articles on the Coding Dojo Blog and CodeGym :) Follow good blogs and comment on posts, share your opinion.

9. Read books about the profession

Nothing can replace a good book. A good book teaches basic concepts in a much simpler form and applies to things in the real world. Their authors are great programmers themselves. By reading books, you can learn from someone else's experience. I suggest you read Joshua Bloch's "Effective Java". This book presents seventy-eight indispensable rules of thumb for programmers: the best working solutions for the programming problems you face every day. It contains the most practical, authoritative guidelines for writing effective, well-designed programs. If you are just getting started with Java and have no programming experience, you can read "Sams Teach Yourself Java 2 in 24 Hours". And for writing clean code, there is an excellent book, "Clean Code", by Robert Martin. After reading it, you will experience a "feeling of beauty" in relation to code.

10. Code! Code! Code!

You can't become a good programmer just by memorizing a book. It's easy to talk about theoretical concepts. But you can only learn the limitations of a language or work out best practices when you write code. Therefore, to become a good programmer, you need to write a lot of code. If you are a beginner, start by writing programs for simple tasks like the Fibonacci series, palindromes, Pascal's triangle, etc. Then move on to larger tasks like a binary search tree, etc. If you are looking for an online platform for practicing Java programs, take a look at Coding Ground. Work your way through programming courses and I guarantee that your skills will be much better. Another option is to take the Harvard CS50 course, which is free.

Let's summarize

The person who makes no mistakes is the one who does nothing. That's why we marshal our patience and, like a hardworking grasshopper, we hone our coding skills. To do this, don't forget:
  • Read others' code
  • Provide and ask for code reviews
  • Write unit tests
  • Use tools to improve your code
  • Write simple and understandable code
  • Read the documentation written by those who could
  • Follow interesting programmers
  • Read books about the profession
  • Code! Code! Code!
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Omar Garnica Java Developer
22 January, 01:15
Great article ! I was already into reading books about it and doing ! You learn a lot by doing !
Roxana Bodin
Level 22 , United States of America, United States
20 January 2023, 20:36
very useful article! i enrolled in Harvard CS50 course, and i watched the intro of the first video, and as a graduate of the Automatic and Computer Science faculty in Timisoara, i can say that the presentation and teaching methods are genius! also, the tips 2-5 are used in the company i work, so they are really important if you want to become a programmer.
Omar Garnica Java Developer
22 January, 01:11
First time I hear / read about Harvard CS50, definitely will check it out !
Level 11 , Irvine, United States
20 September 2021, 02:33
nice job
Level 20 , Baotou, China
12 October 2020, 00:38
if I only have :4 level is it difficult for me?
Hanna Moruga Chief Editor at CodeGymModerator
13 October 2020, 07:02
Like in mastering any other skill, the time you need to become confident in coding may differ. But it's not rocket science :) Follow your plan, use various sources to understand the most difficult topics for you, code a lot, try not to make too many breaks in your learning. Read others' code and try to get a profound understanding of why it's written that way. Google everything :) And don't you ever lose your motivation: there are thousands of people who learn programming from scratch without any technical background.