TAG | Programming
So Jquery is going to become part of Asp.net MVC. First off, congrats to the Jquery team. They’ve put out a really awesome product. Second, congrats to Microsoft for catching every .Net developer completely by surprise, proving, again, that they are listening to the community.
I never really got into the microsoft javascript libraries that shipped with the ajax control toolkit because I found that I could do things much faster using external javascript libraries .
It will be interesting to see what Microsoft contributes back to the Jquery community in later updates to the ASP MVC product.
.NET · asp.net · jquery · MVC · Programming
The prototype javascript library has a class (Try) and function called these(). This function accepts an array of functions as it’s sole argument and it will execute each function in the array, in the order they are added, and return the result from the first function that executes without error. If none of the functions executes successfully undefined is returned.
For example:
var i = Try.These(
function() { return 9 / 0; },
function() { return 1; }
);
If we ran the sample above i would equal 1 because the first function would encounter a division by zero error. However:
var h = 0;
var i = Try.These(
function() { return 9 / h; },
function() { return 1 / h; }
);
i will equal undefined in this example.
With the .Net framework’s generics library we can achieve roughly the same results. We won’t be able to assign an undefined value to the result but we can play around with the default keyword. :D
public class Try
{
public static T These(params Func[] delegates)
{
for (int i = 0; i <= delegates.Length - 1; i++)
{
try
{
return (T)delegates[i]();
}
catch
{
}
}
return default(T);
}
}
So for example:
static void Main(string[] args)
{
int i = 0;
int y = Try.These(
delegate() {
int x = 0;
return i / x;
},
delegate() {
return ++i;
});
Console.Write("y is " + y.ToString());
}
Will output
y is 0
.NET · c# · Programming · prototype · try.these() · try/catch
I was browsing around Code Project the other day, I subscribe to their most recent submissions feed to watch for interesting samples, and I saw a unique take on a logging system.
It used IL replacement, much like TypeMock Isolator, to allow developers to enable/disable the logger without having to compile any code.
In theory, I haven’t tried it out yet, you would be able to add this logging system to any project without coding anything.
I downloaded the code and it’s huge. 5 assemblies, over 2000 lines and there is some COM stuff in there. I notice two things. First it’s all in c++. This is not a bad thing it’s just not my first language.
So I start looking around for some comments to let me know what the heck is going on in this code.
Annnnnnd… hmmmm thats odd.
There are no comments. Not one. I checked the author notes on the original submission article to see if this was an error and there was an updated link.
Nope. Nothing. It takes me a minute but near the bottom of the post I see why there are no comments.
The author writes:
“I like to think I write self-documenting code so I tend to not have any comments in my code.”
I instantly deleted the code from my system. My thought is if the author of this code didn’t think it was worthy of some kind of explanation then I am certainly not going to waste my time with it.
Comments aren’t just some “nice to have” thing. They show that you actually care about what you are putting out there and they show your thought process throughout the codes execution. If you don’t care about that then why would I?
I’m not saying that every line, function call and variable needs a comment but any programmer worth his salt should put some form of documentation into his/her code. It’s a sign of respect.
So my fellow developers let’s respect each other enough to document what we put out into the world. What do you say?
wrote a great article on best practices and how “best practice” is essentially a bunch of bullshit:
About the only thing [Best Practice] means is ‘I have found that doing XYZ in ABC way works the best for me’. But just because it works for you, or even works for a group of people that does not mean it is ‘Best Practice’.
I’ve never really thought about it but this is 160% correct. Even now, at my current job we are reviewing Spiral, Agile, TDD and other SDLCs in order to conform to “best practices”.
Thankfully my boss understands that what works for some may not work for all and if something isn’t working we won’t have time to try to screw with it. We’ll have to move on and find something that fits better. This is exactly how “best practices” should be treated. As suggestions on how to handle software development.
There is no “one ring to rule them all” in our world. There are many rings each has a different fit to it and needs to be tried on to see where it fits best.
When you need to find out the type of an object in JavaScript you would just do the following:
[sourcecode language='jscript']
var x = 1;
alert(typeof x);
[/sourcecode]
Well what if x were an array? With x as an array a typeof incorrectly returns “object” as the type. Well that is just plain wrong. Enter the isArray() method.
[sourcecode language='jscript']
Object.isArray = function(o) {
return o && o.constructor === Array;
};
[/sourcecode]
Granted it’s not as simple as typeof but it does correctly identify array objects and is something every JavaScript developer should have in their code garage.
