In many languages (e.g. both Perl and Java - which are the two languages I work most with) it is possible to narrow the scope of local variables by declaring them within a block.

Although it adds extra code length (the opening and closing block braces), and possibly reduces readability, should I create blocks purely to narrow the scope of variables to the statements that use the variables and to uphold the principle of narrowest scope or does this sacrifice succinctness and readability just to unnecessarily uphold an agreed 'best practice' principle?

I usually declare local variables to functions/methods at the start of the function to aid readability, but I could not do this, and just create blocks throughout the function and declare the variables throughout the code - within those blocks - to narrow their scope.

  • "avoid asking subjective questions where … every answer is equally valid" (help center). See meta.programmers.stackexchange.com/questions/6483/… – gnat Aug 21 '14 at 12:28
  • 2
    Your question amounts to "Should I create local blocks just for scope?" (but I'm not going to edit it, since it will be closed as opinion-based). My opinion is that you shouldn't, you should go all the way and create local functions instead. – Kilian Foth Aug 21 '14 at 12:30
  • An example would make this question 100 times better. If it gets closed here for being opinion-based, your example might be valid on CodeReview. – GlenPeterson Aug 21 '14 at 13:46

In layman's terms:

  • You don't have to sacrifice succintness to narrow scope
  • You keep functions/methods short
  • With short methods/functions you don't need to use blocks for narrowing scope of variables
  • Using blocks for narrowing scope of variables is an indicator that your functions/methods are too long.
  • Functions/methods so long that they require blocks for narrowing scope of variables, are the opposite of succint.
  • My functions/methods are almost always very short, but there's still opportunity to narrow scope of variables by introducing inner blocks. From my experience this is almost always true. – David Scholefield Aug 21 '14 at 13:39
  • @DavidScholefield How short are they ? I agree with Robert Martin's Clean Code, in that functions shouldn't ever reach 20 lines long. – Tulains Córdova Aug 21 '14 at 13:54

To answer a question with a question... are you talking about mutable or immutable variables?

The value of reducing scope for mutables is much higher - it reduces what the reader/maintainer needs to examine to determine what modifications are safe - or where a bug may be coming from.

The only time I worry much about intra-function scope for immutables is when they are instantiated based on other data that may be changing state within the execution of the function.

  • This is a very interesting answer (question?) I thought the main motivation was to provide a degree of encapsulation and increased ease of maintenance/support generally (not having to trace variable usage through long code blocks). I'm almost certainly talking about mutable variables. – David Scholefield Aug 21 '14 at 13:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.