tag:blogger.com,1999:blog-52525324263165960192024-03-13T16:53:58.472-07:00It keeps me awakeThoughts about hmm... well... everything I guessAvi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-5252532426316596019.post-36327476576402045602010-07-04T03:34:00.000-07:002010-07-04T03:34:25.322-07:00What type of a developer are you?It has been a while since my last post.<br />
I've been busy in lots of stuff like: new job, new baby... you know, the usual stuff :)<br />
<br />
Anyway, Just wanted to share this <a href="http://www.webdesignerdepot.com/2010/05/7-personality-types-of-developers-today/">cool link</a> that presents the developer types as he sees them. I must say, I tend to agree with most of them and I see some of myself in each and every one of them (in a positive way of course).<br />
<br />
I think I have known at least some of the described characters in real life and may have interviewed some of the rest.<br />
<br />
The SWAT member, he will make it happen no matter what. on the way he will fall and climb out of every possible trap a decent developer will avoid.<br />
<br />
The bullshit artist :), he knows the right words and when to say them but once you scrap the surface you discover the bright color of nothing.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-54095632771985645452010-01-04T13:59:00.000-08:002010-01-04T14:00:01.051-08:00Learn SCRUM in under 10 minutesI've just watched a great video on the <a href="http://www.agile-community.com">Agile Community</a> and thought I'd share it. The video claims to explain what SCRUM is in under 10 minutes.<br />
Well, it does actually. it presents all of the key concepts of SCRUM in a light & funny way. However, my guess is that if you are not a SCRUM savvy you might get overdosed from all of that "This is best served in a 3 days course along with 6 months practice stuffed in 8 minutes" new buzzwords.<br />
<br />
Enjoy!<br />
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/Q5k7a9YEoUI&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="never"></param><embed src="http://www.youtube.com/v/Q5k7a9YEoUI&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="never" width="560" height="340"></embed></object>Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-56550300150486854202009-12-23T03:24:00.000-08:002009-12-23T03:31:06.526-08:00Useful Extension methodsI've posted in the past about the hazards in extension methods and how they can be easily abused. there are plenty of links out there specifying the cons of using this feature. <br />
However, since the title got you in here, I'm assuming you are still interested.<br />
I wanted to share some of the most common extension methods I use when writing code.<br />
<br />
IsNullOrEmpty - I think we all know this method from the String class. Why not have the same capability on collections?<br />
I did see plenty of samples on the web showing how to add this method to collections or arrays. <br />
things like:<br />
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> IsNullOrEmpty(<span style="color: blue;">this</span> <span style="color: #2b91af;">ICollection</span> collection)</pre><pre style="margin: 0px;">{</pre><pre style="margin: 0px;"> <span style="color: blue;">return</span> collection == <span style="color: blue;">null</span> || collection.Count == 0;</pre><pre style="margin: 0px;">}</pre></div><br />
This is all fine and will return True if the collection/Array are null or empty.<br />
But hey, what about some other, non "<i>ICollection</i>" objects that implements IEnumerable like XmlNodeList?<br />
For that purpose, we can't use the <i>Length</i> or <i>Count</i> properties since we don't have them. <br />
So, let's work with the <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx">IEnumerator</a>.<br />
Initially, the enumerator is positioned before the first element in the collection so calling <i>Current</i> will throw an exception. By calling <i>MoveNext</i> we move the enumerator to the first element and the bool value will tell us if it is empty or not. Complicated?<br />
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> IsNullOrEmpty(<span style="color: blue;">this</span> <span style="color: #2b91af;">IEnumerable</span> iEnumerable)</pre><pre style="margin: 0px;">{</pre><pre style="margin: 0px;"> <span style="color: blue;">if</span> (iEnumerable != <span style="color: blue;">null</span>)</pre><pre style="margin: 0px;"> {</pre><pre style="margin: 0px;"> <span style="color: blue;">return</span> !iEnumerable.GetEnumerator().MoveNext();</pre><pre style="margin: 0px;"> }</pre><pre style="margin: 0px;"> <span style="color: blue;">return</span> <span style="color: blue;">true</span>;</pre><pre style="margin: 0px;">}</pre></div><br />
Now, since collections and arrays implements IEnumerable we are clear to go.<br />
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)</pre><pre style="margin: 0px;">{ </pre><pre style="margin: 0px;"> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> list = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();</pre><pre style="margin: 0px;"> <span style="color: #2b91af;">Console</span>.WriteLine(list.IsNullOrEmpty());<span style="color: green;">//This will print True</span></pre><pre style="margin: 0px;"> list.Add(<span style="color: #a31515;">"Some text"</span>);</pre><pre style="margin: 0px;"> <span style="color: #2b91af;">Console</span>.WriteLine(list.IsNullOrEmpty());<span style="color: green;">//This will print False</span></pre><pre style="margin: 0px;">}</pre></div><br />
But wait, didn't I say "some" extension methods?<br />
Another one I use when I'm writing console applications is:<br />
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Print(<span style="color: blue;">this</span> <span style="color: #2b91af;">Object</span> obj)</pre><pre style="margin: 0px;">{</pre><pre style="margin: 0px;"> <span style="color: #2b91af;">Console</span>.WriteLine(obj);</pre><pre style="margin: 0px;">}</pre></div>I use this trick when I need to change the output target according to configuration or command line input.<br />
The result will look this:<br />
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)</pre><pre style="margin: 0px;">{ </pre><pre style="margin: 0px;"> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> list = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();</pre><pre style="margin: 0px;"> list.IsNullOrEmpty().Print();<span style="color: green;">//This will print True</span></pre><pre style="margin: 0px;"> list.Add(<span style="color: #a31515;">"Some text"</span>);</pre><pre style="margin: 0px;"> list.IsNullOrEmpty().Print();<span style="color: green;">//This will print False</span></pre><pre style="margin: 0px;">}</pre></div>Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-953152241266790352009-12-14T14:39:00.000-08:002009-12-14T14:41:40.424-08:00Bad namesI've recently done code review for someone and stumbled on a GeneralUtils.cs file I didn't see before. <br />
"OMG!!!" I said "it is <i>general</i> for sure, and provides some <i>utils</i> right? what kind of <i>utils</i>?"<br />
<br />
Him: Grinning as if his hand was caught in the cookie jar. "A bunch of things related to X,Y and Z objects"<br />
Me: "Well, X Y and Z handles 3 different things right?"<br />
Him: "Yes but I didn't think they deserve an independent class"<br />
<br />
The name is bad for sure and I got no objection on that. the thing is, it hides a much bigger problem. The object is doing too many things instead of focusing on just one. This means that the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single responsibility principle</a> was violated.<br />
I usually have a small question that clears the issue in a second. ask yourself what does the class/method is doing. if the answer is "It does x <b>AND</b> y" then a re-factor session should visit your code.<br />
<br />
Him: "I see your point. I'll split them to 3. I thought of calling the first one XHelper"<br />
Me: "Sounds good. this means that XHelper will help the X class right?"<br />
Him: "No, I don't have an X class" <br />
Me: "Let's suppose you have an X class, X will call XHelper right?"<br />
Him: "Yes. and?"<br />
Me: "So, if the helper is doing all the work it would make more sense to move it into X. if it doesn't, please give a more descriptive name"<br />
Him: "But I don't have an X class!!!"<br />
Me: "Even better! who are you helping?"Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-76366734144944795582009-05-18T13:37:00.000-07:002009-05-18T13:42:56.297-07:00Visual Studio 2010 beta 1 is here!Well, its about time! the beta version is now available for MSDN subscribers.<br />They also said that it will be publicly available on Wednesday for all non MSDN users.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-85853354489141174162009-04-26T08:26:00.000-07:002009-04-26T08:30:47.082-07:00C# 4.0 - Collection of linksI've just came across this great post at <a href="http://bogdanbrinzarea.wordpress.com/2009/04/24/learning-net-40-new-features/">Bogdan Brinzarea’s blog.</a> <br />The post contains plenty of links on each of the new features in the new framework.<br /><br />Enjoy!Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-86524865777667878512009-04-24T03:54:00.000-07:002009-04-24T04:03:27.058-07:00//Magic. do not touchI've stumbled upon this hilarious post in <a href="http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered">stackoverflow</a> under the title of "What is the best comment in source code you have ever encountered?".<br /><br />As the title implies, this was my favorite!<br />I also like this one as well :)<!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br />// <br />// Dear maintainer:<br />// Once you are done trying to <span class="str">'optimize'</span> <span class="kwrd">this</span> routine,<br />// and have realized what a terrible mistake that was,<br />// please increment the following counter <span class="kwrd">as</span> a warning\<br />// to the next guy: <br />// total_hours_wasted_here = 16<br />//</pre>Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-83244660259658246752009-04-23T14:17:00.000-07:002009-04-23T14:46:03.553-07:00Find a key by its value in a NameValueCollectionWhile I was working one of my samples, I came across a nice problem: "How do I get a key from a NameValueCollection by its value?"<br />Assume the following declaration:<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br />NameValueCollection nvc = <span class="kwrd">new</span> NameValueCollection { { <span class="str">"a"</span>, <span class="str">"abc"</span> }, { <span class="str">"b"</span>, <span class="str">"bcd"</span> }, { <span class="str">"c"</span>, <span class="str">"cde"</span> } };<br /></pre><br />Well, I can try to explain why I needed it but I assure you it wasn’t so important. In fact, the only added value of this sample was me finding a nice solution to this problem :)<br /><br />Back to the point, there was, of course, the strait forward solution of iterating on the list and checking for a match on each value but that was too ugly even for a sample.<br />So, why not recruit LINQ to the rescue?<br />A small attempt made me realize that though the collection implement IEnumerable, the enumeration works only on the keys so still no cookies for me.<br /><br />Couple of more hits on the head with a club made me remembered that I have Extension methods in my arsenal. I know I had previously warned about in this <a href="http://aviha.blogspot.com/2009/02/respect-your-extension-methods.html">post</a>.<br />Well, I guess it can now come in handy.<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> SampleExtensions<br />{<br /><span class="kwrd">public</span> <span class="kwrd">static</span> IEnumerable<KeyValuePair<<span class="kwrd">string</span>, <span class="kwrd">string</span>>> GetPairs(<span class="kwrd">this</span> NameValueCollection coll)<br />{<br /><span class="kwrd">if</span> (coll == <span class="kwrd">null</span>)<br />{<br /><span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"throw something here"</span>);<br />}<br /><span class="kwrd">return</span> coll.Cast<<span class="kwrd">string</span>>().Select(key => <span class="kwrd">new</span> KeyValuePair<<span class="kwrd">string</span>, <span class="kwrd">string</span>>(key, coll[key]));<br />}<br />}<br /></pre><br />Nothing fancy here. The cast was done so I could use the select method.<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br />var result = (from pair <span class="kwrd">in</span> nvc.GetPairs() <span class="kwrd">where</span> (<span class="kwrd">string</span>.Compare( pair.Value,<span class="str">"abc"</span>)==0) select pair.Key);<br /><br />result.ToList<<span class="kwrd">string</span>>().ForEach(str => Console.WriteLine(str));<br /></pre><br />Now for the usage. The search is now done on a KeyValuePair and now I have access to the value in each pair.<br />The last line prints the result to the console and there you have it.<br /><br />Ugh!<br />I'm sure there was a good reason why this feature was left on the drawing board.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-77832697140180090842009-03-19T06:57:00.000-07:002009-03-20T09:13:50.242-07:00ASP.NET MVC release version is now live!Alas!<br /><br />MVC 1.0 release version was finally published and can be downloaded <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en">here</a>.<br /><br />This came as a surprise since the RC2 version was only published couple of weeks ago and the release version was quite silent. <br />The changes included in the RC2 where a bug fix and a JQuery version upgrade so no broken APIs should be expected.<br /><br />While you are on it, I suggest downloading this free chapter of <a href="http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf">MVC book</a>.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-14751169349321861012009-03-13T13:51:00.000-07:002009-03-13T14:12:31.329-07:00Visual Studio 2010 CTP expiration problemCouple of months ago, Microsoft published the Visual Studio 2010 CTP version at <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&displaylang=en">this link</a>. The files total size was a whopping 7GB. when extracted it took another ~24GB of space. the reason is that it contains a full Virtual pc environment running Windows server 2008 and VS2010 already installed.<br /><br />The problem was that on the first of January 2009 the version had expired and couldn't be used any more. <a href="http://blogs.msdn.com/jeffbe/archive/2008/12/09/dealing-with-the-team-system-2010-ctp-expiration.aspx">Jeff Beehler </a>posted a temporary solution in his blog on how to override this problem in case you need more time to evaluate it. Remember, do the change before you open the VS2010.<br /><br />BTW, this will also solve the nagging windows activation message.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-47558775650080768062009-02-26T00:12:00.000-08:002009-03-03T07:01:34.157-08:00Visual Studio 2010 is having a new lookJason Zander, the General Manager for Visual Studio, presented in his <a href="http://blogs.msdn.com/jasonz/archive/2009/02/20/a-new-look-for-visual-studio-2010.aspx">blog </a> some more details and pictures about Visual Studio 2010.<br /><br />I'm sure that there is more into it then meet the eye but what I really liked in the update was the new Extension manager. The tool will be able to browse for third party templates and tools online and to install them in your environment. Cool!<br /><br />The Library can be browsed at <a href="http://www.visualstudiogallery.com/">Visual Studio Gallery</a>.Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0tag:blogger.com,1999:blog-5252532426316596019.post-59005545215156146012009-02-23T01:12:00.000-08:002009-03-03T07:07:21.509-08:00Integrate ASPX compilation into Visual StudioOne of the principles I insist on is to always try my build on a clean environment and simulate installation as if it was done by QA. Lately I've noticed that I keep falling on errors hidden deep in the aspx files. So, why not using the old and familiar aspnet_compiler.exe tool? It is located at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 and it is quite easy to use.<br /><br />As a VS add-ins freak I've started thinking on an amazing add-in that will integrate to the VS and will listen to build events and display the results at the output pane. Heck, why not add some coffee serving capabilities?<br /><br />It took me about 10 minutes of googling to stumble on this <a href="http://mikehadlow.blogspot.com/2008/05/compiling-aspx-templates-using.html">blog</a>. Mike Hadlow had a genius in its simplicity idea. Use the POST BUILD EVENT!<br />All I need to do is put the following line in the post build event: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v / -p "$(ProjectDir)\"<br /><br />Now, All that is left is to make the process of adding this line to each and every web project in our team to be automatic.<br /><br />I have just the add-in for that :)Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com1tag:blogger.com,1999:blog-5252532426316596019.post-85424013368724617722009-02-21T14:11:00.000-08:002009-03-09T07:04:01.786-07:00Respect your extension methods<p>One of the nice things in C# 3.0 is extension methods. I won't go into details about it but would like to focus on the hazards when actually writing beyond the plain examples.<br />However, I will use a plain example myself since I don't want you to fall asleep.<br /><br />Since the method binding is done at compile time, you will get a different behavior depending on the assigned type.<br /><br />Suppose we have 2 (plain) objects:<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">public</span> <span class="kwrd">class</span> BaseObject<br />{ }<br /><span class="kwrd">public</span> <span class="kwrd">class</span> DerivedObject : BaseObject<br />{ }<br /></pre><br />Now for the extension method:<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> ObjectExtensions<br />{<br /> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ExtensionMethod(<span class="kwrd">this</span> BaseObject obj)<br /> { <br /> Console.WriteLine(<span class="str">"In BaseObject extension version"</span>); <br /> }<br />}<br /></pre><br />The Main method is very simple:<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)<br />{<br /> BaseObject baseObj = <span class="kwrd">new</span> BaseObject();<br /> DerivedObject derObj = <span class="kwrd">new</span> DerivedObject();<br /> baseObj.ExtensionMethod();<br /> derObj.ExtensionMethod();<br />}</pre><br />I've declared 2 objects and when I run this sample I get:<br />In BaseObject extension version<br />In BaseObject extension version<br /><br />So far so good; 2 object, 1 extension and 1 expected result for both calls.<br />Remember the compile time binding thing? Lets add the another version for the extension method. It goes like:<br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ExtensionMethod(<span class="kwrd">this</span> DerivedObject obj)<br />{<br /> Console.WriteLine(<span class="str">"In DerivedObject extension version"</span>);<br />}</pre><br />Now, when running this sample again, I get:<br />In BaseObject extension version<br />In DerivedObject extension version<br /><br />"Is he ever going to get to the point?"<br />Well, the point is this: add these 3 lines to the main method and see what happens.<br /><span style="color:#339999;">BaseObject</span>[] arrOfObj = { baseObj, derObj };<br />arrOfObj[0].ExtensionMethod();<br />arrOfObj[1].ExtensionMethod();<br /><br />We are back to BaseExtension for both new calls even though one of them is a derived type.<br /><br />The danger is obvious. It is very easy to keep your gourds down and write some buggy lines of code that will make you bold trying to figure out what the heck went wrong.<br />Of course, there are other words of caution in this subject:<br /><br />When it comes to your extension method versus the original object version, yours will always loose. The extended version will never be called.<br />"Why the heck would you extend a method that already exists?"<br />Well, the first option is that it didn't exist at the time and was added to the object at a later point of .net life. You know, great minds think alike.<br />The second option is that I tried to override the original behavior. No treats for me in both poor scenarios!<br /><br />"Well, am I at list entitled to access some private fields of the extended object?"<br />Hmm, no! You see, extension method is just a way to call a static method from the instantiated object.<br /><br />You will find many great examples on how to do nifty things with extensions.<br />My advice is to avoid it if you can inherit and add it in the derived object. In case you can't or don't have access to the code then use it with care.</p>Avi Harushhttp://www.blogger.com/profile/01988408536807274339noreply@blogger.com0