代码改变世界

随笔档案-2014年05月

JavaScript Patterns 3.4 Array Literal

2014-05-30 23:03 by 小郝(Kaibo Hao), 459 阅读, 收藏,
摘要: To avoid potential errors when creating dynamic arrays at runtime, it's much safer to stick with the array literal notation. 阅读全文

JavaScript Patterns 3.3 Patterns for Enforcing new

2014-05-29 13:24 by 小郝(Kaibo Hao), 314 阅读, 收藏,
摘要: When your constructor has something like this.member and you invoke the constructor without new, you’re actually creating a new property of the global object called member and accessible through window.member or simply member.This post introduces how to enforce "new an object" with the constructor even if there is no new in front of the constructor. 阅读全文

JavaScript Patterns 3.2 Custom Constructor Functions

2014-05-28 22:56 by 小郝(Kaibo Hao), 302 阅读, 收藏,
摘要: When you invoke the constructor function with new, the following happens inside the function: • An empty object is created and referenced by this variable, inheriting the prototype of the function. • Properties and methods are added to the object referenced by this. • The newly created object referenced by this is returned at the end implicitly (if no other object was returned explicitly). 阅读全文

JavaScript Patterns 3.1 Object Literal

2014-05-27 12:30 by 小郝(Kaibo Hao), 279 阅读, 收藏,
摘要: Don't use new Object(); use the simpler and reliable object literal instead.This will help you get readable and reliable code. 阅读全文

JavaScript Patterns 2.12 Writing API Docs

2014-05-26 13:57 by 小郝(Kaibo Hao), 323 阅读, 收藏,
摘要: Process of generating API documentation • Writing specially formatted code blocks • Running a tool to parse the code and the comments • Publishing the results of the tool, which are most often HTML pages 阅读全文

JavaScript Patterns 2.11 Writing Comments

2014-05-26 13:43 by 小郝(Kaibo Hao), 290 阅读, 收藏,
摘要: Document all functions, their arguments and return values, and also any interesting or unusual algorithm or technique. Think of the comments as hints ... 阅读全文

JavaScript Patterns 2.10 Naming Conventions

2014-05-25 17:29 by 小郝(Kaibo Hao), 329 阅读, 收藏,
摘要: This post introduces how to name the functions, constructors and properties correctly. 阅读全文

JavaScript Patterns 2.9 Coding Conventions

2014-05-24 23:07 by 小郝(Kaibo Hao), 322 阅读, 收藏,
摘要: It’s important to establish and follow coding conventions—they make your code consistent, predictable, and much easier to read and understand. A new developer joining the team can read through the conventions and be productive much sooner, understanding the code written by any other team member. 阅读全文

JavaScript Patterns 2.8 Number Conversions with parseInt()

2014-05-23 12:28 by 小郝(Kaibo Hao), 294 阅读, 收藏,
摘要: This post introduces how to deal with Number Conversions with parseInt(). 阅读全文

JavaScript Patterns 2.7 Avoiding Implied Typecasting

2014-05-22 23:29 by 小郝(Kaibo Hao), 269 阅读, 收藏,
摘要: This post introduces how to use the ===, eval, Function() carefully. 阅读全文

JavaScript Patterns 2.6 switch Pattern

2014-05-21 22:03 by 小郝(Kaibo Hao), 285 阅读, 收藏,
摘要: This post introduces the principle of the use of switch-pattern. 阅读全文

JavaScript Patterns 2.5 (Not) Augmenting Build-in Prototypes

2014-05-20 13:36 by 小郝(Kaibo Hao), 349 阅读, 收藏,
摘要: Other developers using your code will probably expect the built-in JavaScript methods to work consistently and will not expect your additions. And properties you add to the prototype may show up in loops that don't use hasOwnProperty(), so they can create confusion.Please be cautious of Augmenting Build-in Prototypes. 阅读全文

JavaScript Patterns 2.4 For-in loop

2014-05-19 11:06 by 小郝(Kaibo Hao), 335 阅读, 收藏,
摘要: Enumeration should be used to iterate over nonarray objects. It's important to use the method hasOwnProperty()when iterating over object properties to filter out properties that come down the prototype chain. 阅读全文

JavaScript Patterns 2.3 For loops

2014-05-17 10:14 by 小郝(Kaibo Hao), 274 阅读, 收藏,
摘要: Do you know how to write the for-loop efficiently? Read this to get it. :) 阅读全文

Java Concurrency In Practice -Chapter 2 Thread Safety

2014-05-16 20:03 by 小郝(Kaibo Hao), 355 阅读, 收藏,
摘要: This post introduces the challenge and related solution (Atomic, Locks) on Thread Safety. 阅读全文

JavaScript Patterns 2.2 Minimizing Globals

2014-05-15 23:14 by 小郝(Kaibo Hao), 336 阅读, 收藏,
摘要: This blog post introduces the problem with Globals, Side Effect when Forgetting var, Access the Global Object, Single var Pattern and Hoisting: A problem with Scattered vars. 阅读全文

Java Concurrency In Practice - Chapter 1 Introduction

2014-05-14 23:15 by 小郝(Kaibo Hao), 243 阅读, 收藏,
摘要: A brief introduction on why to introduce Multiple treads application and its features. 阅读全文

JavaScript Patterns 2.1 Writing Maintainable Code

2014-05-14 22:11 by 小郝(Kaibo Hao), 273 阅读, 收藏,
摘要: Maintainable code means code that: is readable, consistent, predictable, Looks as if it was written by the same person and documented. 阅读全文

JavaScript Patterns 1 Introduction

2014-05-13 08:00 by 小郝(Kaibo Hao), 314 阅读, 收藏,
摘要: Introduction of the JavaScript patterns such as Object-Oriented, No Classes, Prototypes, Environment. And some others related standard and tools such as ECMAScript 5, JSLint and fire bug . 阅读全文

Effective Java Index

2014-05-12 09:04 by 小郝(Kaibo Hao), 919 阅读, 收藏,
摘要: Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st language I have chosen for this migration. It's a nice chance to read some great books like "Effective Java 2nd Edition" and share the note for what I learned from it with you just as what I did before with CSharp. Here is the index of the notes. Hope you like it :) 阅读全文

Effective Java 78 Consider serialization proxies instead of serialized instances

2014-05-12 08:39 by 小郝(Kaibo Hao), 482 阅读, 收藏,
摘要: Consider the serialization proxy pattern whenever you find yourself having to write a readObject or writeObject method on a class that is not extendable by its clients. This pattern is perhaps the easiest way to robustly serialize objects with nontrivial invariants. 阅读全文

Effective Java 77 For instance control, prefer enum types to readResolve

2014-05-11 20:40 by 小郝(Kaibo Hao), 462 阅读, 收藏,
摘要: You should use enum types to enforce instance control invariants wherever possible. If this is not possible and you need a class to be both serializable and instance-controlled, you must provide a readResolve method and ensure that all of the class's instance fields are either primitive or transient. 阅读全文

Effective Java 76 Write readObject methods defensively

2014-05-10 15:20 by 小郝(Kaibo Hao), 564 阅读, 收藏,
摘要: Anytime you write a readObject method, adopt the mind-set that you are writing a public constructor that must produce a valid instance regardless of what byte stream it is given. Do not assume that the byte stream represents an actual serialized instance. 阅读全文

Effective Java 75 Consider using a custom serialized form

2014-05-09 07:39 by 小郝(Kaibo Hao), 462 阅读, 收藏,
摘要: when you have decided that a class should be serializable (Item 74), think hard about what the serialized form should be. Use the default serialized form only if it is a reasonable description of the logical state of the object; otherwise design a custom serialized form that aptly describes the object. You should allocate as much time to designing the serialized form of a class as you allocate to designing it 阅读全文

Effective Java 74 Implement Serializable judiciously

2014-05-08 22:25 by 小郝(Kaibo Hao), 501 阅读, 收藏,
摘要: Unless a class is to be thrown away after a short period of use, implementing Serializable is a serious commitment that should be made with care. Extra caution is warranted if a class is designed for inheritance. For such classes, an intermediate design point between implementing Serializable and prohibiting it in subclasses is to provide an accessible parameterless constructor. This design point permits, but does not require, subclasses to implement Serializable. 阅读全文

Effective Java 73 Avoid thread groups

2014-05-07 07:42 by 小郝(Kaibo Hao), 545 阅读, 收藏,
摘要: Thread groups don't provide much in the way of useful functionality, and much of the functionality they do provide is flawed. Thread groups are best viewed as an unsuccessful experiment, and you should simply ignore their existence. If you design a class that deals with logical groups of threads, you should probably use thread pool executors (Item 68). 阅读全文

Effective Java 72 Don't depend on the thread scheduler

2014-05-06 08:52 by 小郝(Kaibo Hao), 509 阅读, 收藏,
摘要: Do not depend on the thread scheduler for the correctness of your program. The resulting program will be neither robust nor portable. As a corollary, do not rely on Thread.yield or thread priorities. These facilities are merely hints to the scheduler. Thread priorities may be used sparingly to improve the quality of service of an already working program, but they should never be used to "fix" a program that barely works. 阅读全文

Effective Java 71 Use lazy initialization judiciously

2014-05-05 08:40 by 小郝(Kaibo Hao), 550 阅读, 收藏,
摘要: You should initialize most fields normally, not lazily. If you must initialize a field lazily in order to achieve your performance goals, or to break a harmful initialization circularity, then use the appropriate lazy initialization technique. For instance fields, it is the double-check idiom; for static fields, the lazy initialization holder class idiom. For instance fields that can tolerate repeated initialization, you may also consider the single-check idiom. 阅读全文

Effective Java 70 Document thread safety

2014-05-04 08:54 by 小郝(Kaibo Hao), 520 阅读, 收藏,
摘要: Every class should clearly document its thread safety properties with a carefully worded prose description or a thread safety annotation. The synchronized modifier plays no part in this documentation. Conditionally thread-safe classes must document which method invocation sequences require external synchronization, and which lock to acquire when executing these sequences. If you write an unconditionally thread-safe class, consider using a private lock object in place of synchronized methods. Thi 阅读全文

Effective Java 69 Prefer concurrency utilities to wait and notify

2014-05-03 21:13 by 小郝(Kaibo Hao), 649 阅读, 收藏,
摘要: using wait and notify directly is like programming in "concurrency assembly language," as compared to the higher-level language provided by java.util.concurrent. There is seldom, if ever, a reason to use wait and notify in new code. If you maintain code that uses wait and notify, make sure that it always invokes wait from within a while loop using the standard idiom. The notifyAll method should generally be used in preference to notify. If notify is used, great care must be taken to ensure liven 阅读全文

Effective Java 68 Prefer executors and tasks to threads

2014-05-02 23:39 by 小郝(Kaibo Hao), 600 阅读, 收藏,
摘要: The general mechanism for executing tasks is the executor service. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in terms of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation. 阅读全文

Effective Java 67 Avoid excessive synchronization

2014-05-01 22:17 by 小郝(Kaibo Hao), 588 阅读, 收藏,
摘要: To avoid deadlock and data corruption, never call an alien method from within a synchronized region. More generally, try to limit the amount of work that you do from within synchronized regions. When you are designing a mutable class, think about whether it should do its own synchronization. In the modern multicore era, it is more important than ever not to synchronize excessively. Synchronize your class internally only if there is a good reason to do so, and document your decision clearly (Item 阅读全文