What is wrong with this code? Part 3.

Index

Introduction

Recently I participated in the interview and one of the questions reminded me about the situation I was part of a long time ago. At that moment I was working on the backend Web API to process datasets of various sizes – string from 1.000 to 100.000 items.

One of the processing steps was to group items by key.

Let’s imagine a DataItem class…

public class DataItem
{
    public int Key { get; }
    public string Value { get; }

    public DataItem(int key, string value)
    {
        this.Key = key;
        this.Value = value;
    }
}

… and the code I wrote was something like this:

DataItem[] items;

// more...

this.items.GroupBy(i => i.Key)

Usually when we think "How good this code is?" we’re trying to rate it*:

  • Usage of TABS vs SPACES
  • Class, Methods, Locals naming
  • Code Style
  • Readability & Maintainability
  • Performance (time to execute, memory consumption etc.)

* the items are placed in order of importance.

While all of the above is really important let’s try to analyze code from Performance point of view.

So… What is wrong with this code?

Continue reading “What is wrong with this code? Part 3.”

What is wrong with this code? Part 2.

Index

Introduction


There are lots of active discussions around application performance so I decided to do join the rumble and start a small series of “What is wrong with this code?” posts about interesting things I see in everyday development.

This post was inspired by this tweet by Matt Warren. It reminded me about one curios case I’ve faced in past when I was optimizing web service performance.

Continue reading “What is wrong with this code? Part 2.”

What is wrong with this code?

Index

Introduction


This post originates from the discussion in Twitter started by this tweet by David Fowler.

Inside the tweet there was the following code snippet:

string request = ReadAsString(request.Body);
T obj = JsonConvert.DeserializeObject(request);

It turned into an interesting discussion which reminded me about a code I personally saw numerous times in production:

var list = new List<string>
{
  "item0",
  "item1",
  "item2",
  "item3",
  "item4",
  "item5",
  "item6",
  "item7",
  "item8"
};

// Code with potential usage of List (Add / Remove etc.)

Looking at this code what can you say about it? How do you think – what is wrong with it?

If you are willing to know – Let’s find out.

Continue reading “What is wrong with this code?”