Thursday, October 31, 2013

var object

Last week we were doing some mob programming and a co-worker was telling the person doing the typing to type the following:

var object ...

Now, this obviously doesn't work and intellisense will yell right away, but we all knew that he did not mean what he said.  Either way, it was funny at the time and we got a laugh out of it.

After that meeting, I was working with another co-worker and we were doing some peer programming. One of the statements that we wanted to write something like the following:

var event = GetEvent();

Now, event in this case may be a sporting event so it would make sense to call it event.  The issue is that event is a C# keyword and cannot be used like this.  My question in both examples above is what if I want to call it an event and use event as the name?  The answer is to use the @ symbol in front of it.  For example the following would make the statement legal and will compile.

var @event = GetEvent();

Doing something like this in my opinion, as well as others, is bad practice and should not be done.  If you are wondering what we did, we just changed event to eventRecord and wrote it as follows:

var eventRecord = GetEvent();

Now, most normal people will stop there and call it a night.  I decided to do a few other tests with the var keyword, which after my tests make me think var isn't really a keyword, but some sort of almost keyword.  So, I did the following which is simple and straight forward:

var i = 5;
Console.WriteLine(++i);

For fun, I then changed it to the following and thought it would for sure blow up:

int var = 5;
Console.WriteLine(++var);

To my surprise, it compiled fine and ran fine.  This confused me and I thought it for sure was a bug or something else was wrong.  So, I decided to try something else.  What would happen if I changed int to var and wrote the following:

var var = 5;
Console.WriteLine(++var);

Would this work?  Should it work?

Well, it does work and it may have to do with some sort of backwards compatibility.  Since the var keyword was introduced in C# 3.0, previous code could have something like the following:

int var = 1;

This sort of makes sense because the assembly that is created will change the var to int.  The var is not included in the final assembly code and is just a C# thing.  I will provide another post about what your code looks like after it is compiled and then decompiled in the next couple days.

What's the point of all this?  Nothing, or maybe just to get me, or you, to think about things in more detail and share that knowledge.  Other than that, I think all my code will start off with var var ... just to see if someone complains and tells me to change it.

peace yo!

Monday, October 28, 2013

Bookshelf

Last month, I started a new journey with a new company after being with another company for just shy of four years.  I remember my first few days or so with my previous employer and I remember making separate trips to my car to get some books that I as a developer thought I needed to perform my job effectively and efficiently.  Looking back at those four years, I cannot remember opening any of those books for any of the technical knowledge in them.  I've been at my new company for a little over a month now, and I have yet to bring any of those books to my new cubicle.  The only thought that I had of bringing them was to use them as a monitor stand like many other developers as well as myself who are too cheap to buy a real stand.  One could argue that the need to read a 500 to 1000 page book of if statements is not a need anymore because of the amount of articles, blogs, and videos on the internet.  This statement is false in my opinion and developers should be reading more books than they currently do.  The art that puts chapters full of technical knowledge is getting lost and we may suffer from it.  It is similar to how musicians no longer have to think too much about how their songs tie together in their albums.  We live in a text message/youtube/itunes/google world where texting, watching short videos, downloading one song, and skimming articles is the norm.  With all that said, there is still a need for books, maybe not physical, but books that you can read online.

The way that I continue to read technical books is by subscribing to Safari Books Online.

By doing this, I have replaced this bookshelf:

Physical bookshelf in garage above toolbox
With this bookshelf:

Virtual bookshelf within Safari Books Online

A few great features of Safari Books Online that I like are as follows:


1.  Notes/Tags/Highlighting - You are able to create notes by selecting text within the book.  These notes are available even after you remove your book from the bookshelf.


2.  Bookmarks - You can add bookmarks to the books you are reading.  If you remove a book from your bookshelf, the bookmark will not go away and will stay there if you add the book back to your bookshelf later.


3.  Great Selection - There is a great selection of books new and old.  The following is a book that I plan on reading soon that a co-worker recommended.  I can't remember searching for a book that wasn't available on Safari Books Online.


4. Price - The price is ridiculous, but in a good way.  You can start a free 10 day trial and after that it starts out as a 10 book bookshelf at $19.99.  You can change that like I did to a 5 book bookshelf for $9.99, but if you want to go back to a 10 book, you may have to pay $27.99 for it.  For me, the 5 book is perfect. 



5. Mobile - Everything you do is available on your phone.  


6.  Videos -  I haven't watched any videos yet, but there are some videos to choose from. 


If you are in a technical position and haven't sign up yet or thought about signing up, then you may not be smart. 

peace yo!

Saturday, October 26, 2013

Test Method Snippet (Visual Studio)

If you practice Test-driven Development (TDD) you know there are many standards and patterns that you can use.  One pattern that myself and others on my team like is the Arrange, Act, and Assert pattern when writing our tests.  This pattern is included in the existing tests for you when you create a new ASP.NET MVC 4 Web Application and then select the option to Create a unit test project to it.  What I noticed is that when I use a snippet to create a new test method, the Arrange, Act, and Assert comments are not there.  This is not a big deal to most as they can remember to Arrange, Act, and Assert, but I would like the comments added to my new test methods when I use a snippet.  Along with not liking that my test methods do not have the comments, I also realized that the name of the new test method of MyTestMethod is dumb and should be changed to our naming standards which came from Roy Osherove.  The naming standard we agreed on is [UnitOfWork_StateUnderTest_ExpectedBehavior] and an explanation of this can be found at Roy's site here.  Making these changes in the snippet is simple and the steps to accomplish it are below.

1.  Open Visual Studio.

2.  Go to Tools -> Code Snippets Manager.


3.  Change the Language to Visual C# and expand the Test folder.

4.  Copy the path located in the Location.

5.  Open Windows Explorer and browse to the location copied in the previous step.

6.  Make a Copy of the testmethod.snippet. 

7.  If you want to still use the original snippet in Visual Studio, rename it by adding a prefix.  If you do not want to use the original snippet, you need to make sure it does not end in .snippet and append something to it or change it.  I prefer to keep it so others that are not aware of my new snippet can still use the old one, so I added a prefix of "Orig_".


8.  Open testmethod.snippet in notepad, Visual Studio or any other program that will allow you to edit text.

9.  Change the text within the Shortcut element from testm to testmm.  The reason for the second 'm' is that there are other methods that may show up from IntelliSense and adding the second m will eliminate them from showing up and the snippet will be invoked by typing testmm followed by a tab.

      old: <Shortcut>testm</Shortcut>
      new: <Shortcut>testmm</Shortcut>

10.  Change the text within the Default element from MyTestMethod to to UnitOfWork_StateUnderTest_ExpectedBehavior.

      old: <Default>MyTestMethod</Default> 
      new: <Default>UnitOfWork_StateUnderTest_ExpectedBehavior</Default>

11.  Add the following three lines of comments to the method just before the text  $end$ within public void $name$()
         
          // Arrange          
          var x = false;
          var y = new y();
          
          // Act          
          x = y.DoStuff();
          
          // Assert          
          Assert.IsTrue(x); 


12.  Save the file.

13.  Close all instances Visual Studio if there are any open.

14.  Open a Unit Test Project within Visual Studio and set your cursor within a test class.

15.  Type testmm followed by a [tab].  As you can see, the snippet will now display the following which has our new method name and the comments to help us create good tests.





Hopefully you learned something from this post and it will help you modify or add more snippets to help you code faster and better.

peace yo!