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):
static void Main(string args)
string.Join(" ", args).Contains("/s")
? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(f => File.ReadAllLines(f).Length)
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();
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.
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.
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:
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.
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.
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.
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).
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:
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:
The next thing is to create a new NDepend project and add the assemblies to it that you want to analyze.
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:
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:
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:
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.
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:
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.