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 https://read.amazon.com/kp/notebook

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

Problem
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 

Solution
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

Problem
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.

Solution
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: http://stackoverflow.com/a/10245012/1538717 . 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))
           {
             prev++;
             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))
           {
             prev++;
             if(prev != c)
              return false;
           }
           return true;
        }


Ref:
1. Code Wars kata i used Skip() on: https://www.codewars.com/kata/52c4dd683bfd3b434c000292







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: https://sourcemaking.com. I like to point to this in code reviews as a teaching tool. Like "use Extract Method for this part of the code. https://sourcemaking.com/refactoring/extract-method"

3. Lots and lots of practice

-Katas (links from Clean Coder book)
http://codekata.pragprog.com 
http://katas.softwarecraftsmanship.org
http://butunclebob.com/
http://thecleancoder.blogspot.com/2010/10/craftsman-62-dark-path.html

-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 https://pragprog.com/book/bhwb/exercises-for-programmers




There was an error in this gadget