Posts

On Dependency Injection

I just read an article about eliminating dependencies whose basic thesis is that instead of having a class that depends on something that can give you a value, just depend on that value directly.His example (which you can read more fully in the article) is that instead of having public InvoiceGenerator(IConfigurationReader configurationReader) { _configurationReader = configurationReader; } and later on calling var watermarkText = _configurationReader.Get<string>("invoiceWatermarkText"); if (!String.IsNullOrEmpty(watermarkText)) to get the text we need, just request the text directly: public Invoice GenerateInvoice(string watermarkText) { if (!String.IsNullOrEmpty(watermarkText)) There are two obvious problems with this, which you will hit extremely quickly in real code.One of them, pointed out by a comment by LeszekP, is that most of the time you will require more than a single value from an interface. Worse, some o…

My views on blockchain / bitcoin

Going from abstract to concrete: Private currency: good. I would love to have lots of them, just as I love both gold and silver. Blockchain: not so good, more of a solution in search of a problem. The only advantage it has compared to an append-only database is decentralization… and I don't see a need for that. I would much rather have many private currencies. Also, in practice most coins are actually centralized, or at least a very small oligarchy. Bitcoin: nah. Really bad. Incredibly slow to initialize, slow transactions, easy to control by a sustained state-level effort, very low cap on number of transactions per second, plenty of bugs, hard-forks which means the code is actually controlled by a small group (which in turn means that I can't trust that the currency itself is decentralized)… nah.In conclusion, I like the initiative, and I'm 100% behind the idea that we need to separate states and money, but we're nowhere near yet.

The problem with null

The problem with null is that it pretends to be an object of a given type, without actually having that type. For example (C# code - ignore the uselessness of the GetName method): string GetName(Customer customer) { // I got a customer object, I can access the Name property return customer.Name; } var x = func(null); // <-- not a real Customer object so we have a run-time error The proper way to solve this is by using the Option (aka Maybe) monad; for an example using the Functional.Maybe NuGet package: string GetName(Maybe<Customer> customer) { // I don't actually have a Customer object, I have a Maybe<Customer> // I need to treat it carefully return customer.Select(it => it.Name).OrElse("Missing"); } var x = func(null); // compiler error, because Maybe<> doesn't allow null as a value var x = func(Maybe<Customer>.Nothing); // the customer is missing but the call will not crash Using the Option mon…

FanFiction

FanFictionPhrase from a book I'm reading: I was bitten by a radioactive Jedi as a child. The Havoc side of the Force

Fan-fiction recommendations

I posted this to HN and I realized it might be useful for other people. If at least one person who wasn't aware of fan-fiction discovers it as a result, this has served its purpose.For Buffy fans, TTH has a huge number of stories, both in-universe and crossovers with other worlds. Speakertocustomers, Becuzitswrong, Cordyfan, DianeCastle and Hotpoint are a few of my favorite authors. Hotpoint's crossover between X-COM and SG-1 is absolutely amazing.For other worlds, FF, SB, SV and AO3 are the most well-known sites for fanfiction. One of my favorite stories is Taylor Varga.What I normally do is I download the stories in the .mobi format - TTH and AO3 have that feature in the site, and for the others you can use the FanFicFare plugin for Calibre - and then upload them to my Kindle. This allows me to read in bed, which is bad for my sleep but I like it :)

Don't compare floats

Floating-point numbers are tricky; one of the first things a programmer needs to remember when working with them is to never check floats for equality. (I believe ReSharper warns you if you do that; I don't know if plain Visual Studio does because I never use it without ReSharper.)If precise representations of decimal numbers is needed, like when manipulating currencies, use decimals instead (if using C#); if a similar primitive type does not exist in your language, write a separate package / module / library to "fake it" by using integer values and scale them down by two or four digits, depending on your needs. (For example, the number 123456 can represent either 1,234.56 or 12.3456, depending on your application.)Here's a simple code example to show the difference between floats and decimals in C#: float f1 = 0.1f; float f2 = f1 * 10.0f; float f3 = 0.0f; for (var i = 1; i <= 10; i++)     f3 += f1; Console.WriteLine(f2); Console.WriteLine(f3); Console.WriteLine…

Evaluating an expression

Leaving a note to myself - a simple algorithm for evaluating expressions, no unary operators or parentheses. phase 1 (tokenizer) '+' => op1, add '-' => op1, sub '*' => op2, mul '/' => op2, div \d+ => number, value phase 2 (evaluator) tuple = first (number, op2, number) while tuple: replace tuple with result of op2 tuple = first (number, op1, number) while tuple: replace tuple with result of op1 there should be a single item left, a number; return its value