CodeSOD: True Confession: Without a Map

This post was originally published on this site

The Daily WTF

Today, we have the special true confession from Bruce, who wrote some bad code, but at least knows it’s bad.

Bruce is a C# developer. Bruce is not a web developer. Someone around the office, though, had read an article about how TypeScript was closer to “real” languages, like C#, and asked Bruce to do some TypeScript work.

Now, in C# parlance, your key/value pair data-structure is called a Dictionary. So, when Bruce got stuck on how to store key/value pairs in TypeScript, he googled “typescript dictionary”, and got no useful results.

Disappointed, Bruce set out to remedy this absence:

export class KeyValuePair<TKey,TValue> { Key: TKey; Value: TValue; constructor (key: TKey, value: TValue) { this.Key = key; this.Value = value; } } export class Dictionary<TKey, TValue>{ private Collection: Array<KeyValuePair<TKey, TValue>> private IndexMap: Map<TKey, number> private index: number; public tryAdd(key: TKey, value: TValue): boolean { if (this.containsKey(key)) { return false;

To read the full article click on the 'post' link at the top.