7 Top Reasons for Introducing Technical Debt

Mark Heath:

But I would argue, that more often than not, as developers, we believe we are making a justifiable decision. In over 20 years as a software developer I don’t recall ever having a discussion saying “let’s be lazy and irresponsible and get this finished as quickly as possible”. Instead, there is a quite reasonable sounding rationalization every time we choose the quick way.

Can Enum return string instead of integer value?

Spoiler: Answer is NO!

Current implementation C# Enum is very primitive; it can only return integer value for the selected enumerator option. If your project requirement needs something like returning a string for Enum, you have to come up with your own solution.

You cannot declare enum like this:

1
2
3
4
5
6
7
// enum type can't have string value
public enum WorkerType
{
  Employee = "WT001",
  Contractor = "WT003",
  Resigned = "WT099"
}

One solution usually popular is to declare Description attribute to each enumerators (for example, [Description("WT003")] for Contractor) and use it to store a respective string value. This is more of a hack work to as intend of Description attribute is to provide description for enumerators for documentation purpose only. With that, you then also require to use reflection to pull the string from Description attribute. This is not a clean solution.

I think the best way to implement the solution is through creating a dedicated class instead and provide all implementation underneath to make it behave like an enum.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class WorkerType
{
  private WorkerType(string value)
  {
    Value = value;
  }

  public string Value { get; }

  public static WorkerType Employee => new WorkerType("WT001");
  public static WorkerType Contractor => new WorkerType("WT003");
  public static WorkerType Resigned => new WorkerType("WT099");
}

class Program
{
  static void Main(string[] args)
  {
    WorkerType workerType = WorkerType.Contractor;
    Console.WriteLine($"Contractor Value: {workerType.Value}");
  }
}

We set the Constructor of the class as private which will keeping this class initiated anywhere in the project. All this class is exposing is defined members and Value property in which are interested in.

We can then further extend this class by overriding Equals and implementing == and != operators. Complete implementation will look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;

namespace ConsoleApp
{
  public class WorkerType
  {
    private WorkerType(string value)
    {
      Value = value;
    }

    public string Value { get; }

    public static WorkerType Employee => new WorkerType("WT001");
    public static WorkerType Contractor => new WorkerType("WT003");
    public static WorkerType Resigned => new WorkerType("WT099");

    public override bool Equals(Object obj)
    {
      return obj is WorkerType && this.Value == ((WorkerType)obj).Value;
    }

    public static bool operator ==(WorkerType x, WorkerType y)
    {
      return x.Value == y.Value;
    }

    public static bool operator !=(WorkerType x, WorkerType y)
    {
      return !(x == y);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      WorkerType workerType = WorkerType.Contractor;
      Console.WriteLine($"Contractor Value: {workerType.Value}");

      if (workerType.Equals(WorkerType.Contractor))
      {
        Console.WriteLine("And it matches using Equals method.");
      }

      if (workerType == WorkerType.Contractor)
      {
        Console.WriteLine("Yea! It also matches using == Operator.");
      }

      if (workerType != WorkerType.Resigned)
      {
        Console.WriteLine("And also with != Operator.");
      }

      Console.ReadKey();
    }
  }
}
Works like Enum!

Once we establish the pattern using the above implementation, it’s just a matter of replicating this pattern for other complex enums which has this string return value requirements. I hope this help!

More productive Git

These tips and techniques by James Turnbull will help you to avoid potential source control pitfalls and make your development workflow bit smoother.

Tip # 6: I am bisect and so can you

git bisect is one of the most powerful, seemingly magical, code-based debugging tools available to you. It can be used when you discover a bug you can’t trace to a specific piece of code. A git bisect runs a binary search between two commits: a good commit where the bug wasn’t present and a bad commit where the bug appears.

$ git bisect start
$ git bisect bad                 # Current version is bad
$ git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good

Chrome Extension - DIV Glance

I am so happy to see my first Chrome extension published on the Chrome Web Store. The whole intension of this releasing the extension was to understand Chrome extension development workflow and extension publishing process. The whole process to upload my extension was simple and straight forward even for beginners like me.

Now about the DIV Glance Chrome extension itself. This extension will give you a visual glance of the DIV tags in the page. Just hover your mouse pointer over the page and extension will highlight DIV under the pointer with the Red background color. The intensity of Red background color signifies the depth of highlighted DIV related with regards to its parent DIV layers. You no more have to dig through Chrome DevTools to inspect DIV layout in the page.

DIV Glance

DIV Glance Extension Page: DIV Glance

As I mentioned above, I created this extension just for the sake of learning Chrome extension development. As I saw the practical usage of this extension later, I tried to polish it a bit to make it more presentable. At the core of this extension, it simply injects following CSS style to the page — nothing fancy.

1
2
3
4
5
6
7
<style type="text/css">
  div:hover
  {
    outline:1px solid red;
    background-color:#ff000010;
  }
</style>