Good Practices
##########################################################################################################
Prefer for-each loops to traditional for loops
The for-each loop provides compelling advantages over the traditional for loop in clarity and bug prevention, with no performance penalty. You should use it wherever you can. Unfortunately, there are three common situations where you can’t use a for-each loop:
- Filtering—If you need to traverse a collection and remove selected elements, then you need to use an explicit iterator so that you can call its remove method.
- Transforming—If you need to traverse a list or array and replace some or all of the values of its elements, then you need the list iterator or array index in order to set the value of an element.
- Parallel iteration—If you need to traverse multiple collections in parallel, then you need explicit control over the iterator or index variable, so that all iterators or index variables can be advanced in lockstep
##########################################################################################################
Minimize the scope of local variables
- The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.
- Prefer for loops to while loops – Reduce local variable scope (reducing the cut and paste error for multiple loop in same scope) and it is providing initialization section that is called only once for a loop by that it is reducing each time declaration of local variables cost.
- Keep methods small and focused.
##########################################################################################################
Know and use the libraries
- By using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of those who used it before you.
- You don’t have to waste your time writing ad hoc solutions to problems that are only marginally related to your work.
- Their performance tends to improve over time, with no effort on your part.
##########################################################################################################