November, 2008

  • Kirill Osenkov

    A one-line program to count lines of code

    • 10 Comments

    I wanted to sum the total lines of code in files in a given folder. I thought that writing my own program to do this would be faster than looking for it on the internet, so here's what I came up with (1 line broken into 7 lines to fit into your blog reader):

    using System;
    using System.Linq;
    using System.IO;
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                Directory.GetFiles(
                    Environment.CurrentDirectory, "*", 
                    string.Join(" ", args).Contains("/s") 
                        ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                .Select(f => File.ReadAllLines(f).Length)
                .Sum());
        }
    }

    Just name the executable loc.exe and put it into your PATH - you're good to go. Input "loc" in the command prompt to get the total number of LOC in the current directory, and "loc /s" to do recursive search.

    Please note that the way I wrote this program is not very good for debugging, because you can't put a breakpoint on substatements (technically speaking, this program consists of only one statement). In production code, I would rather write something like this:

    string path = Environment.CurrentDirectory;
    string pattern = "*";
    string commandLine = string.Join(" ", args);
    SearchOption searchRecursively = commandLine.Contains("/s") 
                ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
    string[] files = Directory.GetFiles(path, pattern, searchRecursively); IEnumerable<int> lengths = files.Select(f => File.ReadAllLines(f).Length); int totalLOC = lengths.Sum();
    Console.WriteLine(totalLOC);

    because it better conveys the step-by-step actions and allows to put a breakpoint on any step. However, for my little program, my sense of style guided me to use the former notation.

    As another side note, the "production version" doesn't use var for type inference. I think it improves readability in this case.

  • Kirill Osenkov

    Links about Visual Studio 2010 and C# 4.0

    • 8 Comments

    A while ago we announced Visual Studio 2010 and C# 4.0. In case you'd like to catch up and read articles or watch videos about the upcoming new features, I wanted to gather some links here.

    PDC

    TechEd EMEA 2008 (Barcelona)

    C# 4.0 Language

    As you probably know by now, C# 4 is mostly oriented on interoperability: better COM interop, better interop with dynamic languages (like Python), better interop with the browser (Silverlight, JavaScript) and better Office interop. Briefly, the new language features are:

    1. Dynamic - "late binding" in C# - a way to delay binding and code execution to runtime
    2. Variance - generic co- and contravariance
    3. Named and Optional parameters - better parity with VB, no more "ref missing", more readable code with named parameters
    4. COM interop support - omitting "ref" for COM calls, NoPIA (embedding Primary Interop Assemblies directly into your assembly to ease deployment) and some others

    What to read about C# 4 language features? Well, first and foremost, there is always Charlie's blog that accumulates all C# news:

    C# compiler developer's blogs are a terrific technical resource - highly recommended:

    C# IDE improvements

    As far as I can tell, we don't yet have a comprehensive resource about the new features in the C# IDE. Well, let me spill the beans and briefly mention them here, before I dive into the details in my upcoming posts.

    1. Call Hierarchy - for a method or property, allows to view Calls To (callers), Calls From (callees), Overrides (if applicable) and Implementations (if applicable).
    2. Quick Symbol Search - a very simple dialog with a textbox and a listbox that allows you to type a symbol's name or part of it (any type or member) and hit Enter to navigate to it quickly. Ctrl+, is the shortcut that you can reconfigure if necessary.
    3. Generate From Usage - just like we had Generate Method Stub by pressing Ctrl+. on a SmartTag under undefined method call, now you can generate a new class for an undefined class, a constructor for an unknown constructor, a property, an enum, etc.
    4. Highlight References - when a cursor is on a symbol, all the references to this symbol are highlighted in the currently opened file.
    5. Many other miscellaneous improvements, such as a better background compiler ("Live Squiggles"), XML doc rename refactoring, etc.

    .NET Framework 4.0

    Justin van Patten has a great overview of upcoming new features in .NET 4.0. It is worth mentioning, that 4.0 will have a new CLR version 4.0 (2.0, 3.0 and 3.5 were all working on the CLR version 2.0). Most notably, .NET will introduce tuples, code contracts, parallel extensions, variance annotations and a whole lot more.

    Other Visual Studio improvements

    Of course, I'm only scratching the surface of what the next Visual Studio will bring - many teams across the division worked hard and implemented a lot of cool stuff (e.g. Architecture Explorer, Sequence Diagrams, etc). I'm only mentioning some features that I personally find really interesting as a member of the C# team.

    • New Editor - the Visual Studio code editor has been fully rewritten from scratch using C#, WPF and MEF. It shims the old editor interfaces and pretends to behave just like the old editor, but it also exposes a nice new managed API that you can program against. A huge advantage of the New Editor is flexibility and extensibility - you can do really weird things with it, given the fact that it's WPF and allows any UIElement to be placed on it.
    • WPF Shell - the Visual Studio UI (Shell, menus, toolwindows, toolbars) has been rewritten using WPF as well, to allow customization. Not all of the UI has been converted, so there are some "islands of hWnd" floating in WinFormsHost, which will be converted in future versions.
    • Back-in-time debugger - the Debugger team introduced a new Historical Debugger, which records the program's execution and allows you to rollback the program's state (callstack, variables, etc.) to an earlier point in time.

    In my next blog posts, I will start talking more about the upcoming new features (especially the Call Hierarchy one - since I was the one who tested it).

    kick it on DotNetKicks.com

  • Kirill Osenkov

    NDepend

    • 4 Comments

    Static analysis tools allow us to measure code quality and better understand the design and architecture of software. There are things about source code that are not visible to eye in our day-to-day work: dependencies between components, cyclomatic complexity of methods, hidden and indirect dependencies. There are dangers that a method or a type will become overly complex and induce maintenance nightmare without us even noticing it.

    Today’s post is about NDepend, a static analysis tool developed by Patrick Smacchia. NDepend measures software quality by extracting metrics, gathering and reporting statistics and visualizing architecture.

    Here’s what NDepend does:

    • It analyzes .NET assemblies and provides all thinkable information about them both in a report and interactively
    • It reports various metrics about assemblies, namespaces, types and members
    • It treats the source code as a database and provides a powerful Code Querying Language (CQL) that allows you to write custom SQL-like queries against your codebase
    • It allows navigating the codebase using links, references, usage graphs, call graphs, etc.
    • Unlike some other static analysis tools, NDepend provides rich interactive visualizations of your architecture, that allow you to visually understand layering, distribution of code, code sizes, relationships and dependencies
    • NDepend has an extensive set of Queries and Constraints (compare with FxCop rules) that run against compiled binaries and provide suggestions on how to improve your code (“Top 10 complex methods that need refactoring”, “All methods that shouldn’t be public and can be made internal”, etc.)
    • You can run all these queries to get a full analysis report about your software

    Since both Patrick and I like static analysis and are interested in measuring software quality, Patrick suggested that I try out NDepend on one of my projects and share my experiences – and I gladly agreed. I’ve heard about NDepend long ago, and even before I joined Microsoft I blogged about static analysis tools and publicly promised to take a look at NDepend someday, which I felt was a very interesting tool with unique capabilities. Well, since Patrick recently contacted me himself :), there can be no better opportunity to download NDepend and give it a try. Here I’ll provide my first impressions.

    I went to www.ndepend.com and started to look around. I liked the site and the fact that there were screenshots and some nice 3-min overview videos. Seconds after clicking on a big download button, NDepend was on my desktop. It comes with two executables – command line NDepend.Console.exe (for the build automation) and the UI VisualNDepend.exe. Of course, I started the VisualNDepend first and it presented me with a nice start page:

    clip_image002

    The next thing is to create a new NDepend project and add the assemblies to it that you want to analyze.

    1. First thing that you have to do is to create a project and add the .dlls. Make sure the .pdb files are next to it, so that NDepend can locate the source files and provide additional goodness. If .pdb files point to a non-existing source location, you can use the source file rebasing feature in Project Properties -> Analysis.
    2. I loaded my 6-assembly StructuredEditor project and it automatically displayed the dependent .NET framework .dlls:
      clip_image004
    3. The next step was easy to find as well: the Big Green Run Button to start the analysis:
      clip_image006

    After the analysis ran, it displayed a very comprehensive report about my project. Even glancing over this report already provided a lot of useful information to me:

    1. I was pleased to see that my overall code quality is pretty good :)
    2. It was good to know some quick metrics (number of lines of code, comments, IL instructions, assemblies, types, abstract classed, interfaces, value types, exception classes, attribute classes, delegate classes, percentage of public types and methods, etc.)
    3. It provided a nice visual view of all assemblies, types and members sorted by code size:
      image
    4. I was pleased to see my code in the green area of Abstractness vs. Instability (green is good!):
      image
    5. NDepend visualized the assembly reference graph for me:
      image
      This is really great, since I had this graph in mind before, but I never saw a tool draw this graph for me automatically – it’s like it was reading the architecture from my thoughts.
    6. Finally, it provided a long list of CQL Queries and Constraints (you have to see for yourself, I won’t post it here)
    7. Just reading the rules can be as useful as reading Framework Design Guidelines. Just for example, I learned this:
      // <Name>Attribute classes should be sealed</Name>
      WARN IF Count > 0 IN SELECT TYPES WHERE IsAttributeClass
      AND !IsSealed AND !IsAbstract AND IsPublic AND !IsInFrameworkAssembly
      // The .NET Framework class library provides methods for retrieving custom attributes. 
      // By default, these methods search the attribute inheritance hierarchy; for example 
      // System.Attribute.GetCustomAttribute searches for the specified attribute type, or 
      // any attribute type that extends the specified attribute type. Sealing the attribute 
      // eliminates the search through the inheritance hierarchy, and can improve performance.

    After examining the report, I came back to the main application to find that it presents all the information contained in the report, but interactive:

    image

    I have to say that the UI looks great, but at first seems rather overwhelming and I felt like sitting in a Boeing cockpit. But hey – you can’t expect a complex static analysis tool to have a notepad-like interface – it has to expose its features somehow. Surprisingly, you get used to the UI really quickly and I found my way around really easily.

    All you have are the following things:

    1. Class Browser – displays all assemblies, namespaces, types and members
    2. Metrics – displays a visual map on various levels (types, members, namespaces, etc)
    3. Dependency matrix – helps you analyze dependencies between two code elements
    4. Dependency graph – draws a DAG (remember I was writing about DAG and Topological Sorting?)
    5. CQL Queries – a library of predefined queries over your codebase (and you can write your own!)
    6. Info – information about the selection (updated with focus changes)

    Hopefully this can provide a basic overview of NDepend, what it’s for and what it does, as well as its main features. I was very impressed with the tool, really liked it and looking forward to use it more for better architectural insights and better code quality.

  • Kirill Osenkov

    Book review: Essential C# 3.0 by Mark Michaelis

    • 1 Comments

    Sometimes I'm being asked what book I'd recommend for learning C#. Until recently, I was hesitant and didn't know what to answer. It depends, among other things, on the following factors:

    • Whether the learner has existing programming background
    • Whether he or she wants to learn the language, or the .NET framework as well
    • Whether the learner is already familiar with C# 1.0 (2.0) and wants to learn primarily about C# 3.0 features, such as LINQ

    Well, now I have a good answer for the majority of the cases. Mark Michaelis, a C# MVP, wrote a great book called Essential C# 3.0. It requires pretty much no programming background, and beginners can ramp up easily. One immediate thing about Essential C# 3.0 is that the book is very easy to read. You literally swallow page after page freely and effortlessly. This is one of the major reasons why I'm recommending this book to beginners who ask me where to start.

    The first several chapters introduce basic programming concepts - variables, statements, control flow, methods, and continue gracefully to the .NET type system, classes and objects. It's nice to know that even when you're learning the basics, you're learning the latest version of a modern language and an industry standard.

    The book continues to do a great job at the intermediate level, with chapters about classes, inheritance, interfaces and value types. A great asset is a special chapter called Well-Formed Types, which collects a series of useful information and best practices, such as overriding object members, referencing other assemblies, generating XML comments, finalization and garbage collection etc.

    C# 2.0 features are covered well in a dedicated chapter about Generics and also in other parts of the book (iterators and yield return, anonymous methods).

    Of course, given the book's title, C# 3.0 features and LINQ are introduced and explained well in the following chapters: Delegates and Lambda Expressions, Collection Interfaces with Standard Query Operators, Query Expressions. Several other chapters show more aspects of using the C# language: Events, Exception Handling, Building Custom Collections.

    Finally, an advanced C# user will find a lot of interesting things here as well. I was particularly attracted by a great chapter on Reflection and Attributes, and the two "icing-on-the-cake" chapters: Multithreading and Multithreading patterns. I've learned a lot of new things for myself here. Also, throughout the rest of the book, special "Advanced Topic" fragments provide a lot of value to an experienced reader. For example, I've learned that the new operator generates newobj IL instruction for reference types and initobj for value types - I didn't think about this distinction before.

    To sum up, a great book about C# for every reader - beginner, intermediate, advanced. It's not only about the language - CLR is also covered pretty well (IL, garbage collection, JIT, AppDomains, reflection, threading). This book doesn't cover any of the applied libraries (such as ASP.NET, ADO.NET, WPF or Silverlight), but even without it, it's already more than 700 pages thick. I also really love the formatting, fonts, spacing and illustrations - very ".NET-style".

    A great read and highly recommended.

Page 1 of 1 (4 items)