for loop without index declaration

前端 未结 5 1336
误落风尘
误落风尘 2021-01-24 00:11

So I declare a variable some where and initialize it. Now later on I need to use it to loop while its still positive so I need to decrement it. To me looping using a condition a

5条回答
  •  有刺的猬
    2021-01-24 01:16

    When you're writing code, you should always remember that you're not just writing it so that you can understand it now... it should be understandable to any future reader (which might be you after you've forgotten why you did it the way you did) as long as they have some coding knowledge. It seems to me that you might be trying to re-use your space variable just because you already have one. There's nothing wrong with starting a new one if it increases readability. So you might want to consider using int i = space; inside your for loop - I'm sure your computer will manage this without the stack overflowing, if you'll forgive my lousy pun ;-)

    In fact, consider refactoring your loop to a separate private method, appropriately named for readability of course, passing in space as an argument and setting the variable to the result you return. Happy to give you a code example if you explain what you're trying to achieve in your loop.

提交回复
热议问题