|
On the "?" operator - I will strive NEVER to use that.
On the head-asplode operator - Shouldn't that be
string result = DodgyApi.GetValue?*.();
And why aren't you working on my latest feature request?*.() And please don't say you simply haven't GOTTEN around to it yet.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 21-Jul-16 13:32pm.
|
|
|
|
|
John Simmons / outlaw programmer wrote: And why aren't you working on my latest feature request?
He's waiting for the "?"
Mongo: Mongo only pawn... in game of life.
|
|
|
|
|
John Simmons / outlaw programmer wrote: On the "?" operator - I will strive NEVER to use that. Why?
|
|
|
|
|
Because it obfuscates the code, and because I'm not yet coding in the appropriate version of .Net.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Don't turn a nice language into an on-error-resume-next monster.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I totally forgot about that!
How about:
#pragma on error resume next
Mwahaha
cheers
Chris Maunder
|
|
|
|
|
Who are you, and what have you done with that nice, wholesome Mr. Maunder?
Software Zen: delete this;
|
|
|
|
|
How about a faux-extension method:
string result = DodgyApi.GetValue().OrDefault();
Then we can use it with LINQ as well to get rid of the ridiculousness of Single()/SingleOrDefault(), First()/FirstOrDefault()
edit:
After thinking about this a bit more, instead of syntactic sugar, isn't this one of the few raison d'être for the adapter pattern?
class DodgyApiAdapter: IDodgyApi {
string GetValue() {
try {
string result = _dodgyApiImpl.GetValue();
return result;
}
catch {
return null;
}
}
}
string result = dodgyApiAdapterInst.GetValue();
I mean if you're doing the empty-catch-returns-null pattern for an API enough to want syntactic sugar, then isn't an adapter warranted?
modified 21-Jul-16 15:35pm.
|
|
|
|
|
Headsplode does sound horribly like On Error Resume Next ...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
You could always write a helper method.
string GetString(int x)
{
throw new NotImplementedException();
}
void Foo()
{
string s = NoEx.Run(() => GetString(100));
Console.WriteLine(s == null);
}
class NoEx
{
public static T Run<T>(Func<T> method)
{
try
{
return method();
}
catch
{
return default(T);
}
}
}
Not as clean as syntactic sugar, but fairly close
|
|
|
|
|
|
If you make Run an extension method and rename it to IgnoreExceptions for clarity, you can say:
var s = (() => GetString(100)).IgnoreExceptions();
You can go one further and add another parameterized type for the catch's use to only ignore certain types of exceptions:
var s = (() => GetString(100)).Ignore<ArgumentException>();
|
|
|
|
|
Very interesting Nish,
I was puzzled by the omission of a Type argument to NoEx.Run, and realize that my habit of always writing out the Type arguments wasn't necessary in this case. I've made a note to try and find more information on exactly when the compiler can infer the Type which renders including the Type an option.
thanks !
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
As an idea I despite code that swallows exception without trace...
Also null sometimes your best friend (but not default), so why to eliminate!?
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: I despite code
Quick Nitpick: that should be 'I despise code'. Despite means 'without being affected by; in spite of.', where despise means 'feel contempt or a deep repugnance for.'
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
|
To those of who still use pointers (even in .Net), using (*) might be a little confusing but I have an idea.
How about using a construct similar to the for loop
for (int i = 0; i < limit; ++i) { ... }
NoThrow (var <out>; Func<T>; <result on throw>);
string result;
NoThrow (result; dodgyApi.GetValue(); "I.M.Foo.Bar");
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Too explicit. We need something that truly, deeply hides what's going on
cheers
Chris Maunder
|
|
|
|
|
If that's the end goal, just use a carrot (^) instead of equals. That way anyone the uses managed C++ is really hosed.
string result ^ dodgyApi.GetValue();
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Foothill wrote: To those of who still use pointers (even in .Net), using (*) might be a little confusing
And to those of us who use multiplication.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"new" doesn't mean what you think it does.
|
|
|
|
|
Just something along the lines of a TryGetValue<T> Extension Method. :shrug:
|
|
|
|
|
You get my up-vote for the idea, but making the idea more general-purpose means, imho, not being able to use an Extension method with generics, since the 'this parameter of an Extension method cannot be declared 'ref, or 'out.
How about this (based on the code example in my previous reply to this thread)
using System;
namespace InMemoriamMaunder
{
public enum DodgyResult
{
ResultNull,
ResultOkay,
ResultError
}
public static class DodgyUtilities
{
public static DodgyResult TryGetValueFromDodgy<T1,T2>(T1 t1, ref T2 t2, Func<T1,T2> func)
{
try
{
t2 = func(t1);
if (t2 == null)
{
return DodgyResult.ResultNull;
}
else
{
return DodgyResult.ResultOkay;
}
}
catch (Exception)
{
return DodgyResult.ResultError;
}
}
}
}
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
No programming questions in the Lounge!
Chris Maunder wrote: Or am I setting a new standard for lazy, shameful programming here this hot, lazy afternoon?
Surely you jest! It's only 117° today, but we're supposed to warm up for the weekend.
Will Rogers never met me.
|
|
|
|
|
how about setting that as default for the whole app... you could use a constant like #ON_ERROR_RESUME_NEXT = true;
|
|
|
|