Good behavior with bad consequences

Most companies emphasize on good behavior, being friendly to everybody in order to maintain respect and a calm working environment. Which is really great until they don’t speak up and avoid pointing out flaws.

We all have flaws. Not only things that we should improve, but pure flaws, things we don’t do the proper way. Things that we do wrong! And when you’re not told you’re wrong, you’re not going to improve. If you don’t improve, projects suffer.

Now imagine big companies with a lot of people who are not told what to improve, who are overly protected and bad things are hidden from them. Year after year. They have many directions they can’t level up in.

Being told you’re wrong shouldn’t be offensive to you, instead should raise a flag for yourself and make you ask questions, find answers, learn.

Converting seconds from float to int

Another small one on time precision that I’ve noticed.

package main

import (
	"fmt"
	"math"
	"time"
)

func main() {
	now := time.Now().Add(time.Hour)
	seconds = time.Now().Sub(now).Seconds()
	fmt.Println(int(seconds))
	fmt.Println(int(math.Floor(seconds)))
}

Line 12 will print -3599, line 13 will print -3600 (tested on Ubuntu). So watch out when converting the number of seconds to an integer, you might not always get what you need.

Time precision on Linux and Windows

Unit tests were needed on a new project I’m working on and a few weeks ago I wrote some. Last week, a colleague wrote some more, and when he ran all of them, mine were failing. Same code base, up to date, same tests. On my machine they were still passing. We jumped on the situation and saw math.Floor was giving a result on my machine, which runs on Ubuntu, and another one on his, on Windows.

The code is larger, but I’ve extracted and adapted what’s needed:

package main

import (
	"time"
	"math"
)

func main() {
	datetime := time.Now().Add(time.Hour * 24 * 7 * 4 * 12 * 3)
	seconds := -1 * int(time.Now().Sub(datetime).Seconds())
	a := 29030400
	x := float64(seconds)/float64(a)

	println("input:", x, "floor:", math.Floor(x))
}

Result on Ubuntu:

input: +3.000000e+000 floor: +2.000000e+000

Result on Windows:

input: +3.000000e+000 floor: +3.000000e+000

Continue reading Time precision on Linux and Windows

Language X is easy to switch to

I’ve heard “it’s easy to switch to a new language, it’s just a language, you just learn a new syntax”.

Well, I partially disagree. While you’ll may be able to learn the syntax fast, each language has its own particularities that you don’t just understand in a few days. Some languages have other best practices than others.

I believe that if you jump on writing code in a new language without understanding its philosophy, you’ll miss a lot of edge cases that will lead to undesired situations. Most probably no one will die, but time will be lost.

“We chose Node because we already had JavaScript developers, and it’s the same thing” – Yeah, server side optimization is the same thing. Same for concurrency. Or properly handling the file system.

“I took a look at Go and it’s trivial” – And a few months later you’ll end up tangled in your own work, refactoring each step.

While onboarding to the new project you have with that new language, I suggest  reading the docs, understanding the language’s best use, maybe right from the authors.