in reply to creating a variable in an if statement

When you're writing code, keep it simple and clear. Nothing else really matters.

If you want to confine a certain variable to a limited scope, it's fine to use "{}" braces to create a block, put "my" variable declarations inside that block and go-to-town. The variables will be confined to the static scope of that block. But do that only when that's clear.

“Obfuscations” are for idle-amusement only!

  • Comment on Re: creating a variable in an if statement

Replies are listed 'Best First'.
Re^2: creating a variable in an if statement
by wfsp (Abbot) on Jan 23, 2008 at 10:02 UTC
    I agree.
    ...it's fine to use "{}" braces to create a block...
    You could then consider giving the block a name i.e. create a sub. You can then
    • move the block out of your main code and, with a good descriptive name, make the code more readable
    • use the "return early, return often" idiom which can often help simplify and clarify your intentions
    • write it so that all arguments are passed to it so testing it is much easier
    • re-use it