Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

When don't write comment code in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "when do not write comment code in java". In daily operation, I believe that many people have doubts about when not to write comment code in java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "when not to write comment code in java". Next, please follow the editor to study!

When shouldn't you write notes?

A rule of thumb is that whenever you find yourself using comments to explain what this code is for, it's basically when your code needs to be refactored to get cleaner.

A typical solution

Now that you know why you sometimes avoid writing comments, is there any solution? In fact, there is no effective solution yet, but you can clean your code so that you (and others) can understand it without comments.

In order to replace comments with readable code, our typical solution is to use extraction refactoring (Extract Method refactoring).

Let's take a look at a simple example of how to improve your code to liberate comments.

IsPremium () and $customer- > numberOfPurchases > 8) {} / / OR if ($customer- > canGetFreeProduct ()) {}

I hope you will notice the difference between these two conditional statements. Which is neater? It's obvious that the second one is cleaner and better. This is because, first of all, we can delete comments because we can understand what we are thinking when we look at the code. Second, and most importantly, we have extracted the logic of checking whether customers deserve a free product into its method, which is a good thing, especially if you want to use it again in your application.

At this point, the study of "when you don't need to write comment code in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report