The Art of Readable Code

来源:互联网 发布:阿里云cdn 编辑:程序博客网 时间:2024/05/18 02:29

Chapter One:

key idea:

Code should be written to minimize the time it would take for someone else to understand it.

Chapter Two:

key idea:

pack information into your names.

a few tips:

Use specific words-- for example, instead of Get, would like Fetch or Download might be better, depending on the content.

Avoid generic names like tmp and retval, unless there's a specific reason to use them.

Use concrete names that describe things in more detail -- the name ServerCanStart is vague compare to CanListenPort.

Attach important details to variable  names-- for example append _ms to a variable whose value is in milliseconds or  prepend raw_ to an unprocessed variable that need escaping.

Use longer names for large scopes--don't use cryptic one- or two-letter names for variable that span multiple screens; short names are better for variables that span only a few lines.

Use capitalization, underscores, and so on in a meaningful way--for example, you can apprend "_" to class members to distinguish them from local variables