Nested method calls vs. one-shot variables

爱⌒轻易说出口 提交于 2019-12-03 17:19:45

Personally I think that you should use "one-shot" variables since it doesn't come with any overhead (in "release" mode, that is with optimization on) and it makes debugging easier. Unless of course you have a specific reason not to.

If one-shot variables make your code more readable, use them. There's not often a reason for it to matter, AFAIK.

My vote is to go for whatever is easiest to read.

When I provide code examples here on SO, I use one-shot variables (OSV) because it's easier to explain it to somebody that way. But I don't use them in my own code that much.

If there is a section of my code that needs code comments I look at that as a good candidate to refactor into a method. Same with variables. If I need to think about what a parameter represents I might have that as a OSV, but for obvious parameters, such as nil or myView.subView, I find it harder to read if I create separate variables for them.

To answer the question: I use a mixture as you do, but I try not to worry too much about these things. Coding styles are meant to be guidelines, not rods for our backs -- As long as the code is readable and consistent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!