Regarding my logic flip comment lol. Disregard since I clearly see it was intentional after continuing with the artcile. I recently refactored a too much nested logic check because I had the very impression it wasn't redeable. Glad to see that kind of article, I will keep this tip in mind. Slightly easier to immediately go, ah huh, error s then success paths. The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments. Testing Blog.
This is another post in our Code Health series. A version of this post originally appeared in Google bathrooms worldwide as a Google Testing on the Toilet episode. You can download a printer-friendly version to display in your office. By Elliott Karpilovsky Deeply nested code hurts readability and is error-prone. Call request if response. Because you materialized the results in a list, it doesn't matter anymore that you enumerate fooEnumerated twice; you'll be iterating over an in-memory list twice.
Developer Express also makes CodeRush, which provides more features than Visual Assist, but which is also a much harder drain on your computer, and also which occasionally makes mistakes -- most constructs using macros won't work correctly with CodeRush. Resharper and Visual Assist are nice to have around -- I occasionally flip on CodeRush for some of it's better refactorings, but most of the time leave it off because Visual Assist X's code completion assistance is better.
This means:. It depends on the constructor, GZipStream disposes of the stream you passed in when you dispose of it unless you use one of the overloads that takes in a bool and you pass in true to leaveOpen.
However you do run a risk doing this. If GZipStream throws a ArgumentException because the CanRead property of the stream is false the passed in stream does not get disposed of. Personally I rather not depend on "something not going wrong" and instead usually code defensively and use the 3 statement version. Edit: Just to be clear, I'm not asking about nesting using statements. I'm asking whether or not an IDisposable that is created inside another IDisposable's 'using' statement will be disposed of at the end of the block.
Any explanation on why or why not would be appreciated. Then instead of a bunch of if you have a set of calls the the two methods. Behold the horror! Pretty filthy, right? In all honesty, anyone who has worked in production code is guaranteed to have seen code that nests much much deeper It's a scary world out there, and this example doesn't even begin to illustrate how bad it can get. I mean, this particular example actually fit on my narrow blog window.
Notice how in my refactored version the condition checks are still in the same order that they originally appeared? This is on purpose. Now this is technically not equivalent to the initial implementation.
Because the initial implementation says that for one of the logical paths that call DoThis thing the following must be met:. Thus, by combining the condition8 check with the condition1 check, how have I guaranteed all those other conditions? Additionally, how do I know that skipping those condition checks i.
This optimization actually may not make the code incorrect in certain situations because it really depends what those conditions are , but it's important to note that the checks would not be equivalent to the original. It's just something to pay attention to, but who knows, you may even find that you can optimize some of those checks away depending on your situation!
Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Technical Blog. View Original.
0コメント