Hello everyone, JavaRush community! 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.
Habré JavaRush :D, including mine. Follow good blogs and comment on posts, share your opinion.
1. If you want to improve your code, read someone else’s code
If 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:-
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.
-
ArrayList is the same case as with HashMap. There are no complicated things there, but it’s really worth reading and understanding.
-
String is a great example. Understand why it is immutable.
-
AtomicInteger is a cool class: it describes the process of how to atomically perform operations on Integer objects.
-
And then - at least all the classes in a row...)
2. Follow Code Conventions - conventions for writing code
Coding 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.
3. Use Code Review - code review by others
Code review is one of the most powerful tools for improving code. Why? 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
By 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 :D5. Use tools to improve code quality
There 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
Always 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
One 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.”
8. Follow interesting bloggers
There 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 on9. Read specialized books
Nothing 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. It'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” :DLet'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
- 10 Ways to Improve Your Programming Skills
- One easy way to improve your programming skills is to read other people's code
- 11 Best Practices and Tools to Improve the Java Code Quality
- How to improve Java programming skill
- 10 Tips to Become a Better Java Developer
More of my articles
- Debug in Intellij IDEA: a guide for beginners
- Guide to creating a client for the Skyscanner API and publishing it in jCenter and Maven Central [Part 1]
- Creating an air ticket price monitoring system: a step-by-step guide [Part 1]
- Logging: what, how, where and with what? How an interview test turned into an open-source library
- History of success. 20 hours of programming per week, master's degree and personal life
- How to find a job? Tips for sending out resumes