Saturday, July 15, 2017

Recommended design-level books

I was recently asked which design books i would recommend. So here's design books that I've read and would personally recommend.
  1. Refactoring

Refactoring skills consist of these parts:
  1. Being able to spot code and design "smells". 
  2. Being able to systematically refactor smelly code to a better structure

Refactoring: Improving the Design of Existing Code

I use this site as a reference. When I do code reviews I send links to specific refactoring and code smells. 

2. Design Patterns

This book teaches the SOLID object-oriented design principles + design patterns. 

For example: 
1. The I in SOLID = Dependency-Inversion Principle
2. One design pattern is called the Observer Pattern. One thing this pattern used for is to invert dependencies. 

3. Making existing code unit testable

This book is basically about techniques to make existing code unit testable. The side effect of making code testable is that it has a good, loosely coupled design. 

Working Effectively with Legacy Code

4. Agile Principles, Patterns, and Practices

This book explains everything having to do with Agile. There are parts about the process, but more importantly it explains what we can apply as individual developers. Things like design principles (SOLID), patterns, refactoring, and test-driven development, and much more. 

Agile Principles, Patterns, and Practices in C# (there's a Java version too i think)

Tuesday, June 6, 2017

Get Kindle book highlights

I like to take all my highlights after reading a book and organize them into main ideas etc... One problem is they don't offer the ability to export the highlights without added junk.

How to get the highlights and clean them
1. Go to

2. Click on a book

3. Copy and paste the highlights to Notepad++ (download if you dont have it)

4. Do a Replace All using the regex: (Blue|Yellow) highlight \| (Location|Page): [0-9,]+\r\n

Add more colors to the first group if you use other highlight colors

Poof, all the junk is gone leaving just my highlighted text and notes!

Saturday, June 3, 2017

Starting VS debugger when you can't attach to process

I have a VB6 program that uses my .NET DLL. In an initializing function (before the UI is shown) a problem is happening that I need to debug by stepping into the .NET code. This is made difficult though, because i cannot Attach to Process 

Use System.Diagnostics.Debugger.Launch in the .NET code. When I use this, i no longer need to try to use Attach to Process

Saturday, May 20, 2017

Assembly.LoadFrom does not load types within a DLL

I'm using Assembly.LoadFrom to load an assembly during runtime and it's not able to load all of the types.

A useful symptom is that RequestingAssembly (in Resolve event) is null.

DLL names cannot match the containing folder.

For example, if I am trying to load ../Project/MyDLL/MyDLL.dll, it'll fail, simply because "MyDLL" is both the DLL name and folder name. The solution is therefore to put MyDLL.dll in a differently named folder.

Yes, this makes no sense, but I am not concerned about the internal workings of Assembly.LoadFrom(). After many hours of being confused, I finally found the solution here: . I face-palmed so hard!

Saturday, April 22, 2017

Sunk Cost Fallacy in software development

I am working on a project having to do with government certification. One of their requirements is very odd, so we've had to put time an effort into doing design and research into how we could best meet the odd requirement. Given that this is for a government certification, the requirement is set in stone (or so we thought...!). We thought we simply misunderstood their requirement, and so we figured out two design alternatives - one that was simple based on what we thought they meant, and the other based on if they truly required this weird thing.

We got clarification and they truly required the weird thing. We accepted this, and spent 2 weeks on the design / proof of concept work that takes place before actual coding. That included review meetings with the top dogs in the company. So alot of time and effort was put into this design. 

The design was approved and the stars were aligning, code was about to be slapped into an IDE. But then at the last minute the government changed their requirement (weird, that never happens in software development!). They gave us a choice - we could do the weird requirement or the smart requirement, and we'd just need to tell them what way we chose. 

So now we're at a fork in the road:
  • Go ahead with the coding on the already-completed design, even though the road ahead was a long nasty one
  • Do the simple design approach, taking a day or two to go through the approval process again, and then proceed with the very simple coding

You'd think the obvious choice would be to do the simple design approach, but that's where the Sunk Cost Fallacy comes in. Time and energy was already spent on the first design, therefore we should continue to invest in this design! Nope. The time and energy spent on the WRONG design does not justify going forward with it. 

Sunk Cost Fallacy teaches us that the smart thing to do is cut our losses, and not be fooled into thinking that previous resources spent justifies continuing to throw resources at the wrong solution.

We did end up going with the simpler design, but that's because we were aware of the Sunk Cost trap, and were able to avoid it and make the wise decision.

Saturday, April 8, 2017

Operate on subsets of arrays using LINQ

LINQ Take() and Skip() make it easy to operate on subsets of an array, or any IEnumerable.

Sample problem: I want to know if a number is a contiguous sequence of digits in ascending order

Sample solution using Skip() and Take()
private static bool isAsc(int tester)
          char[] asc = tester.ToString().Replace("0", ":").ToCharArray();
           char prev = asc[0];
           foreach(char c in asc.Skip(1))
             if(prev != c)
              return false;
           return true;

if i didn't have Skip() available i would have written that like this:
for(int i = 1; i < asc.Length; i++)

Now, what if I wanted to find out if a subset had the property? Let's say, I want to know if the 2nd-5th digits have the property. In this case i would use Skip() + Take()

private static bool isAsc(int tester)
          char[] asc = tester.ToString().Replace("0", ":").ToCharArray();
           char prev = asc[0];
           foreach(char c in asc.Skip(1).Take(3))
             if(prev != c)
              return false;
           return true;

1. Code Wars kata i used Skip() on:

Friday, March 24, 2017

Improve your code and design intuition

You should be able to develop your code and design sense to the point where you can look at anyone's code (including your own) and intuitively know if it's good or not. This is a critical skill to have if you are responsible for doing code and design reviews.

Here's some suggestions based on what i've done

1. Study design patterns and antipatterns
-Read some design patterns book. I liked Head First Design Patterns
-Read AntiPatterns
-Read Refactoring

2. Study good code, code smells, and refactoring
-Read Code Complete
-Read Clean Code
-Read Refactoring
-Read Programming Pearls

One of my favorite references for this is here: I like to point to this in code reviews as a teaching tool. Like "use Extract Method for this part of the code."

3. Lots and lots of practice

-Katas (links from Clean Coder book)

-TopCoder algorithm challenges

-Code Wars

4. Use different languages and write different types of programs. Don't get too comfortable!
-Use C# at work? Study Python for fun
-Only do application development? Head to Udacity or freecodecamp for webdev

An excellent way to do this is to use 57 Exercises

Saturday, February 4, 2017

Refactoring challenge - yahtzee

Refactoring challenge

My end results

                                           Original           Refactored
Maintainablility                  61                        90
Cyclometric Complexity    64                        47
Lines of Code                     157                      86

Class Coupling                    0                         11

My refactored C# version of this

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RefactoringPractice { public enum DieValue { One =1, Two=2, Three=3, Four=4, Five=5, Six=6 } public class Dice { private static int NUM_DIE = 5; private int[] dice = new int[NUM_DIE]; private static DieValue[] dieValues = (DieValue[])Enum.GetValues(typeof(DieValue)); public Dice(int die1, int die2, int die3, int die4, int die5) { dice[0] = die1; dice[1] = die2; dice[2] = die3; dice[3] = die4; dice[4] = die5; } internal int Sum() { return dice.Sum(); } internal int SumOfNum(DieValue val) { return whereVal(val).Sum(); } private IEnumerable<int> whereVal(DieValue val) { return dice.Where(e => e == (int)val); } internal int CountOfNum(DieValue val) { return whereVal(val).Count(); } internal bool AllDieHaveNum(DieValue val) { return CountOfNum(val) == NUM_DIE; } internal static DieValue[] DieValues { get { return dieValues; } } } public static class Yatzy { public static int Chance(Dice dice) { return dice.Sum(); } public static int yatzy(Dice dice) { foreach(DieValue val in Dice.DieValues) { if (dice.AllDieHaveNum(val)) return 50; } return 0; } public static int Ones(Dice dice) { return dice.SumOfNum(DieValue.One); } public static int Twos(Dice dice) { return dice.SumOfNum(DieValue.Two); } public static int Threes(Dice dice) { return dice.SumOfNum(DieValue.Three); } public static int Fours(Dice dice) { return dice.SumOfNum(DieValue.Four); } public static int Fives(Dice dice) { return dice.SumOfNum(DieValue.Five); } public static int Sixes(Dice dice) { return dice.SumOfNum(DieValue.Six); } public static int ScorePair(Dice dice) { foreach(DieValue val in Dice.DieValues.Reverse()) { if (dice.CountOfNum(val) == 2) return (int)val * 2; } return 0; } public static int TwoPair(Dice dice) { int numPairs = 0; int score = 0; foreach(DieValue val in Dice.DieValues.Reverse()) { if(dice.CountOfNum(val) >= 2) { numPairs++; score += ((int)val * 2); } } if (numPairs == 2) return score; else return 0; } public static int FourOfAKind(Dice dice) { return OfAKind(4, dice); } private static int OfAKind(int num, Dice dice) { foreach (DieValue val in Dice.DieValues) { if (dice.CountOfNum(val) >= num) return (int)val * num; } return 0; } public static int ThreeOfAKind(Dice dice) { return OfAKind(3, dice); } private static int straight(int left, int right, Dice dice) { for (int i = left; i <= right; i++) { if (dice.CountOfNum(Dice.DieValues[i]) != 1) return 0; } return dice.Sum(); } public static int SmallStraight(Dice dice) { return straight(left: 0, right: Dice.DieValues.Length - 2, dice:dice); } public static int LargeStraight(Dice dice) { return straight(left: 1, right: Dice.DieValues.Length - 1, dice: dice); } public static int FullHouse(Dice dice) { foreach(DieValue threeKind in Dice.DieValues.Reverse()) { if(dice.CountOfNum(threeKind) == 3) { foreach(DieValue twoKind in Dice.DieValues.Reverse()) { if(dice.CountOfNum(twoKind) == 2) { return ((int)threeKind * 3) + ((int)twoKind * 2); } } } } return 0; } } }
There was an error in this gadget