cd’s long lost sibling finally here!

Posted by Michał ‘mina86’ Nazarewicz on 2nd of June 2024

cd is a straightforward command. As per the name, it changes the directory and does its job perfectly well. But what if it could do more? One scenario is wanting to execute a command inside a specific location without affecting the current working directory (CWD). This article introduces a cd replacement which offers that feature as well as provides more ways to specify the target directory.

It is important to note that it’s not intended for scripting. Rather, it’s only meant for interactive use where it streamlines some operations.

Demystifying the jargon: free software vs open source

Posted by Michał ‘mina86’ Nazarewicz on 19th of May 2024

Some people struggle to understand the distinctions between ‘free software’ and ‘open source software.’ Let’s clear up the confusion with an analogy.

Imagine a world without vegetarianism. One day, someone proposes a new diet called ‘moral eating,’ which excludes meat for ethical reasons. Some people embrace it, and discover additional benefits like reduced environmental impact. However, advocates observe that implying people not adhering to the diet are immoral isn’t the best recruitment strategy. They coin the term ‘sustainable eating’ to focus on the environmental advantages.

But now people get bogged down in philosophical debates. If one uses the term ‘moral eating’ some assume they don’t care about the environment; on the other hand, if one says ‘sustainable eating’ some assume they don’t care about animals. To avoid this an all-encompassing acronym MSE (Moral and Sustainable Eating) is created. It signifies the same thing — no meat — but avoids getting entangled in justifications.

And so we end up with three distinct terms — moral eating, sustainable eating and MSE — which all refer to the same diat. What we call vegetarianism.

You’re implementing fmt::Display wrong

Posted by Michał ‘mina86’ Nazarewicz on 12th of May 2024

TL;DR: When implementing Display trait for a wrapper type, use self.0.fmt(fmtr) rather than invoking write! macro. See The proper way section below.

RAII: Tragedy in three acts

Posted by Michał ‘mina86’ Nazarewicz on 1st of April 2023

In a recent Computerphile video, Ian Knight talked about RAII idiom and it’s application in C++ and Rust. While the video described the general concepts, I felt different examples could be more clearly convey essence of the topic.

I’ve decided to give my own explanation to hopefully better illustrate what RAII is and how it relates to Rust’s ownership. Then again, for whatever reason I’ve decided to write it as a play with dialogue in faux Old English so it may well be even more confusing instead.

Monospace considered harmful

Posted by Michał ‘mina86’ Nazarewicz on 19th of March 2023

No, I haven’t gone completely mad yet and still, I write this as an appeal to stop using monospaced fonts for code (conditions may apply). While fixed-width fonts have undeniable benefits when authoring software, their use is excessive and even detrimental in certain contexts. Specifically, when displaying inline code within a paragraph of text, proportional fonts are a better choice.

URLs with // at the beginning

Posted by Michał ‘mina86’ Nazarewicz on 4th of December 2022

A quick reminder that relative URLs can start with a double slash and that this means something different than a single slash at the beginning. Specifically, such relative addresses are resolved by taking the schema (and only the schema) of the website they are on.

For example, the code for the link to my repositories in the site’s header is <a href="//github.com/mina86">Code</a>. Since this page uses https schema, browsers will navigate to https://github.com/mina86 if the link is activated.

This little trick can save you some typing, but more importantly, if you’re developing a URL parsing code or a crawler, make sure that it handles this case correctly. It may seem like a small detail, but it can have a lasting impact on the functionality of your code.

Secret command Google doesn’t want you to know

Posted by Michał ‘mina86’ Nazarewicz on 20th of November 2022

Or how to change language of Google website.

If you’ve travelled abroad, you may have noticed that Google tries to be helpful and uses the language of the region you’re in on its websites. It doesn’t matter if your operating system is set to Spanish, for example; Google Search will still use Portuguese if you happen to be in Brazil.

Fortunately, there’s a simple way to force Google to use a specific language. All you need to do is append ?hl=lang to the website’s address, replacing lang with a two-letter code for the desired language. For instance, ?hl=es for Spanish, ?hl=ht for Haitian, or ?hl=uk for Ukrainian.

If the URL already contains a question mark, you need to append &hl=lang instead. Additionally, if it contains a hash symbol, you need to insert the string immediately before the hash symbol. For example:

  • https://www.google.com/?hl=es
  • https://www.google.com/search?q=bread+sandwich&hl=es
  • https://analytics.google.com/analytics/web/?hl=es#/report-home/

By the way, as a legacy of Facebook having hired many ex-Google employees, the parameter also work on some of the Facebook properties.

Curious case of missing π

Posted by Michał ‘mina86’ Nazarewicz on 28th of June 2022

π is one of those constants which pops up when least expected. At the same time it’s sometimes missing when most needed. For example, consider the following application calculating area of a disk (not to be confused with area of a circle which is zero):

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
	for (int i = 1; i < argc; ++i) {
		const double r = atof(argv[i]);
		printf("%f\n", M_PI * r * r);
	}
}

It uses features introduced in the 1999 edition of the C standard (often referred to as C99) so it might be good to inform the compiler of that fact with a -std=c99 flag. Unfortunately, doing so leads to an error:

$ gcc -std=c99 -o area area.c
area.c: In function ‘main’:
area.c:8:18: error: ‘M_PI’ undeclared (first use in this function)
    8 |   printf("%f\n", M_PI * r * r);
      |                  ^~~~

What’s going on? Shouldn’t math.h provide the definition of M_PI symbol? It’s what the specification claims after all. ‘glibc is broken’ some may even proclaim. In this article I’ll explain why the compiler conspire with the standard library to behave this way and why it’s the only valid thing it can do.

Pro tip: You can put URLs in C & C++ code

Posted by Michał ‘mina86’ Nazarewicz on 1st of April 2022

Documenting source code is important part of software engineering. Code is read more often than it’s written making it crucial to provide enough context for reader to understand what the implementation is doing. This can come in the form of links to external resources providing description of an algorithm, reference for an API or historic context justifying the code.

As it turns out, C and C++ languages offer a little-known feature which allows URLs to be included directly in the function source code. For example:

static float rsqrt(float x) {
	https://en.wikipedia.org/wiki/Fast_inverse_square_root
	static_assert(std::numeric_limits<float>::is_iec559);
	auto i = std::bit_cast<uint32_t>(x) >> 1;
	auto y = std::bit_cast<float>(UINT32_C(0x5F375A86) - i);
	y *= 1.5f - x * 0.5F * y * y;
	return y;
}

Primes ≤ 100 in Rust

Posted by Michał ‘mina86’ Nazarewicz on 20th of June 2021

In a past life I’ve talked about a challenge to write the shortest program which prints all prime numbers less than a hundred. Back then I’ve discussed a 60-character long solution written in C. Since Rust is the future, inspired by a recent thread on Sieve of Eratosthenes I’ve decided to carry the task for Rust as well.

To avoid spoiling the solution, I’m padding this article with a bit of unrelated content. To jump straight to the code, skip the next block of paragraphs. Otherwise, here’s a joke for ya: