Try-catching in a single line of code(adverseconditionals.com)

submitted by mcintyre321mcintyre321(308) 3 years, 11 months ago

Just another silly extension method hack :)

11 comments |category: |Views: 926

tags: another

new Add a live kick counter to your blog >> liveImage

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:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by Tr3vTr3v(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 yesthatmcgurkyesthatmcgurk(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.

Reply

posted by Tr3vTr3v(376) 3 years, 11 months ago 0

Yes that is indeed true, however my comment was aimed at the question rather than the example.

Reply

posted by JudahGabrielJudahGabriel(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.

Reply

posted by spoulsonspoulson(327) 3 years, 11 months ago 0

Not bad. Condenses 5 lines of actual code down to one. Perfect for shrinking tiny little bits.

Reply

posted by offwhiteoffwhite(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.

Reply

posted by software.developersoftware.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.

Reply

posted by JudahGabrielJudahGabriel(814) 3 years, 11 months ago 0

@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.)

Reply

posted by gt1329agt1329a(7849) 3 years, 11 months ago 0

I wouldn't use that in a project with a team consisting of more than myself.

Reply

posted by mcintyre321mcintyre321(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;}

Reply

posted by samdnpsamdnp(980) 3 years, 11 months ago 0

I like it, but wouldn't use it :)

Reply

information Login or create an account to comment on this story