Try-catching in a single line of code(adverseconditionals.com)
submitted by mcintyre321(308) 3 years, 11 months ago
Just another silly extension method hack :)
11 comments |category: C# |Views: 926 Tweet
tags: C# another
Add a live kick counter to your blog >>
You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:
Simply copy and paste this HTML into your blog post.
posted by Tr3v(376) 3 years, 11 months ago 0
"Do you like reducing your line count at the expense of readability to others?" Personally no. I think that the readability of code is rather important, especially in a team environment where you may need to read & understand someone else's code. Even on a personal project I still think readability is important, how many times have you done some clever/cool coding then come back to it months later and just gone WTF?!?
Reply
posted by yesthatmcgurk(4063) 3 years, 11 months ago 0
Dunno. Its pretty clear what he's doing. I mean, the method name is "Catch" and he's got a NRE in the lambda.... The only sore point is figuring out what he's using the lambda for.
Yes that is indeed true, however my comment was aimed at the question rather than the example.
posted by JudahGabriel(814) 3 years, 11 months ago 0
@Tr3v, I think the comment was kind of tongue-in-cheek. FWIW, I found the code still quite readable.
posted by spoulson(327) 3 years, 11 months ago 0
Not bad. Condenses 5 lines of actual code down to one. Perfect for shrinking tiny little bits.
posted by offwhite(975) 3 years, 11 months ago 0
And where do you place a breakpoint so you can step through this? I see absolutely no value in placing 6 steps of execution onto a single line of code. Are we trying to save on disc storage? Are we you trying to fit more code onto our 19 inch screens? I do not thing lambdas have been around long enough for this to be considered readable. Developers know the try...catch coding style and are comfortable with it. This in no way is more efficient code when it comes to the runtime. Either way it will unfold it into the same set of CLR instructions so you gain nothing by reducing 6 lines of code to 1.
posted by software.developer(8) 3 years, 11 months ago 0
If there are 2 projects that are EQUAL IN READABILITY.... <- caps lock I would rather debug the one with less code.
@spoulson "perfect for shrinking [my] tiny little bits" lol @offwhite, actually, since you're introducing a new Function delegate here, you're actually allocating a new object. This will result in more IL instructions, more memory usage. (Unless of course the JITer or compiler can optimize it away.)
posted by gt1329a(7849) 3 years, 11 months ago 0
I wouldn't use that in a project with a team consisting of more than myself.
posted by mcintyre321(308) 3 years, 11 months ago 0
I'm just glad none of you have pointed out that you can write on a single line already :) int y; try { y = DoSomething(); }catch (NullReferenceException ex){ y = -1;}
posted by samdnp(980) 3 years, 11 months ago 0
I like it, but wouldn't use it :)
All tags Your tags
Suggest a new category
dpeterson(1273)
Telerik(409)
fdub(381)
amit.jain(273)
BlackWasp(259)
RobertTheGrey(259)
jalpesh(252)
KodefuGuru(227)
KMillerr(226)
vijayst(223)