Blog

Codefund: Ethical Google Adsense Alternative?

In this article I’ll have a look at Codefund, which is an online advertising platform specifically targeted at the tech sector. So if you’re looking to monetize your tech blog, the documentation of you open source project or similar, this might be a possibly solution for you.

Continue reading “Codefund: Ethical Google Adsense Alternative?”

Music Monday: Sabaton, Combichrist, RAMMSTEIN, Storm Seeker

This Music Monday is going to be a bit different, because it’s not about one artist or band, but a few different ones (because with the old model, I’ll never get through them all).

Continue reading “Music Monday: Sabaton, Combichrist, RAMMSTEIN, Storm Seeker”

Cookies, sessionStorage, localStorage. What’s the difference?!

This post is about different storage possibilities in your browser. There are cookies, and different kinds of storage that can be accessed through JavaScript APIs in your client side code. They’re used for authentication, tracking tools like Google Analytics and a lot of other stuff, so let’s have a look at how they work!

Continue reading “Cookies, sessionStorage, localStorage. What’s the difference?!”

Web Components 101: Hello World

Web Components are a way to encapsulate functionality into little self-contained parts that can be reused and adapted like you’re used to from frontend libraries like React, Vue, Angular or Ember. The special thing about Web components is that they work straight in your browser.

When it comes to support, a lot of features are supported in all major browsers, as can bee checked out at caniuse.com.

As you might have seen, web components usually are understood as several sub-parts, mostly:

Continue reading “Web Components 101: Hello World”

Unit Tests with Rust Tutorial 101

Writing unit tests is heavily integrated into rust without additional dependencies or external test runners. You can write unit tests inside the file that has the testable code or use a separate tests directory, let’s see how we do that.

You don’t need to have any prior rust knowledge, but you will have to have rust installed, which most easily is done by using rustup.

Writing Your First Test

When creating a rust project with cargo new and we’ll need a function that takes arguments and returns something, so let’s go for a simple sum function in main.rs:

fn sum(a: i32, b: i32) -> i32 {
    return a + b;
}

fn main(){
    println!("12 + 1 is {}", sum(12,1));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sum_test() {
        assert_eq!(345+5, sum(345, 5));
    }
}

Now for the interesting parts in the bottom of the file

  • #[cfg(test)] <- the test section attribute (see conditional compilation)
  • #[test] <- the test attribute for the individual test
  • assert_eq! macro to compare the two values

This is the simplest way to implement a unit test in rust, the next step is to look at how to move your functions and tests into another separate file.

Running cargo test should output something like:

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/rust_test_101-e2cf395340fbef91

running 1 test
test tests::sum_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Adding the following has the correct types, but it would cause the return value to be larger than i32 allows:

#[test]
#[should_panic]
fn greater_than_i32() {
    sum(2147483647,1); // this would be bigger than i32 allows
}

By using #[should_panic] we tell the test run to expect the following function to fail. This catches errors that usually crop up with:

thread 'main' panicked at 'attempt to add with overflow', src/main.rs:2:12

Now this is the easiest possible way to get started with writing tests in Rust, let me know if you want to know more in future posts! Don’t forget to check out the Rust Book Chapter on Testing as well.

What is a QR code?

QR codes are little pixelated images that can convey information beyond the pattern that you see with your eye. Usually that information is a little bit if text, like a web address, phone number or similar. They are commonly used where you don’t want to present anyone with instructions like:

  1. open the app called “something”
  2. click on this button
  3. type in “42lkj,nm3ikj25j900jkjlkjaeii”

Instead, you present them with a picture, that’s going to do that for them. For example to lead them to your website, follow you on social media or pay with a cryptocurrency.

Continue reading “What is a QR code?”

Bonding: Season 1

Bonding on Netflix is a show about a Dominatrix that hires her reluctant best (and only) friend to be her assistant.

The show looks amazing, I mean still frames of the main characters could basically be paintings, but it’s not the deepest of shows and loaded with stereotypes. Fortunately, for me, the show doesn’t take itself too seriously in my opinion.

Among sex workers and the BDSM scene the show has gotten some critique, which The Rolling Stone sums up nicely, all justified from my point of view, if you want to know what’s wrong with it.

For Denmark it might be silly and harmless show, but lots of places in the world I bet the show can still lift a bit of stigma and cause conversations, which is why I’m mentioning it (HELLO AMERICAN READERS!).

Check out Bonding, if you’re looking for a mostly funny show about sex and two young adults struggle in the gig culture.

What is an Interface?

An interface is something I first stumbled upon when I started learning compiled languages. I had read the term a couple of times before, but really only with C, Java and lately Go an interface actually means something to me that I can remember.

I’ve used the abbreviation API about one thousand times in my life, which stands for Application programming interface, usually I mean web APIs, where I get some JSON to feed it to my frontend or something, but that’s already a very specific use case.

As with many words in computer science, interface can mean many things, but depending on context it means a very specific thing. To quote wikipedia:

In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information.

and more specifically regarding programming:

the term interface is used to define an abstract type that contains no data but defines behaviours as method signatures.

So an interface is a bit like a puzzle piece. You can’t just attach your code or your function anywhere, but you need to find the right part that’s sticking out.

Implementing / Satisfying Intefaces

To implement or to satisfy an interface means to comply with the rules set by the blueprint. In object oriented languages such as C# or Java, interfaces are abstract classes, that just set some requirements for which functions must be included and which types they can receive or return.

Since Golang is not an object oriented language, there are no classes to implement, but we can still satisfy an interface with our type and it will be checked automatically by the compiler. Let’s pretend we are programming a series of vending machines and instead of a <Product> type, they’re supposed to return a string:

package main

import "fmt"

type VendingMachine interface {
    ReturnItem() string
}

type Cola struct {
}

func (d Cola) ReturnItem() string {
    return "you get a cooled cola"
}

func main() {
    var vend1 VendingMachine
    vend1 = Cola{}
    fmt.Println(vend1.ReturnItem())
}

You can see that type VendingMachine interface creates the first blueprint interface and the Cola struct now will need to satisfy the interface by also having a ReturnItem function.

We test this by creating a variable vend1 of type VendingMachine and assign a Cola{} struct to it.

The expected output is: you get a cooled cola

If we remove the following code:

func (d Cola) ReturnItem() string {
    return "you get a cooled cola"
}

We get the error:

# command-line-arguments
./main.go:21:8: cannot use Cola literal (type Cola) as type VendingMachine in assignment:
        Cola does not implement VendingMachine (missing ReturnItem method)

Thanks Go! Helpful! This way we can’t forget the essential ReturnItem function in our VendingMachine and make our customers really angry at us, because no products come out after they payed.

Similarly, if we change the return type of the Cola ReturnItem to int:

./main.go:13:9: cannot use "you get a cooled cola" (type string) as type int in return argument
./main.go:25:8: cannot use Cola literal (type Cola) as type VendingMachine in assignment:
        Cola does not implement VendingMachine (wrong type for ReturnItem method)
                have ReturnItem() int
                want ReturnItem() string

This makes sure that we return the correct type as well, because the humans or possibly robots interfacing with our vending machine will expect strings, not integers to come out.

Lastly, when coming from other languages you might try to implement a go interface by using the interface to create a type instance, maybe like this:

vend1 := VendingMachine{Cola{}

However, since the implements checking is done by the compiler and abstracted away, you’ll only get an error about a composite literal:

./main.go:17:25: invalid type for composite literal: VendingMachine

Summary

Interfaces are handy tools to ensure that your application can have consistently grouped functionalities with different values, even in a non OOP language like Go.

Real world examples of interfaces are the Readers and Writers in Go are IO parts like the Writer or Reader which you can try out and extend for some simple file system operations if you’re interested.

Fuck You, Pay Me. [Video]

I came across this wonderful video a few weeks ago and it’s more true than ever. It has a couple of valuable lessons for anyone who’s freelancing or trying to run a business without bankrupting themselves.

Mike Monteiro runs a design company and takes you on a bit of a journey on when they hang up the phone because the other party brought their lawyer, but they didn’t.

The key lessons for me are: Lawyers will save you money. The higher you climb the “big client” or “big game” ladder, the more people will test you and try to stretch you, especially if you’re inexperienced.

I’ve started to love contracts, obviously short and straight forward and most of all honest, simply because it’s a written reminder what everybody has to do and who gets what as a delivery. I’m a fan of saying hard things early in the process, so writing up a contract is a good exercise for that. It’s, at least it’s supposed to be, a simple and honest description of the business relationship.

Do yourself and everybody a favour and write contracts, don’t start working without the startup fee. If you can’t afford to walk away from something, that’s where your job will wear you down and it’s the most painful. Trust me, I know.