Crazy Java Learning Notes (a)-----------gaze considerations

Source: Internet
Author: User

Code gazing is more important than the code itself. Here are some ways to make sure that the gaze you write in your code is friendly:

Don't repeat what the reader already knows

It's not helpful for us to see what the code is doing.

If the color is red, turn it greenif (color.is_red ()) {  color.turn_green ();}
To stare at the explanation of reasoning and history

Assuming that the business logic in the code might need to be updated or changed later, it should be left staring:)

/* The API currently returns an array of Itemseven though that would change in an upcoming ticket. Therefore, be sure to change the loop style here so thatwe properly iterate over a object */var Api_result = {items: ["On E "," "", "" "},    items = api_result.items,    num_items = Items.length;for (var x = 0; x < num_items; + +) {  ...}
The same line of gaze don't write very long

Nothing is more heinous than dragging the horizontal scroll bar to read the gaze. In fact, most developers will choose to ignore this kind of gaze. Because it is really inconvenient to read.

function person (name) {  this.name = name;  This.first_name = Name.split ("") [0]; This was just a shot in the dark. If We can extract the first name, let's Do it}
To put the long gaze on the logic, and the short gaze on the back

The gaze assumes that no more than 120 characters identifiers can be placed next to the code. Otherwise, the gaze should be placed directly above the statement.

if (Person.age <) {  Person.can_drink = false;//drinking Age/  * Fees is given to those under Ly in     some states. *  /Person.has_car_rental_fee = function (state) {    if (state = = = "MI") {      return True;
   }  };}
Do not add unnecessary gaze to gaze.

The superfluous gaze will cause confusion. Perhaps the teacher in school teaches you to add a gaze to all the statements, which will help the developer to understand better. But this is wrong.

If anyone says so, you give him two big slap at once.

There is no doubt that the code should be kept clean and concise. Assuming your code needs to be interpreted line by row, the most you need to do is refactor.

if (Person.age >=) {  Person.can_drink = true;//a person can drink at  Person.can_smoke = true;//a Pers  On can smoke @  person.can_wed = true;//a person can get married at +  person.can_see_all_movies = true;//A Person can see all movies @  //i hate babies and children and all things pure because I comment too much}
Stare to be spelled correctly

Don't make excuses for spelling mistakes in your code gaze. The IDE can check spelling for you.

Assuming that there is no such function, then go to download the plugin, do it yourself!

Practice more.

Practice makes perfect Try to write some practical stares. Be able to ask other developers if your gaze is practical. As time goes by. You will slowly know how to be a friendly stare.

To examine the gaze of others

In code reviews, we tend to overlook viewing gaze. Don't be afraid to ask for many other stares. You should ask questions. If everyone has a good habit of writing a good stare, then the world will be better.

Summarize

Gazing is an important part of the development process, but we should not stare at it. The gaze should be practical. Concise, should be a supplement to the code.

The gaze should not be used to interpret the code on a row-by-line basis, but instead it should be used to explain business logic, reasoning, and inspiration for the future.

Transfer from http://www.codeceo.com/article/comments-do-and-dont.html

Crazy Java Learning Notes (a)-----------gaze considerations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.