## Dirty Code – An Update

You might remember my little hack to sort list elements by their values:

```TreeMap<Double, Integer> tree = new TreeMap<>(Collections.reverseOrder());
for (int i = 0; i < customerNumber; i++) {
tree.put(requirement(i) + Math.random(), i);
}
List<Integer> orderedCustomers = new ArrayList<>(tree.values());
```

After a lot of research, I finally found a more elegant solution. First of all, I’m using a `PriorityQueue` instead of a tree. `PriorityQueues` allow duplicates, which is preferrable in this case since values are not guaranteed to be unique. Secondly, implementing a custom `Comparable` ensures a clear and readable structure.

```class Element implements Comparable<Element> {

private final int key;
private final int value;

Element(int key, int value) {
this.key = key;
this.value = value;
}

@Override
public int compareTo(Element anotherElement) {
return compare(getValue(), anotherElement.getValue());
}

private int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

public int getKey() {
return key;
}

public int getValue() {
return value;
}
}
```

That’s it. This solution is still too complicated for my taste, but at least it’s easy to understand.

## Dirty Code

It’s official: I just wrote the ugliest method I’ve ever written.

## Homework

As you might have noticed, I’m still working on a few unfinished projects. Nevertheless, I needed to squeeze a new one in: The final homework for my visual computing class.

The instruction was simple: Use blender and model something. Sounds easy, but we are talking about a beginner’s class. Most students haven’t really used blender before, including me. So luckily, nobody expected amazing art from me. After all, the exercise was meant to help students to get to know the program a bit better.

I decided to model things I keep on my desk:

## Meta: Sharing

In case you were wondering as well: When your share your WordPress blog posts on social media, your favicon will be used as default image.

It took me longer to figure that out than I’d care to admin. But well, now I know.

## Text, Text, Text

In my opinion, choosing the right tool is often half the battle. This is especially true when it comes to writing. Of course, it’s always possible to use a popular WYSIWYG-program. However, most of the time, there’s a better option.

If you are fed up with typical “Office programs”, one of the following two systems might be for you.

## Understanding Algorithms

I’m currently taking a class on algorithms. While practising a bit, I found a cool trick: Most algorithms are easier to read when written in Python instead of pseudocode.

Why? Pseudocode doesn’t follow exact rules. Python’s syntax is very similar to pseudocode, but strictly regulated. Also, most style guides and best practices focus on readability. Python code is predictable; you get exactly what you expect. That’s why Python is perfect for studying algorithms.

You don’t believe me? Take a look at the Euclidean algorithm:

```function gcd(a, b)
while b ≠ 0
t := b;
b := a mod b;
a := t;
return a;
```

Here’s the same algorithm in Python:

```def gcd(a, b):
while b:
a, b = b, a % b
return a
```

The second one requires less thinking, doesn’t it?

Of course, this trick also works on complicated, less famous algorithms. Continue reading “Understanding Algorithms”

## Keep It Simple

In my opinion, simple designs are harder to get right than complicated ones. Still, I felt that my first web app needed to be minimal in order to not take away from the Identicons presented there. Here’s what I did.

## Setting Up a Python Web App

Python is very well documented, but there are still some tricky parts when it comes to setting up a web app. This simple step-by-step guide might help you.

#### Step 0: Make a Project Folder

This one should be obvious.

#### Step 1: Create a Virtual Environment

Virtual environments allow you to install things for one project only, so you can easily keep track of all requirements for your app. Additionally, you avoid installing stuff globally.
You don’t have to use a virtual environment, but it is strongly recommended. It’s also very easy to set up:

• If you haven’t done so before, install virtualenv with the command
`pip install virtualenv `
• Create a virtual environment with the command
`virtualenv virtual_environment_name `

There are two different recommendations where to create a virtual environment and how to call it:

• call it venv and put it in your project folder
• call it the same as your project and put it in a hidden .virtualenv folder in your home directory

``` ```Just choose the method you like best.

## My First Web App

The holidays are here! This means that there is finally enough time to develop my first web app. Knowing how bad I still am at debugging weird errors and how little experience I have with frameworks, I prepared myself for a nerve-racking experience full of apparently insurmountable obstacles.

First of all, I researched a few popular python frameworks, services and tools to find the best options for me. Since I prefer lightweight and modern frameworks, Flask was an obvious choice. Flask also supports Jinja2 Templating out of the box, which is great. For deployment, I picked Heroku because it seems to be quite beginner friendly.

## Unique != Distinguishable

Had you asked me a week ago, “What’s the most difficult part about generating identicons?”, I would have answered, “Making them look good, obviously.”
Turns out I was wrong. It’s actually quite easy to configure an image generator to produce beautiful images: Take care your colors don’t clash and you should be fine.

The problem lies somewhere else. It’s obvious and easy to overlook at the same time: Images need to be complicated enough to make them unique. At the same time, complexity makes them harder to distinguish. Sadly, this only occurred to me when my image generator was fully implemented and I tested it with some strings.