Tag Archives: Exploring

Finding Wakelocks in Android 7 using Google’s Battery Historian

One of my favorite apps prior to my upgrade to Android 7 (Nougat) was GSam Battery Monitor (the paid version is fully worth it). Unfortunately, Android 7 no longer allows apps like GSam Battery Monitor to track individual application battery usage, CPU usage, or wakelocks. That sucks*.

In Android 7 with Doze, you should see periods of marginal battery usage. The more apps you have, the more background services/sync operations/location broadcasts are running and you’ll see plateaus in battery usage be more slanted. Up until a few weeks ago, battery usage would basically flatline between screen time. I set out to figure out what was causing this. I found a thread on Reddit discussing Battery Historian, so I decided to check it out. This post is somewhat of a review of this tool.

Battery Historian analyzes bug report files and presents the information in an interactive graph with categorized breakdowns of battery usage. Battery Historian is a web application written in Go, and doesn’t appear to have an official release (binary or hosted). This means it’s basically limited to power users to evaluate. Since it’s licensed under Apache 2.0, I compiled and hosted this for my own use at https://historian.jimschubert.us/. Before using my hosted instance of Battery Historian, please heed Google’s warning about bug report files:

Bug reports contain data from the system’s various log files, including personal and private information. Only share bug reports with apps and people you trust.

Running Battery Historian locally isn’t too difficult. I definitely recommend going this route if you’re more of a power user; you don’t need programming experience, only comfort on the command line.

Once you’ve taken a bug report on your device and uploaded it to Battery Historian, you’re presented with a graph of all activity found in the bug report.

battery_historian

The declining black line is the same battery usage visualization you’d see in your Battery Usage application or widget. Other groups of battery draining events are presented as horizontal time-oriented lines; some are either on or off (screen), some show gradients for battery draining in the category (temperature), while others display different colors for different states of the event (Mobile signal strength).

Scroll below the graph and you’ll find a breakdown of system statistics. After a little sorting and investigation, I found that JuiceSSH had locked WiFi for 1.5 hours when I hadn’t used the application for over a week:

battery_historian_-_juicessh_wifi_lock

After selecting the JuiceSSH app in the left dropdown, this takes you to the App Stats tab which presents some interesting battery usage statistics:

Wakelocks:
Wakelock Name      | Full Time | Full Count | Partial Time | Partial Count | Window Time | Window Count
JuiceSSH CloudSync |           | 0          | 8m 0s 50ms   | 1             |             | 0           
*alarm*            |           | 0          | 684ms        | 1             |             | 0           
Services:
Service Name                            | Time spent started | # starts | # launches
com.sonelli.juicessh.services.CloudSync | 2h 28m 15s 789ms   | 1        | 1         
Process info:
Time spent running actively in the foreground       | 0ms             
Time spent with a service running in the foreground | 0ms             
Time spent on top                                   | 0ms             
Time spent on top while the device was sleeping     | 0ms             
Time spent running actively in the background       | 1h 33m 10s 343ms
Time spent cached                                   | 38ms            

Looks like, in my case, JuiceSSH Cloud Sync had 8 minutes worth of wakelocks, 2.5 hours of a service running in which 1.5 hours of that was actively running in the background. JuiceSSH unfortunately doesn’t have configuration options for Cloud Sync, so I’ve disabled it. I’ve noticed an improvement since disabling JuiceSSH Cloud Sync.

Although I was a little miffed that the Android 7 update borked GSam’s ability to report on per-application usage, I found Battery Historian to be a really insightful tool. From and Android Development perspective it would be a nice utility to evaluate your application’s effect on the system through normal use. It may be a little overkill from a user or power user’s perspective, though. My Nexus 5X was getting about 18 hours of pretty heavy usage (including my son watching YouTube videos). After disabling JuiceSSH Cloud Sync, I’m seeing about 20 hours of battery out of the same usage. Does that really matter if you’re like me and you charge your phone every night? 👻

* I’ve read that GSam Battery Monitor with root still works as expected on Nougat.

Your IDE’s font matters: Fantasque Sans Mono

A good development environment is crucial to your productivity, there’s no question about it. The phrase ‘good development environment’ is subjective. For example, I’m pretty efficient using WebStorm and VisualStudio. I use vim pretty often but I’m nowhere near ‘efficient’. Matias Niemelä, on the other hand can easily manipulate text in vim.

A year or two ago, I read a blog post somewhere that suggested a font is just as important as your tool of choice. I thought, “yeah, right!”, but downloaded the font anyway. After a day of using the font, I realized how much more quickly I could read through code and how much easier it was to visually identify code blocks. I know that sentence sounds trivial, but after two years of using what I consider a better font I definitely notice a difference when I’m not using the font.

My font of choice these days is called Fantasque Sans Mono.

Here’s a screenshot comparison of Arial and Fantasque Sans Mono, both at 14pt.

Atom: arial 14pt
Atom: arial 14pt
Atom: fantasque sans mono 14pt
Atom: fantasque sans mono 14pt

The first thing to notice is how uncluttered letters are in Fantasque. On line 17, the word ‘function’ feels like it runs together in Arial but the letters clearly stand out in Fantasque. Also notice how the letter ‘g’ is unmistakable on line 18. On line 21, the kerning between parentheses, braces, brackets, and vertical pipes all make the line of code slightly easier to read. On line 23, the zero in Arial and many other fonts often resembles the capital letter ‘O’. For literals, this is obviously a number but it can be an easy typo to overlook within a string. In Fantasque, a zero has a line through it to clearly differentiate the number from the letter. Line 25 is easily consumed in the Arial screenshot mostly because the Atom editor clearly distinguishes between patterns and operations in the regex literal. Take a look at the difference without JavaScript syntax highlighting:

Atom: arial 14pt plain text
Atom: arial 14pt plain text
Atom: fantasque sans mono 14pt
Atom: fantasque sans mono 14pt

The actual code tends to be wider than other fonts. In the Arial screenshot, the image stops at the 80 character vertical indicator, which can easily be seen in the Fantasque screenshot. I actually like to use a 12pt or 13pt font size with Fantasque, which gives me a little more code on screen.

Although these points may seem trivial, I challenge you to at least give a few different fonts a try. You’ll be as surprised as I was to find that subtle differences in your font can improve productivity.

Download Fantasque Sans Mono from OpenFontLibrary.

What is JavaScript?

I haven’t updated my site in a while because my wife and I have been very busy moving from the DC-Metro area to Seattle, WA. We made the trip in a five-day drive.

We stopped at a coffee shop in Cour d’Alene, Idaho along the way, and I heard two guys talking about JavaScript. Here is the gist of the conversation:

-“I can’t really find any reasonable documentation for JavaScript’s API”
-”I could be way off, but doesn’t jQuery fit in there somewhere?”
-”jQuery is a framework for JavaScript, but there isn’t any *real* documentation for JavaScript’s functions and objects. I search and search, and all I find is blog after blog.”

So, I’ve decided to blog about JavaScript!

I didn’t butt into the coffee shop conversation and tell the gentleman that JavaScript is an ECMAScript implementation that is largely reliant on the browser within which it is implemented. The reason I didn’t say this is because it’s a common misconception that JavaScript is used solely within a browser’s rendered page.

For instance, Google coded a JavaScript engine called v8 (ECMA-262-compliant and from scratch, as I understand it) which “can run standalone, or can be embedded into any C++ application.” (source: http://code.google.com/p/v8/ ) This means that a developer could write code in JavaScript and run it as any scripting language (ruby, php, bash, etc.) This alone is a difficult concept to grasp for those of us who immediately jump to “JavaScript depends on whatever browser…” answers.

The API itself is a standard released by ECMA International. In short, ECMA International was created to standardize certain languages and technologies and influence the community to adopt these standards. Languages such as JavaScript, C#, and C++ follow ECMA standards. More information can be found on their website: http://www.ecma-international.org/

I wanted to explore the standalone side of JavaScript scripting, so I did a little experiment with the v8 source code (which, unfortunately, you’ll have to download and build from scratch).

Downloading and building v8 (on openSuSE 11.3 x64)

Install Prerequisites and checkout v8

sudo zypper install subversion
sudo zypper install python
sudo zypper install scons

cd ~/bin && mkdir v8 && cd v8
svn checkout http://v8.googlecode.com/svn/trunk/ v8-read-only
cd v8-read-only

Options and building.

Open SConstruct and view all options. A good number of options are displayed as values of hashes similar to JSON key/value pairs (e.g. all:”mode:debug”) and can be written on the command line replacing the colon with an equal sign (e.g. mode=debug). This is necessary knowledge if buildling on a x64 linux machine because it’s not well-documented on the site.
Now, run the build for a developer shell:

scons d8 arch=x64 snapshot=on mode=debug

Test out the command line scripting

[email protected]:~/bin/v8/v8-read-only> ./d8_g 
V8 version 2.4.6 [console: dumb]
d8> var obj = {};
d8> obj.FirstName = "Jim";
Jim
d8> obj.LastName = "Schubert";
Schubert
d8> function foo(o) { print("My name is " + o.FirstName + " " + o.LastName); }
d8> foo(obj);
My name is Jim Schubert
d8> 

I’ve only been playing around with this for a few minutes now, and I’m wondering how powerful of a scripting language JavaScript can be using a standalone v8 interpreter. After a little more research regarding server-side JavaScript, I found a CGI wrapper around v8 which is also hosted on Google Code.

Bitwise operations and Flags (C#)

I haven’t written anything in a while, so I thought I would finally write about the subject of bitwise operations and the FlagsAttribute.

I mentioned this to one of the developers on my team, and he said that he somewhat understood bit operations but he had never found a reason to use them.

Here is the code I will use to discuss the operations

int a = 57754;
int b = 18782;
			
int aXORb = a ^ b;
int aORb = a | b;
int aANDb = a & b;
int aNOT = ~a;
int bNOT = ~b;
			
string spacer = "---------------------------------------------";
Console.WriteLine("{0} ({1}) a", GetBitString(a), a.ToString());
Console.WriteLine("{0} ({1}) b", GetBitString(b), b.ToString());
Console.WriteLine("{0} ({1}) a ^ b", GetBitString(aXORb), aXORb);
Console.WriteLine(spacer);	
Console.WriteLine("{0} ({1}) a", GetBitString(a), a.ToString());
Console.WriteLine("{0} ({1}) b", GetBitString(b), b.ToString());
Console.WriteLine("{0} ({1}) a | b", GetBitString(aORb), aORb);
Console.WriteLine(spacer);	
Console.WriteLine("{0} ({1}) a", GetBitString(a), a.ToString());
Console.WriteLine("{0} ({1}) b", GetBitString(b), b.ToString());
Console.WriteLine("{0} ({1}) a & b", GetBitString(aANDb), aANDb);
Console.WriteLine(spacer);	
Console.WriteLine("{0} ({1}) a", GetBitString(a), a.ToString());
Console.WriteLine("{0} ({1}) ~a", GetBitString(aNOT), aNOT);
Console.WriteLine("{0} ({1}) ~(~a)", GetBitString(~aNOT), ~aNOT);
Console.WriteLine(spacer);	
Console.WriteLine("{0} ({1}) b", GetBitString(b), b.ToString());
Console.WriteLine("{0} ({1}) ~b", GetBitString(bNOT), bNOT);
Console.WriteLine("{0} ({1}) ~(~b)", GetBitString(~bNOT), ~bNOT);
			
Console.ReadLine();

// And, the GetBitString method
static string GetBitString(int input){
	int sizeInt;
	// Number of bits is bytes * 8
	// I specifically chose 16-bit values to reduce the amount displayed
	unsafe{ sizeInt = (sizeof(ushort) * 8); }
	
	string output = String.Empty;
	for (; sizeInt >= 0; sizeInt--) {
		output += (input >> sizeInt) & 1;
		if(sizeInt % 4 == 0) { output += " "; }
	}
	
	return output;
}

XOR ( ^ )

From MSDN:

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

I think of XOR as a “one-toggle”. The logic can be seen as (format is [first] : [second] –> [result] ):

0 : 1 --> 1
1 : 1 --> 0
1 : 0 --> 1
0 : 0 --> 0

As you can see, Continue reading Bitwise operations and Flags (C#)

String Concatenation in .NET – what really goes on?

There are a number of ways to concatenate a string in C# and other .NET languages. Is there a *best* way?

Let’s look at how the C# compiler translates our code into IL to see the differences.

Here is a simple little console application to concatenate a string via String.Concat, String.Format, System.String’s (+) operator, System.String’s Join, and StringBuilder’s Append method.

    class Program
    {
        static void Main(string[] args)
        {
            string first = "The cake";
            string second = " is a";
            string third = " lie.";

            string fromConcat = Concat(first, second, third);
            string fromFormat = Format(first, second, third);
            string fromPluses = Pluses(first, second, third);
            string fromBuilder = Builder(first, second, third);
            string fromJoiner = Joiner(first, second, third);

            Console.WriteLine(fromConcat);
            Console.WriteLine(fromFormat);
            Console.WriteLine(fromPluses);
            Console.WriteLine(fromBuilder);
            Console.WriteLine(fromJoiner);

            Console.ReadLine();
        }

        static string Concat(params string[] strings)
        {
            return String.Concat(strings);
        }

        static string Format(params string[] strings)
        {
            return string.Format("{0}{1}{2}", strings);
        }

        static string Pluses(params string[] strings)
        {
            return strings[0] + strings[1] + strings[2];
        }

        static string Builder(params string[] strings)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(strings[0]);
            sb.Append(strings[1]);
            sb.Append(strings[2]);
            return sb.ToString();
        }

        static string Joiner(params string[] strings)
        {
            return string.Join("", strings);
        }
    }

Which of these do you think will require the most intermediary code? I’d say it’s StringBuilder, since we have to instantiate an object and call a method on that object a number of times. I’d like to go over these individually. The code in Main() works the same for all methods: a new string array is created and passed to the method which returns a string into a new variable. Here are the methods:

Concat

  .method private hidebysig static string 
          Concat(string[] strings) cil managed
  {
    .param [1]
    .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) // Code size       12 (0xc)
    .maxstack  1
    .locals init ([0] string CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  call       string [mscorlib]System.String::Concat(string[])
    IL_0007:  stloc.0
    IL_0008:  br.s       IL_000a
    IL_000a:  ldloc.0
    IL_000b:  ret
  } // end of method Program::Concat

The C# compiler converts String.Concat into the smallest amount of code, and (I assume), the best as far as performance. It couldn’t get any simpler than this method: it loads the arguments, passes them to the method, stores that result in memory and returns.

Format

  .method private hidebysig static string 
          Format(string[] strings) cil managed
  {
    .param [1]
    .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) // Code size       17 (0x11)
    .maxstack  2
    .locals init ([0] string CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldstr      "{0}{1}{2}"
    IL_0006:  ldarg.0
    IL_0007:  call       string [mscorlib]System.String::Format(string,  object[])
    IL_000c:  stloc.0
    IL_000d:  br.s       IL_000f
    IL_000f:  ldloc.0
    IL_0010:  ret
  } // end of method Program::Format

String.Format (my favorite of all the methods), comes in at a code size of 17 lines. The additional space is required for the string’s template, which of course isn’t necessary with String.Concat, but allows you to perform a number of useful operations on a string. For instance, String.Concat will only string the strings together. If you want to add a space between them, you’d have to create either one string to represent a space and concat that along (which gives you the same amount of code as String.Format anyway), or you could do something like:

string overDoingIt = String.Concat(string[0], " probably", string[1], " big fat", string[2]);

Not to mention, String.Format allows you to easily apply formatting rules without individually creating new objects. Granted, it would be converted into IL as something using a number formatter, but it makes our jobs as developers much easier.

Pluses

  .method private hidebysig static string 
          Pluses(string[] strings) cil managed
  {
    .param [1]
    .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) // Code size       20 (0x14)
    .maxstack  4
    .locals init ([0] string CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  ldc.i4.0
    IL_0003:  ldelem.ref
    IL_0004:  ldarg.0
    IL_0005:  ldc.i4.1
    IL_0006:  ldelem.ref
    IL_0007:  ldarg.0
    IL_0008:  ldc.i4.2
    IL_0009:  ldelem.ref
    IL_000a:  call       string [mscorlib]System.String::Concat(string, string, string)
    IL_000f:  stloc.0
    IL_0010:  br.s       IL_0012
    IL_0012:  ldloc.0
    IL_0013:  ret
  } // end of method Program::Pluses

I see the (+) operator used a lot for string concatenation. There have been a number of articles that claim this performance is worse than that of String.Concat. Not even bothering with the actual performance aspect of it, you can see that this requires nearly twice as much intermediate language code to be generated than String.Concat.

If you’re writing an application that requires thousands of string concatenation operations, I’d suggest using String.Concat. Even if you have to hard-code single characters as constants and occasionally pass a new string into the mix, it should still offer a great deal less generated code (and presumably better performance) than the (+) operator.

Builder

  .method private hidebysig static string 
          Builder(string[] strings) cil managed
  {
    .param [1]
    .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )    // Code size       48 (0x30)
    .maxstack  3
    .locals init ([0] class [mscorlib]System.Text.StringBuilder sb, [1] string CS$1$0000)
    IL_0000:  nop
    IL_0001:  newobj     instance void [mscorlib]System.Text.StringBuilder::.ctor()
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  ldarg.0
    IL_0009:  ldc.i4.0
    IL_000a:  ldelem.ref
    IL_000b:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
    IL_0010:  pop
    IL_0011:  ldloc.0
    IL_0012:  ldarg.0
    IL_0013:  ldc.i4.1
    IL_0014:  ldelem.ref
    IL_0015:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
    IL_001a:  pop
    IL_001b:  ldloc.0
    IL_001c:  ldarg.0
    IL_001d:  ldc.i4.2
    IL_001e:  ldelem.ref
    IL_001f:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
    IL_0024:  pop
    IL_0025:  ldloc.0
    IL_0026:  callvirt   instance string [mscorlib]System.Object::ToString()
    IL_002b:  stloc.1
    IL_002c:  br.s       IL_002e
    IL_002e:  ldloc.1
    IL_002f:  ret
  } // end of method Program::Builder

I see this used a lot in ASP.NET for injecting startup JavaScript scripts (even for 5 lines of code!). Aside from the obvious problems with that, I’d like to mention that in .NET 3.0 and higher, Page.ClientScript.RegisterStartupScript now has an overload which adds script tags. That means you don’t have to worry about the curly braces that would otherwise mess up your String.Format.

Anyway, what’s going on here is: in lines 15, 21, and 27, we’re calling the method Append on our instance of StringBuilder (instantiated as ‘sb’ in line 7). The strings are popped off the stack and passed into the method. At the end, we still have to call ToString() on our object. This is obviously way more work than is necessary. Granted, there are times when StringBuilder comes in handy, but simple concatenation really shouldn’t use it.

Joiner

  .method private hidebysig static string 
          Joiner(string[] strings) cil managed
  {
    .param [1]
    .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )  // Code size       17 (0x11)
    .maxstack  2
    .locals init ([0] string CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldstr      ""
    IL_0006:  ldarg.0
    IL_0007:  call       string [mscorlib]System.String::Join(string, string[])
    IL_000c:  stloc.0
    IL_000d:  br.s       IL_000f
    IL_000f:  ldloc.0
    IL_0010:  ret
  } // end of method Program::Joiner

String.Join is interesting in that it’s sort of between String.Concat and String.Format. I usually overlook String.Join because, like I said, I prefer String.Format. But, look at how the Joiner method requires the same amount of IL code to be generated as String.Format. There really isn’t any additional formatting required here, so String.Join is probably the second best solution.

Assume for a second that we didn’t have any spacing in our strings, and instead had “The”, “cake”, “is”, “a”, “lie”. Instead of passing these to String.Concat with a space such as:

string space = " ";
// typing the following line gets boring very quickly:
return String.Concat(string[0], space, string[1], space, string[2], space // etc...

we could use String.Join:

return String.Join(" ", strings));

Conclusion
Here is a quick summary of the findings of this little exploration:

  • String.Concat– Good for joining strings without any additional processing
  • String.Join– Good for joining strings with a specified delimeter
  • String.Format– Same amount of code for String.Join, but allows for additional formatting of strings
  • String (+) operator– Unnecessary amount of overhead when used to simply combine supplied strings
  • StringBuilder– Overly bloated method for simple concatenation.

Programming to Interfaces or Objects?

The other day, I was asked to add some functionality to code that regularly instantiates objects as interfaces, and I was wondering what exactly is the point? I understand the polymorphic aspect of interfaces, and I fully agree that interfaces should be used as parameter and return types. But, is there some sort of performance gain when implementing an object as the interface rather than the object?

To test this question, I’m going to run a simple console application to list two actors and 2 or 3 movies for each one.
Here is the code required to run the application:

    class Actor
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public decimal AnnualSalary { get; set; }
        public List<Movie> Roles { get; set; }
        public override string ToString()
        {
            return String.Format("{0}({1}), {2:c}/year",
                Name, Age, AnnualSalary);
        }
    }

    class Movie
    {
        public string Title { get; set; }
        public int ReleaseYear { get; set; }
        public string Director { get; set; }
        public decimal Budget { get; set; }

        public override string ToString()
        {
            return String.Format("{0}({1}), {2} [{3:c}]",
                Title, ReleaseYear, Director, Budget);
        }
    }

    class MockRepository
    {
         internal List<Actor> GetActors()
        {
            List<Actor> actors = new List<Actor>();
            actors.AddRange(new List<Actor>
            {
                new Actor { Name = "Zooey Deschanel",
                    Age = Convert.ToInt32(
                        DateTime.Now.Subtract(
                            new DateTime(1980, 1, 17)
                        ).TotalDays) / 365,
                    AnnualSalary = 1000000, 
                    Roles = GetMovies("Zooey")
                },
                new Actor {
                    Name = "Tony Jaa",
                    Age = Convert.ToInt32(
                     DateTime.Now.Subtract(
                        new DateTime(1976, 2, 5)
                    ).TotalDays) / 365,
                   AnnualSalary = 5000000,
                   Roles = GetMovies("Tony")
                }
            });
            return actors;
        }

        internal List<Movie> GetMovies(string actor)
        {
            List<Movie> movies = new List<Movie>();
            switch (actor)
            {
                case "Zooey":
                    movies.Add(new Movie
                    {
                        Director = "Michael Clancy",
                        Title = "Eulogy",
                        ReleaseYear = 2004,
                        Budget = 10000000
                    });
                    movies.Add(new Movie
                    {
                        Title = "(500) Days of Summer",
                        Director = "Marc Webb",
                        ReleaseYear = 2009,
                        Budget = 7500000
                    });
                    movies.Add(new Movie
                    {
                        Title = "Yes Man",
                        Director = "Peyton Reed",
                        ReleaseYear = 2008,
                        Budget = 50000000
                    });
                    break;
                case "Tony":
                    movies.Add(new Movie
                    {
                        Title = "Ong Bak 2",
                        Director = "Panna Rittikrai",
                        ReleaseYear = 2008,
                        Budget = 50000000
                    });
                    movies.Add(new Movie
                    {
                        Title = "Ong Bak",
                        Director = "Prachya Pinkaew",
                        ReleaseYear = 2003,
                        Budget = 50000000
                    });
                    break;
                default:
                    break;
            }

            return movies;
        }
    }

And, here is the console application:

        static void Main(string[] args)
        {
            MockRepository repository = new MockRepository();
            List<Actor> actors = repository.GetActors();

            foreach (Actor actor in actors)
            {
                Console.WriteLine(actor.ToString());
                foreach (Movie movie in actor.Roles)
                {
                    Console.WriteLine("\t{0}", movie.ToString());
                }
            }

            Console.ReadLine();
        }

This produces the following output (salaries and annual budgets aren’t correct):

Zooey Deschanel(30), $1,000,000.00/year
        Eulogy(2004), Michael Clancy [$10,000,000.00]
        (500) Days of Summer(2009), Marc Webb [$7,500,000.00]
        Yes Man(2008), Peyton Reed [$50,000,000.00]
Tony Jaa(34), $5,000,000.00/year
        Ong Bak 2(2008), Panna Rittikrai [$50,000,000.00]
        Ong Bak(2003), Prachya Pinkaew [$50,000,000.00]

I’ve gone back and modified the above code to return interfaces instead of objects.

// Note: IList does not implement AddRange. So instead of:
IList<IActor> actors = new List<IActor>();
actors.AddRange(new List<IActor> { /* etc */ } );
// We'll have to do:
List<IActor> actors = new List<IActor>();
actors.AddRange(new List<IActor> { /* etc */ });

For *fun* I’ve outputted the Intermediate Language for each console application and run a diff between the two. The code follows, but it is easily summed up as: the code is relatively the same.

41c41
< // MVID: {D457D4D6-EFDE-4F7F-9A7D-F77E2FB92D1F}
---
> // MVID: {EF8FAF3C-306F-4E76-AE54-3A4CA179FAB7}
47c47
< // Image base: 0x00400000
---
> // Image base: 0x003E0000
61,65c61,65
<              [1] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> actors,
<              [2] class InterfacesOrObjects.IActor actor,
<              [3] class InterfacesOrObjects.IMovie movie,
<              [4] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IActor> CS$5$0000,
<              [5] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IMovie> CS$5$0001,
---
>              [1] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> actors,
>              [2] class InterfacesOrObjects.Actor actor,
>              [3] class InterfacesOrObjects.Movie movie,
>              [4] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Actor> CS$5$0000,
>              [5] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Movie> CS$5$0001,
71c71
<     IL_0008:  callvirt   instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> InterfacesOrObjects.MockRepository::GetActors()
---
>     IL_0008:  callvirt   instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> InterfacesOrObjects.MockRepository::GetActors()
75c75
<     IL_0010:  callvirt   instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::GetEnumerator()
---
>     IL_0010:  callvirt   instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::GetEnumerator()
82c82
<       IL_001b:  call       instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IActor>::get_Current()
---
>       IL_001b:  call       instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Actor>::get_Current()
86c86
<       IL_0023:  callvirt   instance string InterfacesOrObjects.IActor::ToString()
---
>       IL_0023:  callvirt   instance string [mscorlib]System.Object::ToString()
91,92c91,92
<       IL_0030:  callvirt   instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.IActor::get_Roles()
<       IL_0035:  callvirt   instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::GetEnumerator()
---
>       IL_0030:  callvirt   instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.Actor::get_Roles()
>       IL_0035:  callvirt   instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::GetEnumerator()
99c99
<         IL_0040:  call       instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IMovie>::get_Current()
---
>         IL_0040:  call       instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Movie>::get_Current()
104c104
<         IL_004d:  callvirt   instance string InterfacesOrObjects.IMovie::ToString()
---
>         IL_004d:  callvirt   instance string [mscorlib]System.Object::ToString()
110c110
<         IL_005b:  call       instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IMovie>::MoveNext()
---
>         IL_005b:  call       instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Movie>::MoveNext()
121c121
<         IL_006a:  constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IMovie>
---
>         IL_006a:  constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Movie>
129c129
<       IL_007b:  call       instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IActor>::MoveNext()
---
>       IL_007b:  call       instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Actor>::MoveNext()
140c140
<       IL_008a:  constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.IActor>
---
>       IL_008a:  constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class InterfacesOrObjects.Actor>
197c197
<           instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> 
---
>           instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> 
203c203
<           instance void  set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> 'value') cil managed
---
>           instance void  set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> 'value') cil managed
228c228
<   .property instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>
---
>   .property instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>
231,232c231,232
<     .get instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.IActor::get_Roles()
<     .set instance void InterfacesOrObjects.IActor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>)
---
>     .get instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.IActor::get_Roles()
>     .set instance void InterfacesOrObjects.IActor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>)
246c246
<   .field private class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> '<Roles>k__BackingField'
---
>   .field private class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> '<Roles>k__BackingField'
334c334
<           instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> 
---
>           instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> 
340c340
<     .locals init (class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> V_0)
---
>     .locals init (class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> V_0)
342c342
<     IL_0001:  ldfld      class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.Actor::'<Roles>k__BackingField'
---
>     IL_0001:  ldfld      class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.Actor::'<Roles>k__BackingField'
351c351
<           instance void  set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> 'value') cil managed
---
>           instance void  set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> 'value') cil managed
358c358
<     IL_0002:  stfld      class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.Actor::'<Roles>k__BackingField'
---
>     IL_0002:  stfld      class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.Actor::'<Roles>k__BackingField'
415c415
<   .property instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>
---
>   .property instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>
418,419c418,419
<     .set instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>)
<     .get instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.Actor::get_Roles()
---
>     .set instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>)
>     .get instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.Actor::get_Roles()
699c699
<   .method assembly hidebysig instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> 
---
>   .method assembly hidebysig instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> 
704,705c704,705
<     .locals init ([0] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> actors,
<              [1] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> '<>g__initLocal0',
---
>     .locals init ([0] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> actors,
>              [1] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> '<>g__initLocal0',
708c708
<              [4] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor> CS$1$0000,
---
>              [4] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor> CS$1$0000,
712c712
<     IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::.ctor()
---
>     IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::.ctor()
715c715
<     IL_0008:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::.ctor()
---
>     IL_0008:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::.ctor()
751,752c751,752
<     IL_006f:  call       instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.MockRepository::GetMovies(string)
<     IL_0074:  callvirt   instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>)
---
>     IL_006f:  call       instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.MockRepository::GetMovies(string)
>     IL_0074:  callvirt   instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>)
755c755
<     IL_007b:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::Add(!0)
---
>     IL_007b:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::Add(!0)
791,792c791,792
<     IL_00e1:  call       instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> InterfacesOrObjects.MockRepository::GetMovies(string)
<     IL_00e6:  callvirt   instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>)
---
>     IL_00e1:  call       instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> InterfacesOrObjects.MockRepository::GetMovies(string)
>     IL_00e6:  callvirt   instance void InterfacesOrObjects.Actor::set_Roles(class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>)
795c795
<     IL_00ed:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::Add(!0)
---
>     IL_00ed:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::Add(!0)
798c798
<     IL_00f4:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IActor>::AddRange(class [mscorlib]System.Collections.Generic.IEnumerable`1<!0>)
---
>     IL_00f4:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Actor>::AddRange(class [mscorlib]System.Collections.Generic.IEnumerable`1<!0>)
808c808
<   .method assembly hidebysig instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> 
---
>   .method assembly hidebysig instance class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> 
813c813
<     .locals init ([0] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> movies,
---
>     .locals init ([0] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> movies,
819c819
<              [6] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie> CS$1$0000,
---
>              [6] class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie> CS$1$0000,
822c822
<     IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::.ctor()
---
>     IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::.ctor()
864c864
<     IL_0072:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::Add(!0)
---
>     IL_0072:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::Add(!0)
887c887
<     IL_00b5:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::Add(!0)
---
>     IL_00b5:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::Add(!0)
910c910
<     IL_00f8:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::Add(!0)
---
>     IL_00f8:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::Add(!0)
935c935
<     IL_0146:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::Add(!0)
---
>     IL_0146:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::Add(!0)
958c958
<     IL_018f:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.IMovie>::Add(!0)
---
>     IL_018f:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<class InterfacesOrObjects.Movie>::Add(!0)
988c988
< // WARNING: Created Win32 resource file C:\Users\Jim\Desktop\Interfaces or Objects\AsInterfaces.res
---
> // WARNING: Created Win32 resource file C:\Users\Jim\Desktop\Interfaces or Objects\AsObjects.res

This leads me to the question, why would anyone program implementations as interfaces? In Visual Studio, when you right click and choose “Go to declaration” on a method implemented as an interface instead of an object (for instance, if actor.Roles used a getter to perform some action on movies), you will be directed to the interface, not to the actual method of the object. As noted above, IList doesn’t implement AddRange, so we’d have to explicitly cast the interface implementation in order to use the desired method.

Is it possible that interfaces can be overdone?

I don’t think there is any huge benefit to using interfaces unless your application is going to be “interfaced” with, or if there is some amount of inheritance or polymorphism in the application. In the example posted above, the interfaces are completely unnecessary.