JavaRush /Java Blog /Random EN /10 ways to improve the code, proven by personal experienc...

10 ways to improve the code, proven by personal experience

Published in the Random EN group
Hello everyone, CodeGym community! 10 ways to improve the code, proven by personal experience - 1 Today we will talk about the quality of the code. Yes, dear friends. Nobody's perfect. Everyone understands at some point that the code could be better... But what to do in this situation? At the very least, start investigating this issue. Since you're here, it means you're interested in the topic, 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 will not 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

10 ways to improve the code, proven by 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. But if you take a chance, I promise: there will be a reward for the time spent. For example, do not read on Habré how HashMap, ArrayList, LinkedList and so on work, but read their code and figure it out on your own. Here is a list of classes to read:
  1. HashMap - the most common interview questions for it. You can kill several birds with one stone: understand the code and get the knowledge you need.

  2. ArrayList is the same case as with HashMap. There are no complicated things, but it is 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, you should read the code from Pivotal. These guys have written the most in-demand 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 hard, but very rewarding work.)

2. Follow Code Conventions

10 ways to improve the code, proven by personal experience - 3Coding conventions are a set of guidelines developed by development teams that include recommendations on coding style and methods for every aspect of the code that is written in-house or on a specific project. These conventions are generally specific to each programming language and cover file organization, indentation, comments, declarations, operators, spaces, naming conventions, programming methods and principles, programming rules, architectural best practices, etc. The main benefit of certain standards is 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, in the future, it will be easier to maintain and extend the code, refactor it, and resolve integration conflicts. Code conventions are important to programmers for a number of reasons:
  • 40%-80% of the cost of the software goes to its maintenance;
  • hardly any software is maintained by the author throughout his life;
  • coding conventions improve the readability of software by allowing programmers to understand new code more quickly.
To be honest, I'm a big fan of coding standards. It makes sense for me 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 don't work properly, they need to be reworked or removed from the guidelines.

3. Use Code Review - code review by others

Code review is one of the most powerful code improvement tools. 10 ways to improve the code, proven by personal experience - 4Why? Because the code will be looked at by those specialists who did not write it. A fresh look is very helpful. And often it is Code Review that helps prevent writing frankly terrible code. I know that this is not always possible, because you need to find another person who would be willing to do this. But this does not mean that this tool should not be used. Quite the contrary: this is a reason to find like-minded people who also need to improve the quality of the code. By the way, who forbids finding them here on CodeGym? A place where everyone wants to be a programmer.

4. Write Unit Tests - unit tests

10 ways to improve the code, proven by personal experience - 5Definitely my favorite code improvement technique is writing unit tests. And the more you write them, 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 they work as expected. This will help you identify failures in algorithms and/or logic before code is released. Since unit testing requires proper structure of your code, this means that the code must be split into smaller and more focused functions. Each of which is responsible for a single operation on a set of data, and not for large functions that perform a number of different operations. (As they say, hello fromSingle Responsibility Principle ...) The second benefit of writing well-tested code is that future crashes can be prevented with small changes to existing code-breaking functionality. When it fails, the unit tests will say something was written wrong. At first glance, spending time writing unit tests during development looks like an extra expense. However, this will save time in the future, which will have to be spent on debugging. It should be a step by step process. Therefore, go ahead and with a song - we write tests for all methods and classes: D

5. Use tools to improve code quality

10 Ways to improve code, proven by personal experience - 6There is no developer who would never make a mistake. Normally, the compiler catches the syntax and arithmetic problems and outputs a stack trace. But there may still be some issues 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 bugs is to have some senior developer check your code. This approach is not a panacea and changes little. With each new developer on the team, you should have an extra pair of eyes to look 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. There are others. 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 understandable code

10 ways to improve the code, proven by personal experience - 7Always write simple, understandable and logical code. People tend to write complex code to prove their ability to do so. Simple but logical code always works well, leads to fewer problems, and is more extensible. Good code is the best documentation. If the thought of adding a comment comes up, 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 code, proven by personal experience - 8One of the important habits of good programmers is to read a lot of documentation. Be it specs, JSRs, API docs, tutorials, etc. Reading the docs helps you build that solid foundation on which you can best code. Last but not least, don't compare yourself to others. Comparing yourself to others will only lead to negative feelings and unhealthy competition. Each has its 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 a real pleasure: enjoy it.

“One person's constant is another person's variable.”

Alan J. Perlis

8. Follow interesting bloggers

10 Ways to improve code, proven by personal experience - 9Around the world, thousands of enthusiasts work on the same technologies and write about them. 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 in blogs. At least read the articles on Habré CodeGym :D, including mine. Follow good blogs and comment on posts, share your opinion.

9. Read profile books

10 Ways to improve code, proven by personal experience - 10Nothing can replace a good book. A good book will teach the basic concepts in a much simpler way and apply them 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 indispensable rules of thumb for a programmer: working, best solutions to the programming problems you face every day. It contains the most practical, authoritative guides that you should use to write effective, well-designed programs. If you're just getting started with Java with no programming experience, you might want to read Sams Teach Yourself Java 2 in 24 Hours. And for writing clean code, there is a great book called Clean Code by Robert Martin.

10. Code! Code! Code!

You can't become a good programmer just by memorizing a book. 10 ways to improve code, proven by personal experience - 11It is easy to talk about theoretical concepts. But you can only learn language restrictions 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 bigger 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 10 ways to improve the code, proven by personal experience - 12

Summing up

The one who does nothing makes no mistakes. Therefore, we gain patience and, like a hardworking Japanese, hone our coding skills. To do this, do not forget:
  • read someone else's code;
  • conduct and ask to conduct Code Reivew;
  • write unit tests;
  • use tools to improve the code;
  • write simple and understandable code;
  • read the documentation of those who could;
  • 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