Thursday, January 17, 2019

Things I've Been Learning Lately in Software Dev (mostly C# .net core)

.Net Core 2.0, to include an HTML fragment into a .cshtml page:

    @await Html.PartialAsync("~/Views/Shared/_Address.cshtml")

To prepare for Dependency Injection in .Net Core 2.0

public void ConfigureServices(IServiceCollection services)
 {
     services.AddSingleton(provider => Configuration);
     services.AddTransient<IUserRepository, UserRepository>();
     services.AddSingleton<IMailer, SmtpMailer>();
 }

Create simple Moq object in C#

var imailerMoq = new Mock();
imailerMoq.Setup(x => x.SendEmail(It.IsAny(), It.IsAny())).Returns(true);

How to return async task for a unit test

public async Task GetAwesomeString() {
   return await Task.FromResult("I'm the awesome returned text");
}

sudo -s //makes the entire session root

In C# you cannot downcast an object, only cast an object upwards:

wrong    Sheep sheep = (sheep)mammal;
right  Mammal mammal = (Mammal)sheep;

C# -- to get prettyprint of json object with indentation:

var json = JsonConvert.SerializeObject(myObject, Formatting.Indented);

C# to get a key from a value:

Dictionary dictionary = GetMyDictionary();
var name = dictionary.FirstOrDefault(x => x.Value == id).Key;

To change the prompt in linux:

echo "PS1='\w\$ '" >> ~/.bash_profile; source ~/.bash_profile

Wednesday, January 16, 2019

A TestDriven Workshop Example Using C# and NUnit

I've done this workshop multiple times at work to teach developers Test Driven Development using C# and NUnit.

You can clone it at https://github.com/fincher42/TDDWorkshopNunit.git

The theme of the workshop is from the movie "We Bought A Zoo".

Enjoy and leave me feedback below on how to improve the workshop.


Here's an outline of my notes:

Essence of TDD: Write failing tests **first**, before coding the functionality.

Ping Pong

  1. Bob writes a failing test
  2. Ashley writes just enough functionality in the method so it will pass.
  3. The code may then be refactored to make it cleaner.
  4. Bob and Ashley now switch roles and continue.

This is the Red/Green/Refactor cycle.

One method of arranging your code is to have Arrange/Act/Assert sections in your test.

Benefits of Test Driven Development:

  1. Encourages better architectural design
  2. Makes debugging easier
  3. Guarantees tests are written
  4. Encourages us to think about requirements and edge cases
  5. Let's you be courageous and refactor code

Drawbacks of Test Driven Development:

  1. Tests are expensive. They are expensive to write and more expensive to maintain.
  2. Perfect Unit Tests with 100% coverage do not offer any guarantees on overall code quality. Don't be lulled into a sense of false security.
  3. GUI, Integration, Performance, and Endurance tests are also needed.

Thursday, January 10, 2019

The Four Biggest Changes in Software Development in the Last 40 Years

I was giving a workshop on Test Driven Development today. Being a history nerd (don't get me started about the causes of the fall of the Roman Empire), I was reflecting back on how much has changed in software and how much hasn't. I started programming 42 years ago on punched cards. Here's my list of the most game changing developments in software in the last 40 years.

  1. Duh, It's the Internet
  2. Being able to use Stackoverflow and its kin, we can peruse the minds of thousands of people and find a solution quickly. Online training courses are available to teach us the latest software. It's simply amazing how much more we can create and learn.

  3. Computers are so much faster.
  4. With punched cards at Texas Tech University in the fall of 1976, a diligent student could get in five compile/executes cycles a day. Five. Now you can do that in a few minutes. This changes the way we develop software. Instead of mapping out your entire program on quadral paper using the IBM flowchart template with a Pentel mechanical pencil, we can start programming and build the software iteratively.

  5. Interactive Development Environments (IDE)
  6. Having an IDE that can pop-up all the available methods on an object is such a time saver. Having Resharper to refactor code on the fly, perfectly, is such an asset.

  7. Test Driven Development (TDD)
  8. Test Driven Development encourages us to write software that is easy to test, but more importantly, it demands a layered architecture and encourages small modules. TDD gives us the courage to do massive refactoring. Thank you Kent Beck.

My major professor at Tech use to tell us about a principle of good software engineering, but follow it up with "But, of course, this doesn't matter if you have really good people." That is still true today. The heart of producing good software is excellent people with small egos who have a passion to hone their craft and cooperate with their peers.