JavaRush /Java Blog /Random EN /10 ways to improve your code, tested from personal experi...

10 ways to improve your code, tested from personal experience

Published in the Random EN group
Hello everyone, JavaRush community! 10 ways to improve your code, tested from personal experience - 1 Today we'll talk about code quality. Yes, yes, dear friends. Nobody's perfect. Everyone someday comes to the understanding that the code could be better... But what to do in this situation? At the very least, start researching this issue. Since you're here, it means the topic is interesting to you, so let's go. Today we will describe ways in which you can make your code better and cleaner. So that in the future you won’t be ashamed of your current code!..)) All these methods will help the programmer become a good programmer.

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

10 ways to improve your code, tested from personal experience - 2If you want to dramatically improve your programming skills, you need to... read code written by other programmers. You can believe in it, you can not believe it. But if you take the risk, I promise you there will be a reward for your time. For example, don’t read on Habré how HashMap, ArrayList, LinkedList and so on work, but read their code and figure it out yourself. Here is a list of classes to read:
  1. HashMap is the most frequently asked question in interviews. You can kill several birds with one stone: understand the code and obtain the necessary knowledge.

  2. ArrayList is the same case as with HashMap. There are no complicated things there, but it’s really worth reading and understanding.

  3. String is a great example. Understand why it is immutable.

  4. AtomicInteger is a cool class: it describes the process of how to atomically perform operations on Integer objects.

  5. And then - at least all the classes in a row...)

If we talk about something more serious, of course, it’s worth reading the code from Pivotal. These guys wrote the most sought-after ecosystem in the Java world. Their code is definitely worth reading. Therefore, I advise you to start with Spring Core . Reading unfamiliar source code is challenging but very rewarding work.)

2. Follow Code Conventions - conventions for writing code

10 ways to improve your code, tested from personal experience - 3Coding conventions are a set of guidelines developed by development teams that include guidelines for programming style and techniques for every aspect of the code that is written within a company or a specific project. These conventions are typically specific to each programming language and cover file organization, indentation, comments, declarations, operators, whitespace, naming conventions, programming techniques and principles, programming rules, architectural best practices, etc. The main benefit of certain standards is that 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 consistently applied throughout the development process, it will be easier to maintain and extend the code, refactor it, and resolve integration conflicts in the future. Coding conventions are important to programmers for a number of reasons:
  • 40%-80% of the software cost is spent on its maintenance;
  • hardly any software is maintained by the author throughout his life;
  • Code conventions improve software readability, 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, as it is a valuable activity that will save you time and effort in the future. Code Conventions should be reviewed frequently. Rules can be translated from “mandatory” to “optional” and vice versa. If some of them do not work properly, they need to be redesigned or removed from the guidelines.

3. Use Code Review - code review by others

Code review is one of the most powerful tools for improving code. 10 ways to improve your code, tested from personal experience - 4Why? Because the code will be looked at by those specialists who did not write it. And a fresh look is very useful. And often it is Code Review that helps prevent the writing of frankly terrible code. I know that this is not always possible, because you need to find another person who would want to do this. But this does not mean that this tool is not worth using. Quite the contrary: this is a reason to find like-minded people for whom it is also important to improve the quality of the code. By the way, who forbids you to find them here on JavaRush? In a place where everyone wants to become a programmer.

4. Write Unit Tests - unit tests

10 ways to improve your code, tested from personal experience - 5By far my favorite method for improving code is writing unit tests. Moreover, the more you write them, the better. In computer programming, unit testing is a software development process in which the smallest testable part of the source code, called a unit, is tested individually and independently to check whether they work as expected. This will help you identify failures in algorithms and/or logic before releasing the code. Since unit testing requires the appropriate structure of your code, it means that the code must be divided into smaller and more focused functions. Each of which is responsible for a single operation on a set of data, rather than large functions performing a number of different operations. (As they say, hello from the Single Responsibility Principle ...) The second benefit of writing well-tested code is that you can prevent future failures on small changes to existing code breaking functionality. When a failure occurs, the unit tests will tell you that something was written incorrectly. At first glance, spending time writing unit tests during development seems like an additional expense. However, this will save time in the future, which will have to be spent on debugging. This should be a step-by-step process. Therefore, go ahead and sing - write tests for all methods and classes :D

5. Use tools to improve code quality

10 ways to improve your code, tested from personal experience - 6There is no developer who never makes mistakes. Typically the compiler will catch syntax and arithmetic problems and output a stack trace. But there may still be some problems that the compiler doesn't catch. For example, improperly implemented requirements, incorrect algorithms, incorrect code structure, or some potential problems known to the community from experience. The only way to catch such errors is to ask some senior developer to review your code. This approach is not a panacea and changes little. With every new developer on the team, you should have an extra pair of eyes looking at his/her code. But fortunately, there are many tools that can help you control the quality of your code. I used Checkstyle, PMD, FindBugs, SonarQube in my work on various projects. There are others too. All of them are commonly used for quality analysis and generating some useful reports. Very often these reports are published by continuous integration servers such as Jenkins.

6. Write simple and clear code

10 ways to improve your code, tested from personal experience - 7Always write simple, clear and logical code. People tend to write complex code to prove their ability to do it. Simple but logical code always works well, results in fewer problems, and is more extensible. Good code means better documentation. If you are thinking about adding a comment, ask yourself: “ How can I improve the code so that this comment is not needed? " ~Steve McConnell.

7. Read the documentation

10 ways to improve your code, tested from personal experience - 8One of the important habits of good programmers is to read a lot of documentation. Let it be specifications, JSR, API docs, tutorials, etc. Reading docs helps you build that strong foundation on which you program best. Last but not least, don't compare yourself to others. Comparing yourself to others will only lead to negative feelings and unhealthy competition. Everyone has their own strengths and weaknesses. Therefore, it is important to know them and work with them. Analyze yourself, list your strengths and work on them. Programming is real fun: enjoy it.

“One man’s constant is another man’s variable.”

Alan J. Perlis

8. Follow interesting bloggers

10 ways to improve your code, tested from personal experience - 9There are thousands of enthusiasts around the world working on and writing about the same technologies. Blogs are often written by programmers themselves, and most of them share personal opinions and experiences. You can see different perspectives of the same technology on blogs. You can see both good and bad technology on blogs. At a minimum, read articles on Habré JavaRush :D, including mine. Follow good blogs and comment on posts, share your opinion.

9. Read specialized books

10 ways to improve your code, tested from personal experience - 10Nothing can replace a good book. A good book will teach basic concepts in a much simpler way and apply to real world objects. Their authors are great programmers themselves. By reading books, you can learn from the experiences of others. I suggest reading “Java. Effective Programming by Joshua Bloch. This book presents seventy-eight irreplaceable programmer rules of thumb: working, best solutions for the programming problems you face every day. It contains the most practical, authoritative guidelines you can use to write effective, well-designed programs. If you are just starting out with Java and have no programming experience, you may want to 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 it, a “feeling of beauty” will appear in relation to the code.

10. Code! Code! Code!

You can't become a good programmer just by memorizing a book. 10 ways to improve your code, tested from personal experience - 11It's easy to talk about theoretical concepts. But you can only learn language limitations or develop 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 problems like Fibonacci series, palindrome, Pascal's triangle, etc. and then move on to larger problems like binary search tree, etc. If you are looking for online platform for practicing Java programs, take a look at Coding Ground . Moreover, you can take the CS50 course , the translation of which was provided to us by “you know who” :D

Let's sum it up

The one who does nothing makes no mistakes. Therefore, we must be patient and, like a hardworking Japanese, we hone our coding skills. For this, do not forget:
  • read someone else's code;
  • carry out and ask for a Code Reivew;
  • write unit tests;
  • use tools to improve code;
  • write simple and understandable code;
  • read the documentation of those who were able;
  • follow interesting programmers;
  • read specialized books;
  • Code! Code! Code!
  • subscribe to Roman on GitHub .

useful links

More of my articles

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION