Effective javascript Page

Effective JavaScript by Cloud Monk, 2025



Return to Effective Polyglot Programming by Cloud Monk, Effective TypeScript by Dan Vanderkam, Effective JavaScript, JavaScript, Effective React, Effective SQL, Effective Java, Effective Spring Boot, Effective Scala, Effective Kotlin, Effective CPP | Effective C++, Effective Swift, Effective C Sharp, Effective PHP, Effective Golang, Effective Rust, Effective C Language, Effective Clojure, Effective Haskell, Effective Elixir, Effective Erlang, Effective Elm, Effective PowerShell, Effective F Sharp, Effective Dart, Effective R Language, Effective Ruby, Effective Julia

Have The Borg rewrite Effective Java for JavaScript as Effective JavaScript


Effective TypeScript by Cloud Monk - Table of Contents



Inspired by and Based on Effective Java, Third Edition by Joshua Bloch, Pearson Education Inc., 2018, ISBN-13: 978-0-13-468599-1, ISBN-10: 0-13-468599-7 and on Effective TypeScript by Dan Vanderkam


Contents

Foreword

Preface

Acknowledgments

1 Introduction

2 Creating and Destroying Objects

* Item 1: JavaScript Best Practices - Consider static factory methods instead of constructors
* Item 2: JavaScript Best Practices - Consider a builder when faced with many constructor parameters
* Item 3: JavaScript Best Practices - Enforce the singleton property with a private constructor or an enum type
* Item 4: JavaScript Best Practices - Enforce noninstantiability with a private constructor
* Item 5: JavaScript Best Practices - Prefer dependency injection to hardwiring resources
* Item 6: JavaScript Best Practices - Avoid creating unnecessary objects
* Item 7: JavaScript Best Practices - Eliminate obsolete object references
* Item 8: JavaScript Best Practices - Avoid finalizers and cleaners
* Item 9: JavaScript Best Practices - Prefer try-with-resources to try-finally

3 Methods Common to All Objects

* Item 10: JavaScript Best Practices - Obey the general contract when overriding equals
* Item 11: JavaScript Best Practices - Always override hashCode when you override equals
* Item 12: JavaScript Best Practices - Always override toString
* Item 13: JavaScript Best Practices - Override clone judiciously
* Item 14: JavaScript Best Practices - Consider implementing Comparable

4 Classes and Interfaces

* Item 15: JavaScript Best Practices - Minimize the accessibility of classes and members
* Item 16: JavaScript Best Practices - In public classes, use accessor methods, not public fields
* Item 17: JavaScript Best Practices - Minimize mutability
* Item 18: JavaScript Best Practices - Favor composition over inheritance
* Item 19: JavaScript Best Practices - Design and document for inheritance or else prohibit it
* Item 20: JavaScript Best Practices - Prefer interfaces to abstract classes
* Item 21: JavaScript Best Practices - Design interfaces for posterity
* Item 22: JavaScript Best Practices - Use interfaces only to define types
* Item 23: JavaScript Best Practices - Prefer class hierarchies to tagged classes
* Item 24: JavaScript Best Practices - Favor static member classes over nonstatic
* Item 25: JavaScript Best Practices - Limit source files to a single top-level class

5 Generics

* Item 26: JavaScript Best Practices - Don’t use raw types
* Item 27: JavaScript Best Practices - Eliminate unchecked warnings
* Item 28: JavaScript Best Practices - Prefer lists to arrays
* Item 29: JavaScript Best Practices - Favor generic types
* Item 30: JavaScript Best Practices - Favor generic methods
* Item 31: JavaScript Best Practices - Use bounded wildcards to increase API flexibility
* Item 32: JavaScript Best Practices - Combine generics and varargs judiciously
* Item 33: JavaScript Best Practices - Consider typesafe heterogeneous containers

6 Enums and Annotations

* Item 34: JavaScript Best Practices - Use enums instead of int constants
* Item 35: JavaScript Best Practices - Use instance fields instead of ordinals
* Item 36: JavaScript Best Practices - Use EnumSet instead of bit fields
* Item 37: JavaScript Best Practices - Use EnumMap instead of ordinal indexing
* Item 38: JavaScript Best Practices - Emulate extensible enums with interfaces
* Item 39: JavaScript Best Practices - Prefer annotations to naming patterns
* Item 40: JavaScript Best Practices - Consistently use the Override annotation
* Item 41: JavaScript Best Practices - Use marker interfaces to define types

7 Lambdas and Streams

* Item 42: JavaScript Best Practices - Prefer lambdas to anonymous classes
* Item 43: JavaScript Best Practices - Prefer method references to lambdas
* Item 44: JavaScript Best Practices - Favor the use of standard functional interfaces
* Item 45: JavaScript Best Practices - Use streams judiciously
* Item 46: JavaScript Best Practices - Prefer side-effect-free functions in streams
* Item 47: JavaScript Best Practices - Prefer Collection to Stream as a return type
* Item 48: JavaScript Best Practices - Use caution when making streams parallel

8 Methods

* Item 49: JavaScript Best Practices - Check parameters for validity
* Item 50: JavaScript Best Practices - Make defensive copies when needed
* Item 51: JavaScript Best Practices - Design method signatures carefully
* Item 52: JavaScript Best Practices - Use overloading judiciously
* Item 53: JavaScript Best Practices - Use varargs judiciously
* Item 54: JavaScript Best Practices - Return empty collections or arrays, not nulls
* Item 55: JavaScript Best Practices - Return optionals judiciously
* Item 56: JavaScript Best Practices - Write doc comments for all exposed API elements

9 General Programming

* Item 57: JavaScript Best Practices - Minimize the scope of local variables
* Item 58: JavaScript Best Practices - Prefer for-each loops to traditional for loops
* Item 59: JavaScript Best Practices - Know and use the libraries
* Item 60: JavaScript Best Practices - Avoid float and double if exact answers are required
* Item 61: JavaScript Best Practices - Prefer primitive types to boxed primitives
* Item 62: JavaScript Best Practices - Avoid strings where other types are more appropriate
* Item 63: JavaScript Best Practices - Beware the performance of string concatenation
* Item 64: JavaScript Best Practices - Refer to objects by their interfaces
* Item 65: JavaScript Best Practices - Prefer interfaces to reflection
* Item 66: JavaScript Best Practices - Use native methods judiciously
* Item 67: JavaScript Best Practices - Optimize judiciously
* Item 68: JavaScript Best Practices - Adhere to generally accepted naming conventions

10 Exceptions

* Item 69: JavaScript Best Practices - Use exceptions only for exceptional conditions
* Item 70: JavaScript Best Practices - Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
* Item 71: JavaScript Best Practices - Avoid unnecessary use of checked exceptions
* Item 72: JavaScript Best Practices - Favor the use of standard exceptions
* Item 73: JavaScript Best Practices - Throw exceptions appropriate to the abstraction
* Item 74: JavaScript Best Practices - Document all exceptions thrown by each method
* Item 75: JavaScript Best Practices - Include failure-capture information in detail messages
* Item 76: JavaScript Best Practices - Strive for failure atomicity
* Item 77: JavaScript Best Practices - Don’t ignore exceptions

11 Concurrency

* Item 78: JavaScript Best Practices - Synchronize access to shared mutable data
* Item 79: JavaScript Best Practices - Avoid excessive synchronization
* Item 80: JavaScript Best Practices - Prefer executors, tasks, and streams to threads
* Item 81: JavaScript Best Practices - Prefer concurrency utilities to wait and notify
* Item 82: JavaScript Best Practices - Document thread safety
* Item 83: JavaScript Best Practices - Use lazy initialization judiciously
* Item 84: JavaScript Best Practices - Don’t depend on the thread scheduler

12 Serialization

* Item 85: JavaScript Best Practices - Prefer alternatives to JavaScript serialization
* Item 86: JavaScript Best Practices - Implement Serializable with great caution
* Item 87: JavaScript Best Practices - Consider using a custom serialized form
* Item 88: JavaScript Best Practices - Write readObject methods defensively
* Item 89: JavaScript Best Practices - For instance control, prefer enum types to readResolve
* Item 90: JavaScript Best Practices - Consider serialization proxies instead of serialized instances

Items Corresponding to Second Edition

References

Index





Appendix: Items Corresponding to Second Edition


Second Edition Item Number

Third Edition Item Number, Title



1

1, Consider static factory methods instead of constructors



2

2, Consider a builder when faced with many constructor parameters



3

3, Enforce the singleton property with a private constructor or an enum type



4

4, Enforce noninstantiability with a private constructor



5

6, Avoid creating unnecessary objects



6

7, Eliminate obsolete object references



7

8, Avoid finalizers and cleaners



8

10, Obey the general contract when overriding equals



9

11, Always override hashCode when you override equals



10

12, Always override toString



11

13, Override clone judiciously



12

14, Consider implementing Comparable



13

15, Minimize the accessibility of classes and members



14

16, In public classes, use accessor methods, not public fields



15

17, Minimize mutability



16

18, Favor composition over inheritance



17

19, Design and document for inheritance or else prohibit it



18

20, Prefer interfaces to abstract classes



19

22, Use interfaces only to define types



20

23, Prefer class hierarchies to tagged classes



21

42, Prefer lambdas to anonymous classes



22

24, Favor static member classes over nonstatic



23

26, Don’t use raw types



24

27, Eliminate unchecked warnings



25

28, Prefer lists to arrays



26

29, Favor generic types



27

30, Favor generic methods



28

31, Use bounded wildcards to increase API flexibility



29

33, Consider typesafe heterogeneous containers



30

34, Use enums instead of int constants



31

35, Use instance fields instead of ordinals



32

36, Use EnumSet instead of bit fields



33

37, Use EnumMap instead of ordinal indexing



34

38, Emulate extensible enums with interfaces



35

39, Prefer annotations to naming patterns



36

40, Consistently use the Override annotation



37

41, Use marker interfaces to define types



38

49, Check parameters for validity



39

50, Make defensive copies when needed



40

51, Design method signatures carefully



41

52, Use overloading judiciously



42

53, Use varargs judiciously



43

54, Return empty collections or arrays, not nulls



44

56, Write doc comments for all exposed API elements



45

57, Minimize the scope of local variables



46

58, Prefer for-each loops to traditional for loops



47

59, Know and use the libraries



48

60, Avoid float and double if exact answers are required



49

61, Prefer primitive types to boxed primitives



50

62, Avoid strings where other types are more appropriate



51

63, Beware the performance of string concatenation



52

64, Refer to objects by their interfaces



53

65, Prefer interfaces to reflection



54

66, Use native methods judiciously



55

67, Optimize judiciously



56

68, Adhere to generally accepted naming conventions



57

69, Use exceptions only for exceptional conditions



58

70, Use checked exceptions for recoverable conditions and runtime exceptions for programming errors



59

71, Avoid unnecessary use of checked exceptions



60

72, Favor the use of standard exceptions



61

73, Throw exceptions appropriate to the abstraction



62

74, Document all exceptions thrown by each method



63

75, Include failure-capture information in detail messages



64

76, Strive for failure atomicity



65

77, Don’t ignore exceptions



66

78, Synchronize access to shared mutable data



67

79, Avoid excessive synchronization



68

80, Prefer executors, tasks, and streams to threads



69

81, Prefer concurrency utilities to wait and notify



70

82, Document thread safety



71

83, Use lazy initialization judiciously



72

84, Don’t depend on the thread scheduler



73

(Retired)



74

85, Prefer alternatives to JavaScript serialization

86, Implement Serializable with great caution



75

85, Prefer alternatives to JavaScript serialization

87, Consider using a custom serialized form



76

85, Prefer alternatives to JavaScript serialization

88, Write readObject methods defensively



77

85, Prefer alternatives to JavaScript serialization

89, For instance control, prefer enum types to readResolve



78

85, Prefer alternatives to JavaScript serialization

90, Consider serialization proxies instead of serialized instances