Good practices in object-oriented programming - # 1
- Always limit the scope of a local variable
The local variables are great, they make life easier for us. But sometimes they can insert more bugs during the recycling of old code. Minimizing the scope of a local variable makes the code more readable, less prone to errors and more maintainable.
Therefore, we must declare variables just before they are used.
Try to initialize a variable from its declaration. If that is not possible, assign it the null value and initialize the empty objects when necessary, in Java it is one of the most expensive operations in terms of memory use and impact on performance is the creation of objects, this is avoidable by initializing the objects only when they are required in the code.