楼主: ReneeBK
760 3

The Art of Readable Code [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49492 个
通用积分
53.3854
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

1论坛币

关键词:READ ABLE code ABL ART
沙发
ReneeBK 发表于 2015-11-10 12:11:23 |只看作者 |坛友微信交流群
  1. Avoid Generic Names Like tmp and retval

  2. Names like tmp, retval, and foo are usually cop-outs that mean “I can’t think of a name.” Instead of using an empty name like this, pick a name that describes the entity’s value or purpose.

  3. For example, here’s a JavaScript function that uses retval:

  4. var euclidean_norm = function (v) {
  5.     var retval = 0.0;
  6.     for (var i = 0; i < v.length; i += 1)
  7.         retval += v[i] * v[i];
  8.     return Math.sqrt(retval);
  9. };
  10. It’s tempting to use retval when you can’t think of a better name for your return value. But retval doesn’t contain much information other than “I am a return value” (which is usually obvious anyway).

  11. A better name would describe the purpose of the variable or the value it contains. In this case, the variable is accumulating the sum of the squares of v. So a better name is sum_squares. This would announce the purpose of the variable upfront and might help catch a bug.

  12. For instance, imagine if the inside of the loop were accidentally:

  13.          retval += v[i];
  14. This bug would be more obvious if the name were sum_squares:

  15.          sum_squares += v[i];  // Where's the "square" that we're summing? Bug!
复制代码

使用道具

藤椅
ReneeBK 发表于 2015-11-10 12:12:13 |只看作者 |坛友微信交流群
  1. Example: DISALLOW_EVIL_CONSTRUCTORS
  2. Here’s an example from the codebase at Google. In C++, if you don’t define a copy constructor or assignment operator for your class, a default is provided. Although handy, these methods can easily lead to memory leaks and other mishaps because they’re executed “behind the scenes” in places you might not have realized.

  3. As a result, Google has a convention to disallow these “evil” constructors, using a macro:

  4. class ClassName {
  5.   private:
  6.     DISALLOW_EVIL_CONSTRUCTORS(ClassName);

  7.   public:
  8.     ...
  9. };
  10. This macro was defined as:

  11. #define DISALLOW_EVIL_CONSTRUCTORS(ClassName) \
  12.     ClassName(const ClassName&); \
  13.     void operator=(const ClassName&);
  14. By placing this macro in the private: section of a class, these two methods become private, so that they can’t be used, even accidentally.

  15. The name DISALLOW_EVIL_CONSTRUCTORS isn’t very good, though. The use of the word “evil” conveys an overly strong stance on a debatable issue. More important, it isn’t clear what that macro is disallowing. It disallows the operator=() method, and that isn’t even a “constructor”!

  16. The name was used for years but was eventually replaced with something less provocative and more concrete:

  17. #define DISALLOW_COPY_AND_ASSIGN(ClassName) ...
复制代码

使用道具

板凳
ReneeBK 发表于 2015-11-10 12:12:59 |只看作者 |坛友微信交流群
  1. Example: --run_locally
  2. One of our programs had an optional command-line flag named --run_locally. This flag would cause the program to print extra debugging information but run more slowly. The flag was typically used when testing on a local machine, like a laptop. But when the program was running on a remote server, performance was important, so the flag wasn’t used.

  3. You can see how the name --run_locally came about, but it has some problems:

  4. A new member of the team didn’t know what it did. He would use it when running locally (imagine that), but he didn’t know why it was needed.

  5. Occasionally, we needed to print debugging information while the program ran remotely. Passing --run_locally to a program that is running remotely looks funny, and it’s just confusing.

  6. Sometimes we would run a performance test locally and didn’t want the logging slowing it down, so we wouldn’t use --run_locally.

  7. The problem is that --run_locally was named after the circumstance where it was typically used. Instead, a flag name like --extra_logging would be more direct and explicit.

  8. But what if --run_locally needs to do more than just extra logging? For instance, suppose that it needs to set up and use a special local database. Now the name --run_locally seems more tempting because it can control both of these at once.

  9. But using it for that purpose would be picking a name because it’s vague and indirect, which is probably not a good idea. The better solution is to create a second flag named --use_local_database. Even though you have to use two flags now, these flags are much more explicit; they don’t try to smash two orthogonal ideas into one, and they give you the option of using just one and not the other.
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-9-20 04:53