Home  >  Article  >  Backend Development  >  Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

WBOY
WBOYOriginal
2016-07-29 08:50:14951browse

Introduction

I have been working on projects during this period, so I have been relatively busy. Today I finally have time to go back and take a good look at the code I wrote to see where there are problems and where there are "bad smells".

Take your time and recall the code slowly. Start catching bad smells.

Things we often do and think about

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Obscure if conditions

1) Processing of ||

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

 Get up and spend more time Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

While reducing the number of lines of code is a good goal, minimizing the time it takes to understand the code is an even better goal.

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Return value

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

The "$ret" above is an excuse for "I can't think of a name". Rather than using such an empty name, choose a name that describes the value or purpose of the entity. Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

$alias declares that this variable is used to carry aliases - indicating the purpose of this variable. And may help us find defectsLooking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

A good name should describe the purpose of the variable or the value it carries.

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Temporary variables

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code The $i here is "my temporary variable", which is specially used to automatically increase statistical data and avoid duplication of statistical points. But the most important thing about $i is not the temporary variable. Use charset_index to represent "the subscript of my statistical data", which is more "descriptive".

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Loop iterator

I also found bad smell in my js code.

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source codei Variable names are very generic, so don’t do them.

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Names like i, j, iter and it are commonly used as indexes and loop iterators. Although the name is vague, everyone knows that they mean "I am an iterator". - Actually if you use these names to mean something else, it would be confusing. So don't do it.

If you insist on using vague names like i, j, it, then you must have a good reason to convince yourself.

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Summary

If we spend a few more seconds to come up with a good name during the coding process, you will find that our "naming ability" will quickly improve.

I usually think of Chinese names first. If I really can’t think of the corresponding English name, I will use a translation tool to paste the Chinese name that comes to mind, and then cut out the named variable or function name.

Give me a gift of a nice name I saw recently

Looking for bad code smells in the project (naming) Project industry code list Project discipline and code Javaee project source code

Original link: http://www.cnblogs.com/baochuan/archive/2012/07/12/2588317.html

The above introduces the bad smell (naming) of looking for code in the project, including the project and code aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn