.Net Source Code

by Lord Cod3n 28. September 2008 07:28

I came across a blog post.  The post provided a link to a tool you could use to download the available .net source code.  Microsoft allows you debug into the framework.  If you are like me you may just want to be able to browse the source code at your leisure.  Here is a tool that will download the the pdb and corresponding source code files. 

http://www.codeplex.com/NetMassDownloader

 

Here is the command line I used to download the source files:

 

PS C:\SRC\NetMassDownloader\Debug> .\NetMassDownloader.exe -d 'C:\Program Files\Reference Assemblies' -o C:\src\symbols\
MassDownload

The PS prompt is from powershell which is not required, but I love it.

Happy Coding

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Advice from an Elegant Coder

by Lord Cod3n 26. September 2008 06:40

Here is some advice/comments from Tony Rasa who can be found at: http://elegantcode.com/about/tony-rasa/.  I misused the term refactoring.  I kind of realized that after it was pointed out.  Here is what Tony had to say:

 

Glad that Red/Green/Refactoring is starting to make sense for you.

You might want to look at the definition of 'refactoring' a little more closely however - "To re-write existing source code in order to improve its readability or structure without affecting its meaning or behavior" - so "refactoring" to add in the implementation of your test body is not truly 'refactoring,' you're still getting to green.

refactoring is realizing that your implementation makes sense as its own method, so you move the code out to its own private method and replace that space with a call to your newly minted method -- no change in behavior, the test still passes, but you've improved the structure of the code.

Tony

 

My response was along the lines of when do I write the real code ?  His response to that question was as follows:"

you can think of the 'not compiling' step as an "extra red," where you figure out what you'd like the usage to look like. Sometimes this is easy to skip.

Then you have a

'compiles-but-throws-notImplementedException-or-otherwise-returns-the-wrong-values'

step, still red. the test fails because you haven't implemented it yet.

Then you implement 'just enough' to pass the test, where 'just enough'

is sort of tricky to figure out but at first err on the side of "as little code as possible." this is where you make the code work for real.

and now you're green.

now, look at the code you wrote, and consider the next place you need to go with the code & tests - is it in the right direction? if theres something that isn't, refactor (changing structure but not behavior) - and you can re-run the tests to make sure you don't break anything while you're refactoring. (thats the value of the tests - they remove some of the risk)

now that you've got this test case done, move on to the next test (red) etc etc.

Excellent advice.  I am going to chew on this a bit while I take a break.  I will post up more later.  Thanks Tony for taking the time to respond.  You have been very helpful is correcting my off path wanderings, and thus probably saved me a ton of reading.

 

Code Happy

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

TDD Update w/Breakthrough

by Lord Cod3n 26. September 2008 05:01
Technorati Tags: ,,,

Last night I had an AH-HA moment.  It finally hit me and made sense.

 

1.  Red

2.  Green

3.  Refactor

 

Sounds obvious, but I really was not thinking about that part of the TDD process correctly.  First, the test needs to fail.  Write the unit test the way you want to read it. I think this is a critical step when you first start out.  Even if you know it wont work, you want to validate your assumption.  Second, you need to make the test work.   Get the code to compile.  Once it is green, you begin refactoring it to work correctly.  This is where you would add the actual logic and make your database calls.

My current project required me to read from disk a certain cs file in a test directory.  The test class had one method.  I made the test work by hard coding the 1.  That is how I was able to get to green quickly.  Next in the refactoring step, I actually added the code.  The code uses a regular expression parser that I am writing from scratch.  While I was implementing the logic, I kept my eye out for design changes and duplicate code I could remove.  These refactoring steps need to be related to the current test.  If the refactoring does not relate to the current test, then you need to add them to the test list.  Stay focused.  I know developer ADD can be bad.

I have to say again, it is a pretty awesome feeling knowing you can go in there rip out the guts, delete methods, rename things, and you have backup to tell you when you messed up.  It worked the way it was supposed to before you started.

I will keep adding posts as I move through the phases of this project.  It is requiring a large amount of research to find better methods.  I am going to keep the posts fairly frequent.  This is both for a learning perspective, and so I do not forget to document any hurdles.  I love AH-HA moments.

 

Code Happy,

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Var... What about it?

by Lord Cod3n 24. September 2008 16:43

According to MSDN, the definition of var is...

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

I find this interesting.  When I was first exposed to the var keyword, I mistakenly assumed it was for use with just LINQ queries like the code below:

 

        private void ExampleMethod()
        {
            var file = from d in System.IO.Directory.GetFiles("c:\\")
                       select d;
        }

 

Var is of type IEnumerable in the case above.  As it turns out, this is not the only use for var.  It can be used for any locally scoped variable. 

This is something as a former VB6 programmer I shudder to think about.  VB6 had a variant data type.  This was not only a bad idea, but it was horribly misused by inexperienced programmers.  Just to cover all the bases, it was bad for reading the code, and it was bad for performance.

The var data type in C# is different, it is not like a variant at all.  It is assigned the variable type from the result of an assignment.  Lets run a test:

        public static void TestMethod1()
        {
            var isAString = "ABC";
            Console.WriteLine(isAString.GetType().ToString());
        }

 

I would like to point something out.  Intelli-sense treated the isAString variable as a string as I was typing the Console.WriteLine function call arguments.  I am expecting to see String output to the Console.  It said System.String but close enough ;).  This tells us the variable is strongly typed.  The compiler inferred its type from the assignment.  This makes for some good variable declaration short cuts.  I am not sure if that is a great idea,but  If you couple that with a good variable naming conventions you might be ok.

Now let's try another test:

        public static void TestMethod1()
        {
            var isAString = 1;
            isAString = 2.02;
            Console.WriteLine(isAString.GetType().ToString());
        }

 

That code would not even compile.  In case you don't see it right away, I have created a var of type int and tried to assign a double value to it.  Let's see if this will work...

        public static void TestMethod1()
        {
            var isAString = 5/7;
            Console.WriteLine(isAString.GetType().ToString());
        }

 

I am not so sure I like this, no visible compile errors this time.  Lets see if it builds...  It does build.  Now lets see what the type is when we run it.  Ok so it creates an integer and it gets the value of 0. 

 

It would be good for temp or throw away variables.  Let's try assigning it to the return type of a function.  I don't think this would be a great idea. 

        public static void TestMethod1()
        {
            var isAString = getIntForTest;
            Console.WriteLine(isAString.ToString());
            Console.WriteLine(isAString.GetType().ToString());
        }
        public static int getIntForTest()
        {
            return 5;
        }

Sweet that code would not compile.  I really think allowing var to be assigned the result of a function would have been a bad idea, because it would make it more difficult to determine what the return type of the function was supposed to be.  I think they did a good job with this keyword.  As a developer with a little bit of experience everything I would have thought would be a bad idea does not work.  I hope the VB6 guys are listening.

Alright enough blogging for tonight.

 

Code Happy,

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

The TDD road continued .... again

by Lord Cod3n 24. September 2008 12:10

Please feel free to leave me comments on items you think could use some elaboration.

So in my attempt to multi-task (learn mvc, learn TDD) at the same time I was going through the test cases in MVC preview 5.  I found these videos which are excellent TDD and MVC videos.  The first video of the series can be found here:

http://www.asp.net/learn/mvc-videos/video-405.aspx 

The Video series is called MVC Pair Programming.

 

The one of the gentlemen in the video is the developer of the NUnit Testing Framework.  Very awesome videos if you guys ever browse my blog.

 

Code Happy,

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

MVC and TDD

by Lord Cod3n 23. September 2008 01:53

Trying to better my understanding of the MVC framework from Microsoft   I begin stepping through the test cases.  This perspective is one of the reasons I love TDD it is a good way to figure out what the code is supposed to do.

 

I believe that some of the test cases are not valid.  Here is an example:

 

        [TestMethod]
        public void FindActionMethodDoesNotMatchProperty() {
            // FindActionMethod() shouldn't match special-named methods like property getters.
            // Arrange
            var controller = new FindMethodController();
            ControllerContext context = GetControllerContext(controller);
            ControllerActionInvokerHelper helper = new ControllerActionInvokerHelper(context);
            // Act
            MethodInfo mi = helper.PublicFindActionMethod("get_Property");
            // Assert
            Assert.IsNull(mi);
        }

This is an example of testing the framework.  When they do reflection to get the methods they do not ask for the properties. Is this a valid test ? 

Right now I am going to go with no.  It could be argued that they are making sure their code works as expected, but when you write code and ask for something specific i.e. (methods)  how would you get back a property?

 

Have a good day

Bill

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Being a Purist

by Lord Cod3n 22. September 2008 10:31

I received an offer to do some basic work in PHP and some database changes in mySQL. While these are not my area of expertise, I am functional in PHP and mySQL.   When it comes to standard SQL I have that down cold.  I have also played around with mySQL at home, so I am a little familiar with the database. 

 

I do not personally feel that open source brings viable products to the market.  My exposure to these areas is purely from an academic stand point up to now.  My development experience with these tools has been terrible to say the least.  I just do not feel these tools save you any money in the long run.  If you pay a developer to develop a website, the tools take him 5x longer, and it is of lower quality, is that better?   Then again, you maybe able to pay the developer a little more than a mcdonalds employee, but the computer systems are needing to be so much more than that.  They need to last, they need to run, they need to integrate, and they need to upgrade cleanly.   Most custom PHP work I have seen is a mess, it does not follow any type of development standards, and the interpreter lets you get away with really bad design. 

Microsoft has free products also. You can develop on .Net with the express editions for free.  I wonder if I am missing something.  mal here is your chance to comment.  IMHO the .Net environment is better the tools are better, and I think you deliver better quality code.  Now you can also see the source for the .net framework while debugging.  If someone out there has good input, I would like to hear it.  I know a lot of anti-Microsofties out there, but I don't get what the benifits of PHP, and mySQL are.  Aside from being free.

 

More TDD posts later.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

TDD going deep Day 2

by Lord Cod3n 19. September 2008 06:29

 First of, created the following test case...

        [TestMethod]
        public void canGetListofClassesFromCodeFile()
        {
            CodeManager cm = new CodeManager();
            string[] classnames = cm.GetClassNamesFromFile();
            Assert.IsTrue(classnames.Count() > 0);
        }

What I did here was write the test the way I wanted it to work.  This is a little difficult because design and implementation keep flying around in my head.  I suppressed the urge to design and possibly over-design and went with adding a user story/functionality to the code. 

The first time the test would not compile, so I implemented the GetClassNamesFromFile() with no code in the method.  The code compiles but throws a NotImplementedException.  We will return a List<string> with one element.  

The test case passed, and I am returned a List<string> instead, with a classname that I added. Once again I am faced with an incorrect implementation and I wonder do I add another test to fix it, or do I modify the code now. 

Q. What happens when one test breaks another test?

A.  [I have not been able to answer this.  Feel free to post an answer in the comments] 

Now attempting, to implement reading in the code file.  My unit test is failing, so I am incrementally fixing it.

My code feels very messy at the moment.  I will post up more later.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

TDD Part 2

by Lord Cod3n 18. September 2008 14:53

After some feedback from Tony Rasa from http://www.elegantcode.com/ , I decided not to finish the book I started with.  If I have not mentioned it before, I use Safari Books Online from O'reilly.  This gives me instant access to over 2000 books from Microsoft, Apress, O'reilly, and others.    I switched to "Test Driven Development by Example" by Kent Black.  This book seems to be more inline with what I am looking for.  It answered some of my questions right up front.  

Warning:  The examples in the book are in JAVA, but it often reads similiar to C#, you should pick it up with no problem.  

Here is a piece I found interesting:

...following are two of the three strategies I know for quickly getting to green:
  •  
    • Fake It— Return a constant and gradually replace constants with variables until you have the real code.
    • Use Obvious Implementation— Type in the real implementation.
When I use TDD in practice, I commonly shift between these two modes of implementation. When everything is going smoothly and I know what to type, I put in Obvious Implementation after Obvious Implementation (running the tests each time to ensure that what's obvious to me is still obvious to the computer). As soon as I get an unexpected red bar, I back up, shift to faking implementations, and refactor to the right code. When my confidence returns, I go back to Obvious Implementations.
There is a third style of TDD, Triangulation, which we will demonstrate in Chapter 3. However, to review, we
  •  
    • Translated a design objection (side effects) into a test case that failed because of the objection
    • Got the code to compile quickly with a stub implementation
    • Made the test work by typing in what seemed to be the right code

The translation of a feeling (for example, disgust at side effects) into a test (for example, multiply the same Dollar twice) is a common theme ofTDD. The longer I do this, the better able I am to translate my aesthetic judgments into tests. When I can do this, my design discussions become much more interesting. First we can talk about whether the system should work like this or like that. Once we decide on the correct behavior, we can talk about the best way of achieving that behavior. We can speculate about truth and beauty all we want over beers, but while we are programming we can leave airy-fairy discussions behind and talk cases. 

 This presented a great example on how to think about tests.  I like all three of these ideas and will try to use them moving forward. 

 

Code Happy,

Bill 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

TDD Update

by Lord Cod3n 18. September 2008 10:14
Wow this gets complicated fast. I am still have trouble what kind of tests I should write. I am attempting to write a code Indexer with Search capabilities. Here is the list of Test Cases so far:
  • CodeManagerCanCreateInstanceOfFileManager
  • CodeManagerReturnsEmptyListWhenSearchTermNotProvided
  • CanGetAListOfFilesFromDirectory
  • CanGetListOfOnlyCodeFiles
  • CreateCodeFileObject

The main problem I have is trying to figure out how to move to the next test. The code is evolving and I feel confident after I make changes.  The code generation feels off.  I have created a Test List.  Some of the items I am currently researching include What goes in a Test Lest ?   Does it map to a Use Case?  Something the developer does?

Let me give an example of this one.  I am creating a test then I see a need for the FileManager object.  Do I then create a Test that makes me create the File Manager Object , or do I just create another test and implement the File Manager Class as a by product of that Test?

 Right now the Code has evolved to include the following objects :   (CodeFile,FileManager,CodeManager) using SoC(Separation of Concerns) I am attempting to isolate working with the File System.  The FileSystem object uses the System.IO namespace.  The CodeManager uses the FileManager to search the file system and Contains a List of CodeFiles.  

 

I am researching by reading the following book:

Test-Driven Development in Microsoft® .NET 

You can find it on amazon here

http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1221783997&sr=8-1  

 

 More updates when the mood hits.

Bill 

 

 

 

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

William Moore is the lead Software Architect and Technologist for Coden Enterprises. He has more than a decade of software development experience primarily Microsoft Platforms.  William enjoys the full gamit of coding everything from the UI down to the database.

Most comments

Page List