in Thoughts

Something about writing code

Projects are pretty fun. Currently, I restrict myself to small projects (less than 8 hours) because I don’t know if I have the motivation to do more. But I started a small project again today – this morning and it was great. I’ve written quite a few lines of code and they worked pretty fine.

I remember when I first started learning to program. I was happy if a line of code worked after my third try. It was pretty funny. Even a bit later when I first learned C++ I was happy if one or two lines worked without debugging.

One of the most demanding tasks was writing assembler on paper. Actually, this was even included in our finals. The programs were rather simple of course, e.g. a traffic light circuit but thinking about all the registers and stuff was pretty hard. Especially, because I couldn’t test the code.

One thing about writing code is familiarity. And this was one aspect I didn’t consider in the past. When I looked at source code from some experts in a field, e.g. from Peter Norvig I was always overwhelmed. Everything was so clever and well thought through. After I read more and more code from Norvig I’ve learned that he uses the same techniques in different pieces of code. I started to learn his techniques and applied them in my code. And whoosh suddenly it looked also pretty clever.

Also these people are exceptional and have tons of experience. Norvig is in the field over 20 years.

What helped me is learning functional programming languages. Especially my python code improved a ton when I learned Scheme and Haskell. I started to use more list comprehensions and thought more in functional terms. For me, that meant I thought about map, reduce, filter, zip, etc.

A simple example is that you need to manipulate a list of strings for example. The idea is that you want to replace each $ with USD.

A C-type Python-inspired code looks like this:

lst = ["23.56$", "8.43$", "$95.02", "$2.99"]

new_lst = []
for l in lst:
    new_lst.append(l.replace("$", "USD"))

I just didn’t include the very C-ish approach using an index.

A more pythonic approach – which I would probably use – looks like this:

lst = ["23.56$", "8.43$", "$95.02", "$2.99"]

new_lst = [l.replace("$", "USD") for l in lst]

Alternatively, a functional approach:

lst = ["23.56$", "8.43$", "$95.02", "$2.99"]

new_lst = map(lambda l: l.replace("$", "USD"), lst)

I normally, fall back to using map and its friend when I want very performant code because it’s faster.

And I see that a lot of beginners have problems writing pythonic code. It doesn’t help that their teachers often don’t know how to write pythonic code or just write C-like code in Python.

But still it has to do with habit and familiarity. When I’m familiar with a domain I know how to structure my code, how to write functions and how to solve problems. This results in two things: a) I’m pretty fast and b) the code is more robust and nicer.

I think my best-known area of expertise is bots or automation in the web field. Yeah, we say the latter is a better description. I just understand how the web works. How APIs work, how the HTTP protocol works, how JS works, etc. I can quickly do things which other people think is impossible.

It’s funny because I often heard old school hackers talk about younger people who wanted the source code to fix problems while they just disassembled the binary and fixed it there. In the same spirit I see people today who say you can’t write code for some service because they don’t have an API. I just write my own.

When I think back I started writing bots without even using a url library. Everything was sockets and I wrote my HTTP requests by hand. The same for cookie or session handling. Probably, not very people will that do today but I have a great advantage because of this. I have no fear of diving into a new protocol because I know how to deal with sockets. I have no fear of services without an API because I know how to write one myself. Pretty cool.

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.