and has 0 comments

  I don't remember where I got the idea of reading Prosper's Demon, but I am glad I did! Having listened to it on my headphones I got to that part where they list other works by the same author and so I've learned that K. J. Parker is a nom de plume for Tom Holt. I hadn't heard of him until then, but the list of works went on and on and on. Probably I will have to read some other books from him now, but which one should I start with?

  Prosper's Demon is a novella about an exorcist that wanders the land in order to excise demons from living people, often leaving those people hurt, dead or insane. You get little glimpses of what this means as the story progresses, making it more and more clear that you have to be a certain kind of person to do a job like this. I don't want to spoil the ending, but I have to say that the writing was so good that I really wouldn't have cared much how the story ended. I had fun just seeing this anti hero's character unfold in front of me. The world building was also exquisite, considering the short length of the work.

  Bottom line: really good fun, mixing together logic, science, art and philosophy with pure unapologetic mischief and good writing.

and has 0 comments

A few years ago I wrote an article about using RealProxy to intercept methods and properties calls in order to log them. It was only for .NET Framework and suggested you inherit all intercepted classes from MarshalByRefObject. This one is a companion piece that shows how interception can be done in a more general way and without the need for MarshalByRefObject.

To do that I am going to give you two versions of the same class, one for .NET Framework and one for .NET Core which can be used like this:

//Initial code:
IInterface obj = new Implementation();

//Interceptor added:
IInterface obj = new Implementation();
var interceptor = new MyInterceptor<IInterface>();
obj = interceptor.Decorate(obj);

//Interceptor class (every method there is optional):
public class MyInterceptor<T> : ClassInterceptor<T>
{
    protected override void OnInvoked(MethodInfo methodInfo, object[] args, object result)
    {
        // do something when the method or property call ended succesfully
    }

    protected override void OnInvoking(MethodInfo methodInfo, object[] args)
    {
        // do something before the method or property call is invoked
    }

    protected override void OnException(MethodInfo methodInfo, object[] args, Exception exception)
    {
        // do something when a method or property call throws an exception
    }
}

This code would be the same for .NET Framework or Core. The difference is in the ClassInterceptor code and the only restriction is that your class has to implement an interface for the methods and properties intercepted.

Here is the .NET Framework code:

public abstract class ClassInterceptor<TInterface> : RealProxy
{
    private object _decorated;

    public ClassInterceptor()
        : base(typeof(TInterface))
    {
    }

    public TInterface Decorate<TImplementation>(TImplementation decorated)
        where TImplementation:TInterface
    {
        _decorated = decorated;
        return (TInterface)GetTransparentProxy();
    }

    public override IMessage Invoke(IMessage msg)
    {
        var methodCall = msg as IMethodCallMessage;
        var methodInfo = methodCall.MethodBase as MethodInfo;
        OnInvoking(methodInfo,methodCall.Args);
        object result;
        try
        {
            result = methodInfo.Invoke(_decorated, methodCall.InArgs);
        } catch(Exception ex)
        {
            OnException(methodInfo, methodCall.Args, ex);
            throw;
        }
        OnInvoked(methodInfo, methodCall.Args, result);
        return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
    }

    protected virtual void OnException(MethodInfo methodInfo, object[] args, Exception exception) { }
    protected virtual void OnInvoked(MethodInfo methodInfo, object[] args, object result) { }
    protected virtual void OnInvoking(MethodInfo methodInfo, object[] args) { }
}

In it, we use the power of RealProxy to create a transparent proxy. For Core we use DispatchProxy, which is the .NET Core replacement from Microsoft. Here is the code:

public abstract class ClassInterceptor<TInterface> : DispatchProxy
{
    private object _decorated;

    public ClassInterceptor() : base()
    {
    }

    public TInterface Decorate<TImplementation>(TImplementation decorated)
        where TImplementation : TInterface
    {
        var proxy = typeof(DispatchProxy)
                    .GetMethod("Create")
                    .MakeGenericMethod(typeof(TInterface),GetType())
                    .Invoke(null,Array.Empty<object>())
            as ClassInterceptor<TInterface>;

        proxy._decorated = decorated;

        return (TInterface)(object)proxy;
    }

    protected override object Invoke(MethodInfo targetMethod, object[] args)
    {
        OnInvoking(targetMethod,args);
        try
        {
            var result = targetMethod.Invoke(_decorated, args);
            OnInvoked(targetMethod, args,result);
            return result;
        }
        catch (TargetInvocationException exc)
        {
            OnException(targetMethod, args, exc);
            throw exc.InnerException;
        }
    }


    protected virtual void OnException(MethodInfo methodInfo, object[] args, Exception exception) { }
    protected virtual void OnInvoked(MethodInfo methodInfo, object[] args, object result) { }
    protected virtual void OnInvoking(MethodInfo methodInfo, object[] args) { }
}

DispatchProxy is a weird little class. Look how it generates an object which can be cast simultaneously to T or Class<T>!

There are many other things one can do to improve this class:

  • the base class could make the distinction between a method call and a property call. In the latter case the MethodInfo object will have IsSpecialName true and start with set_ or get_
  • for async/await scenarios and not only, the result of a method would be a Task<T> and if you want to log the result you should check for that, await the task, get the result, then log it. So this class could make this functionality available out of the box
  • support for Dependency Injection scenarios could also be added as the perfect place to use interception is when you register an interface-implementation pair. An extension method like container.RegisterSingletonWithLogging could be used instead of container.RegisterSingleton, by registering a factory which replaces the implementation with a logging proxy

I hope this helps!

P.S. Here is an article helping to migrate from RealProxy to DispatchProxy: Migrating RealProxy Usage to DispatchProxy

and has 0 comments

  I wish I would have read Women when I was a teenager. I would have learned then that there is no shame in being a man and wanting women and not caring about their neuroses. They might complain about it, but that's their point of view. And in a way, that's the only good thing this book has to teach: how to live unapologetically, accepting who you are. Other than that, the main character, an alter ego of Charles Bukowski, is an old writer who drinks all day, fucks whatever he finds and gambles at the horse race track. By his own admission he only writes so he can do these three things.

  The prose is almost without introspection, just what people did and what they said, but when the character delves into analysing the situations, there are some brilliant passages, some hilariously funny. Imagine someone going through life like in a third person game. They see themselves do things, things that they just feels like doing, and feel little to no shame or responsibility. The entire book is dedicated to the women in his life which are, although very different from one another, easy, sexual, desperate and sometimes downright crazy. He never judges them, except for their sexual technique or pussy size and shape, but he never gets stuck with any of them, even the ones he is in love with.

  What I liked about the character is that he is never violent. Like a Big Lebowski kind of guy, he sails through life like a goose in water; nothing sticks. He loves and leaves, he cares but up to a point, he tries to be good to the people around him, but only after he takes care of his own needs. I had a friend like that once. He was caring and amazingly charismatic, but never right or reliable. Another similar character was Hank Moody, from Californication, which I really loved in the first two seasons.

  Did I love the book? I can't say that I did, but I did like it a lot. It was different from other things I've read and what I feel is probably gratefulness for having such a raw depiction of everything the character/the writer lived and felt. So many books are trying too hard to be smart and fail to pass that first bar of characters laid bare so the reader can fully understand them. There is absolutely no story in this other than the life of Henry Chinaski.

  Something else that I have to say is that the book is something that should be read in this period. It goes back to the basic essence of man and woman, without caring one iota about politics or correctness or trying to absolve the main character in any way. Just like the guy lives in the book, the writing is take it or leave it.

Definition

So, the task at hand is the subject of a common interview question: Implement an algorithm to get all valid (opened and closed) combinations of n pairs of parentheses. This means that for n=1 there is only one solution: "()". "((" or "))" are not valid, for 2 you will have "(())" and "()()" and so on. The question is trying to test how the interviewee handles recursion and what is commonly called backtracking. But as usual, there's more than one way to skin a cat, although for the life of me I can't see why you would want to do that.

The solutions here will be in C# and the expected result is an enumeration of strings containing open and closed parentheses. The code can be easily translated into other languages, including Javascript (ECMAScript 2015 introduced iterators and generator functions), but that's left to the reader. Let's begin.

Analysis

Before we solve any problem we need to analyse it and see what are the constraints and the expected results. In this case there are several observations that can be made:

  • the resulting strings will be of length n*2 (n pairs)
  • they will contain n '(' characters and n ')' characters
  • they cannot start with a ')' or end in a '('
  • in order to generate such a string, we can start with a smaller string to which we add '(' or ')'
  • we cannot add a ')' if there isn't at least one corresponding unclosed '(' 
  • if we add a '(' we need to have enough characters left to close the parenthesis, so the number of unclosed parentheses cannot exceed the characters left to fill
  • we could count the open and closed parentheses, but we only care about the number of unclosed ones, so instead of "closed" and "open" values, we can only use "open" to represent unclosed parentheses

Let's go for some variables and calculations:

  • n = number of pairs
  • open = number of unclosed parentheses in a string
  • open cannot be negative
  • one cannot add ')' if open = 0
  • one cannot add '(' if open >= n*2 - substring.length

Recursive solution

A simple implementation of these requirements can done with recursion:

public IEnumerable<string> GenerateRecursive(int n, string root = "", int open = 0)
{
    // substring is long enough, return it and exit
    if (root.Length == n * 2)
    {
        yield return root;
        yield break;
    }
    // if we can add '(' to existing substring, continue the process with the result
    if (open < n * 2 - root.Length)
    {
        // if only C# could just 'yield IteratorFunction()' this would look sleeker
        foreach (var s in GenerateRecursive(n, root + "(", open + 1))
        {
            yield return s;
        }
    }
    // if we can add ')' to existing substring, continue the process with the result
    if (open > 0)
    {
        foreach (var s in GenerateRecursive(n, root + ")", open - 1))
        {
            yield return s;
        }
    }
}

However, every time you see recursion you have to ask yourself: could n be large enough to cause a stack overflow? For example this fails for n=3000. The nice thing about this method, though, is that it can be limited to the number of items you want to see. For example var firstTen = GenerateRecursive(1000).Take(10) is very fast, as the generation is depth first and only computes the first ten values and exits.

So, can we replace the recursion with iteration?

Iterative solution

In order to do thing iteratively, we need to store the results of the previous step and use them in the current step. This means breadth first generation, which has its own problems. Let's see some code:

public IEnumerable<string> GenerateIteration(int n)
{
    // using named tuples to store the unclosed parentheses count with the substring
    var results = new List<(string Value,int Open)>() { ("",0) };
    for (int i = 0; i < n*2; i++)
    {
        // each step we compute the list of new strings from the list in the previous step
        var newResults = new List<(string Value, int Open)>();
        foreach (var (Value, Open) in results)
        {
            if (Open < n * 2 - Value.Length)
            {
                newResults.Add((Value + "(", Open + 1));
            }
            if (Open > 0)
            {
                newResults.Add((Value + ")", Open - 1));
            }
        }
        results = newResults;
    }
    return results.Select(r=>r.Value);
}

It's pretty sleek, but if you try something like var firstTen = GenerateRecursive(1000).Take(10) now it will take forever since all combinations of 1000 parentheses need to be computed and stored before taking the first 10! BTW, we can write this much nicer with LINQ, but be careful at the gotcha in the comment:

public IEnumerable<string> GenerateLinq(int n)
{
    // looks much nicer with LINQ
    IEnumerable<(string Value, int Open)> results = new[] { ("", 0) };
    for (var i = 0; i < n * 2; i++)
    {
        results =
            results
                .Where(r => r.Open < n * 2 - r.Value.Length)
                .Select(r => (Value: r.Value + "(", Open: r.Open + 1))
            .Concat(results
                .Where(r => r.Open > 0)
                .Select(r => (Value: r.Value + ")", Open: r.Open - 1))
            );  // but if you do not end this with a .ToList()
                // it will generate a huge expression that then will be evaluated at runtime! Oops!
    }
    return results.Select(r => r.Value);
}

But can't we do better? One is going to stack overflow, the other memory overflow and the last one kind of does both.

Incremental solution

They did say this requires an incremental solution, right? So why don't we take this literally? '(' and ')' are like 0 and 1, as ')' must always follow a '('. If you view a parenthesis string as a binary number, then all possible combinations can be encoded as numbers. This means that we could conceivably write a very fast function that would compute all possible combinations using bit operations, maybe even special processor instructions that count bits and so on. However, this would work only for n<=32 or 64 depending on the processor architecture and we don't want to get into that. But we can still use the concept!

If a string represents a fictional number, then you can start with the smallest one, increment it and check for validity. If you combine the incremental operation with the validity check you don't need to go through 2n operations to get the result. It doesn't use any memory except the current string and it is depth first generation. The best of both worlds! Let's see some code:

public IEnumerable<string> GenerateIncrement(int n)
{
    // the starting point is n open parentheses and n closing ones
    // we use the same array of characters to generate the strings we display
    var arr = (new string('(', n) + new string(')', n)).ToCharArray();
    // iteration will stop when incrementation reaches the "maximum" valid combination
    var success = true;
    while (success)
    {
        yield return new string(arr);
        success = Increment(arr, n);
    }
}

private bool Increment(char[] arr, int n)
{
    // we begin with a valid string, which means there are no unclosed parentheses
    var open = 0;
    // we start from the end of the string
    for (var i = arr.Length - 1; i >= 0; i--)
    {
        // ')' is equivalent to a 1. To "increment" this string we need to go to the previous position
        // incrementing 01 in binary results in 10
        if (arr[i] == ')')
        {
            open++;
            continue;
        }

        // '(' is equivalent to a 0. We will turn it into a ')' to increment it,
        // but only if there are unclosed parentheses to close
        open--;
        if (open == 0) continue;

        // we 'increment' the value
        arr[i] = ')';
        // now we need to reset the rest of the array
        var k = n - (open + i) / 2;
        // as many opening parenthesis as possible
        for (var j = i + 1; j < i + 1 + k; j++)
        {
            arr[j] = '(';
        }
        // the rest are closing parentheses
        for (var j = i + 1 + k; j < n * 2; j++)
        {
            arr[j] = ')';
        }
        return true;
    }
    // if we reached this point it means we got to a maximum
    return false;
}

Now doing GenerateIncrement(1000000).Take(10) took more to display the results than to actually compute them.

More solutions

As this is a classic interview question, there are a billion solutions to it at LeetCode. Yet the purpose of interview questions is to find out how one thinks, not what the solution of the problem actually is. I hope this helps.

and has 0 comments

Intro

  When talking Dependency Injection, if a class implementing Interface1 needs an implementation of Interface2 in its constructor and the implementation for Interface2 needs an implementation of Interface1 you get a circular dependency error. This could be fixed, though, by providing lazy proxy implementations, which would also fix issues with resources getting allocated too early and other similar issues.  Now, theoretically this is horrible. Yet in practice one meets this situation a lot. This post will attempt to clarify why this happens and how practice may be different from theory.

Problem definition

  Let's start with defining what an interface is. Wikipedia says it's a shared boundary between components. In the context of dependency injection you often hear about the Single Responsibility Principle, which stipulates that a class (and by extension an interface) should only do one thing. Yet even in this case, the implementation for any of the Facade, Bridge, Decorator, Proxy and Adapter software patterns would do only one thing: to proxy, merge or split the functionality of other components, regardless of how many and how complex they are. Going to the other extreme, one could create an interface for every conceivable method, thus eliminating the need for circular dependencies and also loading code that is not yet needed. And then there are the humans writing the code. When you need a service to provide the physical location of the application you would call it ILocationService and when you want to compute the distance between two places you would use the same, because it's about locations, right? Having an ILocationProviderService and an ILocationDistanceCalculator feels like overkill. Imagine trying to determine if a functionality regarding locations is already implemented and going through all the ILocation... interfaces to find out, then having to create a new interface when you write the code for it and spending sleepless nights wondering if you named things right (and if you need to invalidate their cache).

  In other words, depending on context, an interface can be anything, as arbitrarily complex as the components it separates. They could contain methods that are required by other components together with methods that require other components. If you have more such interfaces, you might end up with a circular dependency in the instantiation phase even if the execution flow would not have this problem. Let's take a silly example.

  We have a LocationService and a TimeService. One handles points in space the other moments in time. And let's say we have the entire history of someone's movements. You could get a location based on the time provided (GetLocation) or get the time based on a provided location (GetTime). Now, the input from the user is text, so we need the LocationService and the TimeService to translate that text into actual points in space and moments in time, so GetLocation would use an ITimeService, while GetTime would use an ILocationService. You start the program and you get the circular dependency error. I told you it would be silly. Anyway, you can split any of the services into ITimeParser and ITimeManager or whatever, you can create a new interface called ITextParser, there are a myriad refactoring solutions. But what if you don't have the luxury to refactor and why do you even need to do anything? Surely if you call GetLocation you only need to parse the time, you never call GetTime, and the other way around.

Solution?

  A possible solution is to only actually get the dependency implementation when you use it. Instead of providing the actual implementation for the interface you need, you provide a lazy proxy. Here is an example of a generic (and lazy one liner) LazyProxy implementation:

public class LazyProxy<TInterface>:Lazy<TInterface>
{
    public LazyProxy(IServiceProvider serviceProvider) : base(() => serviceProvider.GetService<TInterface>()) { }
}

  Problem solved, right? LocationService would ask for a LazyProxy<ITimeService> implementation, GetLocation would do _lazyTimeService.Value.ParseTime(input) which would instantiate a TimeService for the first time, which would ask for a LazyProxy<ILocationService> and in GetTime it would use _lazyLocationService.Value.ParseLocation(input) which would get the existing instance of LocationService (if it's registered as Singleton). Imagine either of these services would have needed a lot of other dependencies.

  Now, that's what called a "leaky abstraction". You are hiding the complexity of instantiating and caching a service (and all of its dependencies) until you actually use it. Then you might get an error, when the actual shit hits the actual fan. I do believe that the term "leaky" might have originated from the aforementioned idiom. Yuck, right? It's when the abstraction leaked the complexity that lies beneath.

  There are a number of reasons why you shouldn't do it. Let's get through them.

Criticism

  The most obvious one is that you could do better. The design in the simple and at the same time contrived example above is flawed because each of the services are doing two very separate things: providing a value based on a parameter and interpreting text input. If parsing is a necessary functionality of your application, then why not design an ITextParser interface that both services would use? And if your case is that sometimes you instantiate a class to use one set of functions and sometimes to use another set of functions, maybe you should split that up into two. However, in real life situations you might not have full control over the code, you might not have the resources to refactor the code. Hell, you might be neck deep in spaghetti code! Have you ever worked in one of those house of cards companies where you are not allowed to touch any piece of code for fear it would all crash?

  The next issue is that you would push the detection for a possible bug to a particular point of the execution of your code. You would generate a Heisenbug, a bug that gets reproduced inconsistently. How appropriate this would have been if an IMomentumService were used as well. Developers love Heisenbugs, as the time for their resolution can vary wildly and they would be forced to actually use what they code. Oh, the humanity! Yet, the only problem you would detect early is the cycle in the dependency graph, which is more of a design issue anyway. A bug in the implementation would still be detected when you try to use it. 

  One other issue that this pattern would solve should not be there in the first place: heavy resource use in constructors. Constructors should only construct, obviously, leaving other mechanisms to handle the use of external resources. But here is the snag: if you buy into this requirement for constructors you already use leaky abstractions. And again, you might not be able to change the constructors.

  Consider, though, the way this pattern works. It is based on the fact that no matter when you request the instantiation of a class, you would have a ready implementation of IServiceProvider. The fact that the service locator mechanism exists is already lazy instantiation on the GetService method. In fact, this lazy injection pattern is itself a constructor dependency injection abstraction of the service provider pattern. You could just as well do var timeService = _serviceProvider.GetService<ITimeService>() inside your GetLocation method and it would do the exact same thing. So this is another reason why you should not do it: mixing the metaphors. But hey! If you have read this far, you know that I love mixing those suckers up!

Conclusion

  In conclusion, I cannot recommend this solution if you have others like refactoring available. But in a pinch it might work. Let me know what you think!

  BTW, this issue has been also discussed on Stack Overflow, where there are some interesting answers. 

and has 0 comments

  I wanted to read something by Larry Niven, so I've decided to go with The Mote in God's Eye, written in collaboration with Jerry Pournelle. I may have read it a long time ago, decades past, because I remembered one particular line from it, but just that. And it is a wonderful book. I guess I appreciate it now more than I would have ever appreciated it in my childhood because now I know what kind of crap can be sold as science fiction and also how ridiculous most science predictions from 1975 turned out to be.

  This book stands the test of time and the test of writing and the test for science fiction. The first one, for me, it's the more extraordinary, but it is helped a lot by the second. It takes a certain amount of maturity to decide to write something respectful to the reader and only include the minimal technical and scientific descriptions required to fully describe the situation. As for the third, I think the genre is defined by asking "What if?" and taking it to a plausible technological extreme. And this book does it brilliantly.

  Imagine, if you will, what would happen when the Empire of Man will meet a race of aliens that are stuck in their solar system, but other than that older, smarter, faster and more advanced than we are. The slight anachronism of the book allows for people to discuss what should be done before anything is decided, that both military, political and civilian agencies get to have a say. To get the scientists be actual caring people who argue their cause and viewpoint, the soldiers actual caring people who think about what their best choice should be in concordance with their beliefs, thoughts as well as their orders and to have nobles that care about their empire and their people and listen before they decide. Imagine that in a recent fiction book.

  I don't want to spoil it. The species of the "moties" is defined and explored very well, as are the humans and their society. It was interesting to me that the less plausible things were not technological, but biological and medical. We've come a long way since fifty years ago in that area and I am glad to see it. The ending of the book had a much slower pace than the rest. I felt like it was going to end, but it just kept going. It is important to reach the end, but that's just about the only problem I have with the book: the pacing problems at the end.

  Bottom line: I forced myself to start reading another book because I wanted to immediately read the entire series, which contains an immediate sequel, a prequel and a 2010 sequel written by Jennifer R. Pournelle, the original author's daughter. I highly recommend it. 

  MultiSelect is a Kendo UI control that transforms a select element into a nice dropdown with text filtering which allows the selection of multiple items. This is how you use the same control to write values directly in the list, something akin to the Outlook address bar functionality.

  Long story short: the control exposes some events like: 'filtering','open','close' and 'change'. In the filtering event, which is fired by someone writing or pasting text in order to filter the list of items, we dynamically create a list item that holds that value, so that the user can just press Enter and enter the value in the list. The code also allows for a custom transformation function, so for example someone could enter "1,2,3" and it would be translated into three values 1, 2 and 3 instead of an item with the value "1,2,3". On the close and change events we clear the items in the list that have not been selected. This means you cannot use this code as is to show an autocomplete list and also add dynamic values, but it is easy to tweak for that purpose.

  In order to use it, instead of doing $(selector).kendoMultiSelect(options), just use $(selector).kendoDynamicMultiSelect(options). Here is the code:

$.fn.kendoDynamicMultiSelect = function (options) {
  var multiSelect = $(this).kendoMultiSelect(options).getKendoMultiSelect();

  multiSelect.bind('filtering', function (ev) {
    var val = ev.filter && ev.filter.value;
    if (!val) return;
    
    var dataSource = ev.sender.dataSource;
    var items = dataSource.data();
    
    // if there is an existing item in the list, don't create a new one
    var existingItem = items.filter(function (i) {
      return i.value == val;
    })[0];
    if (existingItem) return;

    // find or create the item that will hold the current filter value
    var inputItem = items.filter(function (i) {
      return i.isInput;
    })[0];
    if (!inputItem) {
      inputItem = dataSource.insert(0, { isInput: true });
      // when inserting a value the input gets cleared in some situations
      // so set it back 
      ev.sender.input.val(ev.filter.value);
    }
    inputItem.value = val;
  });

  // cleans input items and also applies an optional value transformation function
  var updateValues = function (ev) {
    var values = ev.sender.value();
    if (typeof options.valueTransformationFunction === 'function') {
      // for example split comma separated values
      values = options.valueTransformationFunction(values);
    }

    var dataSource = ev.sender.dataSource;
    var items = dataSource.data();
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      item.shouldBeKept = false;
    }

    // add items for existing values
    for (var i = 0; i < values.length; i++) {
      var value = values[i];
	    
      var item = items.filter(function (i) { return i.value == value; })[0];
      if (!item) {
        item = dataSource.add({ value: value });
      }
      item.isInput = false;
      item.shouldBeKept = true;
    }

    ev.sender.value(values);

    // delete all others
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (!item.shouldBeKept) {
        dataSource.remove(item);
      }
    }
  };

  multiSelect.bind('change', updateValues);
  multiSelect.bind('close', updateValues);
};

I kind of copied this code by hand and tried it on another computer. If you find any bugs, let me know. Also, I know this is old time tech, but they use it in my company and I couldn't find this functionality by googling it, so here it is.

I hope it helps.

OK, so I played a little with SQL and I found an interesting flow for analysing queries. It uses the SET STATISTICS PROFILE functionality, but the results of this are usually hard to read and handle in any meaningful way. There are applications that help out with this, but this blog post is trying to show you a method that doesn't need any extra software (for when you are working for a paranoid company that doesn't allow you to install what you need to do your work, for example).

This works in the query itself, so no need of any extra tool except SQL Server Management Studio and Excel:

  1. Add SET STATISTICS PROFILE OFF at the start of the query (because you don’t need to profile the setup)
  2. Add SET STATISTICS PROFILE ON just before the SELECT that you want to optimize
  3. Clear cache and stats - this is optional, but good practice. There are multiple ways of doing this and it depends on your environment and preferences, so I am not covering this here.
  4. Execute the query -> In the query results you will get the results of the query, but also the profiling statistics of the query execution, also in table form
  5. Copy the entire statistics table with headers and insert it into a new Excel sheet
  6. Add a new column right after Parent, call it IsLeaf
  7. Fill the IsLeaf column with a formula to see if the value in NodeId exists in the Parent column
    1. Write "=COUNTIF($F$2:$F$10000,E2)=0" as the first value of the column
    2. Keep pressing Shift, then press End and Down arrow (and release Shift) – you should have the entire column selected
    3. Press Ctrl-D
  8. Select the header row of the table
  9. Click on "Sort and Filter"
  10. Select "Filter"
  11. Click on a random cell, click on "Sort and Filter" again
  12. Click on "Custom sort"
  13. Select TotalTreeSubcost and "From largest to smallest"
  14. Now click on the filter on the IsLeaf column and filter on value TRUE (only the leaves)

You should now have the rows of the final tree branch nodes, ordered descending by the cost to the query.

Here you can look at the IO cost, CPU cost and Rows columns to find the places you need to work on. These values need to be as small as possible.

I hope this helps.

and has 0 comments

  I found out Neil Gaiman when I read American Gods, which I enjoyed much more than the TV series based on it. Also a book about a parallel magical world that most people are not aware of, it was still something I felt was very American, very Western, even if it was telling the stories of a multitude of gods from all over.

  Neverwhere feels more like Spirited Away than a Western story, though. And it is funny, because the mythology used as its base is British and everything happens in a parallel underground London. The lead character is an anonymous successful dolt working in the financial system who is suddenly thrust into this magical world by a simple act of kindness.

  The book is rather short and the story, while extremely enjoyable and very well written, is not that important. I mean, it's a classic hero journey (I am a sucker for those) but the beauty is in the characters and the details. I still would have wanted the main character to do something about Anastaesia and the reasons why Door was alive, then people trying to kill her, then back again are quite dodgy when you think about it. Also, the Warrior? Seriously?

  Bottom line: if you feel like immersing yourself into a magical world that feels close and real, but also incredible and impossible, then that's the book for you.

and has 0 comments

  Finna is a novella of only 144 pages, of course the beginning of a series, one that I have absolutely no intention to read. To be blunt, the only reason why I didn't rate this booklet the lowest is that Nino Cipri is actually queer/trans/nonbinary and so I can't complain about the characters being that way with absolutely no relevance to the story. I probably fell (again) for one of those agenda driven fake reviews that recommended it.

  Speaking of the story, it's a rather refreshing concept but that can be explored in a single page. It's a variation on the "doors to other universes" trope. However, most of the short span of the book doesn't focus on the idea or on what happens or even on character development. Instead, it goes on and on about how offensive it is for people to not use the correct pronouns, how tough it is to be queer or mentally afflicted, which is the all the depth the two main characters ever reach. The writing style is telegraphic, almost report like, lacking anything to make me feel anything (good).

  It is a really annoying book because the leads are totally unlikeable. They work at an Ikea clone for minimum wage, they complain all the time, they couldn't care less about people around them except for the awkward romantic relationship they have and even then not much, they go through parallel universes without paying attention, they act and emote without considering the consequences of their actions then blame it on how their brain is wired and so on. It's a story seen through the eyes of teens who would rather spend time with their phone than wonder about the world. I wouldn't be surprised to hear this was one of those books written in tweets or whatever. Still better than 50 Shades of Grey, but that's not saying much.

  And yes, I sound like a grumpy old man because this is what this book makes me feel like: totally disconnected from the entire generation of the characters in the story. They don't even sound like real people to me. It's a story about exploring strange new worlds which pauses every time something could be interesting to focus on how the characters felt in the world they left behind and how their relationship should have, could have, will have...

  Bottom line: Minimum effort and maximum annoyance.

and has 0 comments

  I have heard about Kurt Vonnegut a few times, like someone I just had to read. So I started with Slaughterhouse Five, first published in 1969 and widely acclaimed as his best work. I guess for 1969 it was great. It is subtle, it is poignant, it is ingenious, it is satirical, it is anti-war. It is not entertaining, though. It's just really sad and bleak.

  Imagine if Forrest Gump would not have been a kind country boy with a slightly slow mind, but a guy heavily affected by PTSD after witnessing the horrific firebombing of Dresden, with illusions of alien abduction. Dresden was bombed in WWII into oblivion by British forces, some say as retaliation for the German rocket bombings of London, even if it was mostly a civilian city filled with refugees.

  But that part of the book, which is drawn from Vonnegut's own experience, even if it is the event that caused everything, is placed at the end of it. The rest of it is the story of the fictional main character who becomes (or believes he does) unstuck in time, which allows him to randomly travel back and forth into his life. He is also abducted by aliens who live in four dimensions, time being the fourth one, and to which time, cause, effect, action and consequences are nonsense. Every moment is unchangeable and set and they can just visit any of them. It is worth mentioning that one of the symptoms of PTSD is this very realistic recollection of past events. It also makes sense for a person afflicted by this to build a narrative in which everything that happens, no matter how atrocious, is not preventable. Anyway, this way of telling events adds an interesting way of understanding them, allows for comedy and satire, makes it all very personal, which is good, but that's about the only thing I liked about the book.

  Now, being studied in school, there are way too many commentaries and reviews trying to explain what the book was about and how brilliant it was. I can only say if I liked it or not. And the answer is that I understand why it is a highly regarded piece of literature, but I did not enjoy reading it. And not because it is terribly depressing, which it is, but because the main character is only interesting because he went through some horrific events, otherwise he is boring and worthless. This makes things even sadder, because this means a big part of the author felt that way about himself.

  Bottom line: It may be poignant, as the cover says, but it is not hilarious. Instead it is depressing and gets so more as one understands more of it. It is a personal expression of deep trauma, so if you enjoy that kind of thing, this is the book for you.

and has 0 comments

  When I was a high school kid it was fashionable among the street thugs of Bucharest to use the insult "slave". I don't believe this was related to the history of slavery of the gipsy people in Romania's past, which was the ancestry of many influential such thugs, as it was something borrowed from the Americans, where that issue is much more aggravating. If I am correct, then the very use of the term denotes the way Romanians relate to other people, especially those who they perceive superior. Of course, if I am wrong, then I am guilty of the same thing, so QED, I guess?

  After the Romanian Revolution against Communism (note the big R we use for that event, being the only real change we ever affected as a people) people from different countries came to assess the opportunities presented by a newly opened territory. One report that I saw with my own eyes was from a Jewish lawyer who said just that: Romania is filled with highly educated people who distrust their own government, laws and look poorly on local products and people. Instead, they revere national brands they never actually had any real recent contact with like the U.S. and West Germany, preferring ideas and things imported from there to things they could get or make locally. He concluded that it was a good place to invest in, since the quality of the local human resource was high and their expectations were low.

  The Revolution was in 1989, 32 years ago, but the mentality is still mostly there, as even the people who teach the children of today are still of the generation that lived through that era. It is funny to discuss education with a Romanian, they all complain about how bad it is related to how it was, because children aren't fed the same amount of unprocessed information that was the mainstay of the Communist education. They are rarely complaining about the lack of technological advancement in schools or of skills that are useful in real life or about how children are not taught how to be passionate and happy. Even so, they don't do anything at all to change anything. The only measure of control that parents have is to which school to pay people for their kids to get into and, more recently, to which expensive private school to send their kids in order to give them what they see as the proper education.

  The heroes of the Romanians are not the successful entrepreneurs. The media rarely mentions them and then it is mostly because they were paid for by those people. In case an average Romanian hears about a successful Romanian businessman, it is assumed they had connections with the people who ruled the country during the Communist era. Since they started with money and/or influence, their success is surely undeserved. Funny enough, people like doctors are not heroes either, because in Romania people usually have to bribe medical personnel to get any of the treatments that are theoretically free. Even when they go to private clinics the instinct is to pay extra to appease the doctors who, in their natural state, would try to kill them. This is, of course, caused by a systemic underfunding of medicine and by the endemic corruption which sees any funds misused or embezzled. Because of this, a medic who saves lives - even when they refuse to accept any extra money, is not a hero, but just incompetent or parasitic. Heroes are not the people who left the country to get abused as cheap labor abroad either. People who went to Spain to pick strawberries or to Greece to pick olives are just poor uneducated people who are clearly poor stock, perhaps even gipsy people. They deserve no respect. Same for prostitutes, who are universally despised as shaming the country and at the same time praised for being amongst the most beautiful of prostitutes.

  There was once a news report about people going to pick strawberries on TV and the mother of a girl going to Spain explained how she taught her daughter to conform to whatever they say there, to not antagonize the boss. Just work hard and make money and do whatever he says. This was her mother!

  Police people are not heroic, either. They don't protect the citizens, they enforce unreasonable rules. Politicians are not heroes, they are either the absolute evil or the person who is opposing the absolute evil, which makes them slightly less evil. The few situations where political fervor is so high that someone becomes close to being considered heroic, their efforts pale in comparison with the expectations put on them, which of course proves they were evil all along.

  No, the heroes of the Romanians are the hard working people who work for other people. Great theoreticians, the army of Romanian software developers that left the country to work for multinational corporations, any doctor who would be despised in the country is a hero when working abroad, engineers of all kinds, any white collar job, even people working in construction - which is somehow seen as a clean job. With the caveat that they must not be gipsy or poor stock, they surely left the country to thieve and steal and they are shameful to us all. If people "from the developed countries" (this is a phrase still very much in use in relation to the Western countries) praise a hard working Romanian, the entire country breaths a collective awwh of pride, like dogs petted on their heads for being good boys. "You see?", they say, "Now they see what we are made of!".

  One obvious exception is any managerial job. If you are a manager, you actually do little to nothing, you are an oppressor, not a hard working individual. Even if you got there through your own efforts (and didn't rely on the people you knew or money you had) you have a dirty job that deserves no respect. Unless you are related to the person who judges the situation, in which case you are the pride of the family. 

  The biggest heroes are of course great athletes, especially if they are part of an international team. If a Romanian is part of a of German football team, the entire country believes he would be the reason why Germans are good at football. Of course, people doing great things in Romanian sports are usually seen as part of the endemic corruption in Romanian sports. They still get the status of heroes, but they are always on trial and any mistake will be fatal to their reputation. An interesting exception is Simona Halep, which is the greatest Romanian hero of all, because she goes to international Tennis competitions and wins some of them against the best international tennis players in the world! The fact that she is reasonably good looking doesn't hurt either. She is of the people for the people.

  There are great Romanian writers, for example, but we only value the dead ones. We learn in school about great Eminescu, or Rebreanu, or Blaga. The more poetic, the better. The more obtuse the better. And in order to verify if children really read these big greats, we ask them to write commentaries. And kids just read existing commentaries and summarize them. With the advent of Google and AI, it's impossible to not automate this, since it requires the lowest level of human inventivity. Current writers are poor unknown quantities, always shorted when negotiating with local publishers. And no one reads books anymore anyway, what are they doing? There are great Romanian actors and filmmakers, but few people actually bother to watch Romanian productions. We occasionally see films winning some international prize somewhere and it's usually some drab and depressing drama about people being treated as slaves and behaving like one.

  All these point to a mentality that can only be called "slave mentality". Anything Romanian is poor quality, therefore we export raw material and import the very things made from it. As an example, we export a lot of apples and import a lot of apple juice. The opportunities for Romanians are to either find a good job in Romania or - much better - a good job abroad. To have a company, make your own money, employ other people, is still seen as something dirty. Romanians are not educated on how to make, keep or invest money, instead they are instructed on which jobs are better paid. When I was a child, I knew that I should go for being a medic or a lawyer. Now kids are probably taught to try to go into software. No parent would ever tell a kid to think freely, pursue their dreams and try to make something of themselves through their own strength unless it first starts with being employed somewhere. Surely, dreams can wait, they say, all sad and depressed.

  Funny enough, people who somehow get to be business owners, employers or managers many times behave like slave owners. This is also slave mentality: slaves don't dream of being free, but of becoming masters themselves. This sadly also encourages Romanian employees to feel that anything Romanian sucks. In my career the most unprofessional, choleric, petty and unethical employers were Romanians. With small exceptions that I will attribute to mental illness and not national culture.

  Even when we go on holidays, the biggest complaint we have is "it was nice, but there were too many Romanians there". When we were most complaining about how poor our country was, right after the big R, we were the country importing most expensive cars and high end smartphones.

  This is a sad sad cycle, Romania is and will continue to be a nation of employees. Nothing in our culture impresses the importance of self actualization, of generating and defending our own values, of pushing to get ahead and then actually pulling people with you. Nothing teaches us to band together under those values and fight for them. From all of the cultures we've had contact with, which mostly invaded our country and enslaved us for most of our history, we only learned to slave or enslave others.

  We had and we still have a lot of potential, smart people, hard working people, but is that relevant without a set of values? I personally feel that the Romanians abroad impress their employers, but also their employees or their subordinates and their friends, not by being hard working, but by being open and by not being complete assholes, by being kind and ethical. It hurts me to see how American we have become, partially because of the malevolent influence of Russia, but instead of going for the American ideal declarations of freedom and equality, we emulate the American actions of selfish individuality and status driven inequality. They also pushed this idea that somehow you are either a Capitalist or a Communist, master or slave. This just in case you are not a Fascist or a Terrorist, which makes you gipsy or poor stock.

  Let's abandon this obsolete thinking of masters and slaves and instead think of the others as simply people. Your employer is your partner not your owner, people from other nations are still just people, dreaming is still OK and when you reach the top, give yourself a pat on the back and help others up. Find something you care about and do it. There is nothing on the other end. Every moment is not the first day of your life, but the very last. Make it worthwhile.

and has 0 comments

  Rarely have I read such a frustrating book. Civilized to Death is trying to show that our modern life and societal order is not the only solution, that progress doesn't mean what we think it means and that a lot of the things we take for granted about our (pre)history and path as a species is just propaganda. So on one side, I was fascinated by the concepts shown in the books. However the tone of the text is so shrill, inconsistent and self contradicting that I found it infuriating.

  Christopher Ryan is obsessed with hunter gatherer societies. He rose to fame with a book that covered the sexual behavior of our closest relatives: chimps and bonobos, as well as the information he had collected on our ancestors in prehistory about the subject. Again, he was trying to show that most of what we know or take for granted about human sexuality is self interested bullshit, beginning with the arrival of agriculture. In Civilized to Death he just moves even further to blame all the ailments of humanity on that same event and the hierarchical paternalistic profit obsessed society that arose since then. Progress, he argues, can only be measured in human happiness and well being and modern society sucks at that.

  You can skip reading the book and just read the cheat-sheet that the author published on his web site for it. The basic gist is that we were better off hunting and gathering and that modern society is just a self-sustaining meme that leads to our domestication for its own survival, not something that increases our happiness. It's a zoo for ourselves and we should take control over what that zoo looks like and how it treats us.

  I agree with that last statement, but I call bullshit on Ryan's arguments. He vilifies Malthus, but then he claims if we limited our population to 100 million, we would all have enough resources to live without effort. He attacks statistics and experiments as being biased or even intentionally skewed, then he uses statistics and experiments of his own that he likes better. He attacks globalization and how it forced the same narrative on everyone, but then he makes his research and his money with the help of the global communication network that technology provided.

  His worst sin, though, is how we so liberally uses the terms "we" or "humanity" to define the specific group of people that he likes. For example, people are slaves to the drive to work. If they try not to work, the entire society brings hell down on them, so they are forced into this horrific labor camp that the Earth has become. He completely ignores the people that absolutely love to work! Ryan mentions people who could very easily stop working and live a comfortable life, but they don't, only he puts this all on some perceived addiction to work, not that it gives people meaning and fulfilment if doing the thing they love. He talks about how egalitarian hunter-gatherer societies are, but he completely ignores what happens to the members of those groups who do not want to be egalitarian. And so on. He is furious against what he calls the Narrative of Perpetual Progress, which he then dismissively calls NPP in the rest of the book, but he doesn't really come with an alternative.

  And that makes the book very frustrating, because you want to believe what he says, you want to look further into the sources he mentions, but the way he puts everything together feels like very artificial cherry picking. And the ending is always something like "OK, I may have gone too far, but what I really mean is...". No! Just don't go too far. Make me trust what you are saying, not feel I am being swindled by a TED talking Marx!

  Bottom line: a very interesting premise, but poor argumentation. Still worth a read.

and has 0 comments

  Last Call is about the occult and symbolism, threaded through history, actual famous people, the Vegas culture, the world of gambling and card games, Tarot cards and so on, filled with action and fringy characters. Tim Powers has put a lot of effort in the research and the details and that, ironically, may be the problem of this book. There are too many characters, too deeply rooted into the mythology of chaotic gods and the significance of cards, doing things that you need to reread a few times in order to understand. It gives the book a hallucinatory feeling, like you are there and not there at the same time. This might help people get into the atmosphere or push them away.

  The book is also quite long and just the first of a trilogy. While I've enjoyed living in the world described by the book, and while it is well written, I also found it a chore to read. That doesn't mean it's not a very good book, it's just that I don't think I was its intended audience or maybe reading a few pages before going to sleep or listening to it while walking the dog were not the best situations for getting into the story.

  Bottom line: very interesting concepts and well written, but requires effort from the reader to fully enjoy. I found it a bit too long for comfort and the pacing was a bit unreliable, too. If you're into symbolism and Tarot mythology, this might be the book for you.

  Yesterday I saw one of those things that make me exclaim "Only in Romania!". There was a pharmacy and in front of it there was an old lady waiting her turn to enter, because there can't be too many people inside. Next to her, like a few feet away, there was a maskless man puffing away on a cigarette, his smoky exhalations reaching the masked old woman as she was trying to protect herself and the people in the pharmacy.

  And it made me more attentive to people around me. Bands of maskless high schoolers, with the loud talking, fake swagger and hysterical laughter typical of their age and need for attention were happily walking on the street, not a care in the world, their bravado, as idiotic as it was, clear on their faces. Then groups of parents, masks on their chin, smoking and talking to each other as they supervise their children playing, blocking access ways where people have to squeeze between them in order to pass. I know those people, too. They already believe that they have been exposed as much as they could be, that trying to protect themselves when their children literally exchange spit and snot is futile. Then the drivers, happy that they don't have to wear masks inside their own car, but pretending to forget to use them even when they have to get out to buy something or to get home. Meanwhile, the police has found some specific sweet spots where they hunt for people not doing their duty regarding the pandemic, ignoring all others. And of course, the noseholes. People who wear their masks because it's mandatory, but only on their mouths, because they breathe through their noses.

  And yes, all of these are selfish assholes, but that's not my point now. These people are contributing to the rise of infections, but only a few weeks ago, when the rates were plummeting, they were careful with the masks, the distance, everything. The first and simplest hypothesis on why this happens is that as people stop wearing masks, infections start rising, and it surely does explain a lot, but it's not enough. These people could have relaxed weeks ago. Why now?

  My theory is that as schools opened, the number of people not wearing masks on the streets increased. Whether due to parent fatigue (let's call it that) or idiotic bravado or because some of them are too small to wear masks, children have skewed the proportion of people being responsible. And like a switch being flipped, other have started copying the same behavior. It's not even something conscious. I have difficulty believing people are actually reasoning that if a four year old doesn't wear mask, they should stop, too. It's a social phenomenon, a monkey see, monkey do thing. This has nothing to do with charts or numbers or public announcements which clearly show that we have now reversed our trend and the percentage of sick people is rising again. Another subconscious trigger is that more light is coming from the sun as we head towards spring. People attach darkness with crisis and light with things getting better. And yes, again, without thinking about it.

  This means several things. One of them is that communication of the facts doesn't function as it should. This is on the media and their panic inducing way of reporting things. They only discuss the bad things, the shocking things, not the good ones. Numbers don't matter for people who have lived in fear for months. At one point or another they are going to break and act out. The solution is to reward people when things are going well, to inspire rather than scare people off. It would have been easy to understand and feel what is going on if, after weeks of smiling news anchors congratulation the population for their efforts, they would have turned sad and announced the trend is reversing. But they don't do this and that makes them responsible.

  Second thing is that statistically people are barely aware of their existence. Like automatons they live their lives and copy each other's behaviors without a single thought. If prompted, they will find rationalizations for their actions after the fact, then feel resentment towards the person that forced them to make the effort. There is no solution for this, I am afraid. It falls on the authorities to be aware of this and act preventively to ensure a healthy proportion of people looking like they are doing the right thing. Note: looking like. This can also mean good propaganda or spin. Who would have thought that I would ever advocate for that?

  Finally, this cannot be turned into another cat and mouse game, where the authorities are punishing people when they catch them, so people do bad things when the police is not there. This is not a carrot and stick situation. The ingrained distrust of authorities and petty selfishness we learned during the Communist era makes traditional measures from the government fail miserably or even backfire. This is the most difficult thing yet: change the ways the system works in order to reward people for their good work. This is anathema to Romanian life and politics, but it must be done. Social responsibility needs to be bred back into our national DNA. Otherwise we will remain a nation of assholes like the guy in the image.