The Blog

Observations and ideas on software development, architecture, and algorithms.

just abstract cubes

Topics of interest

Recent articles

QuickSort is actually two different algorithms (Lomuto and Hoare)

05 June 2018

General observations

QuickSort is a Divide & Conquer sorting algorithm, which immediately hints how you should start implementing it:

public void QuickSort<T>(T[] items)
    where T : IComparable<T>
{
    QuickSort(items, 0, items.Length - 1);
}

private static void QuickSort<T>(T[] items, int low, int high)
    where T : IComparable<T>
{
    if (low < high)
    {
        // pi is for partition index;
        // moves smaller or equal items to the left of the index pi
        // and greater or equal to the right
        int pi = Partition(items, low, high);

        // recursively sort the two parts;
        // that's why "Divide & Conquer"
        QuickSort(items, low, pi);
        QuickSort(items, pi + 1, high);
    }
}

What’s left is to implement the Partition function which internally choses the pivot and moves smaller or equal (compared to pivot) items to the left of the returned index pi and greater or equal (compared to pivot) to the right of pi.

It turns out, that there are two commonly used types of such functions, which are more than different.

... read more

Reducing any code duplication

08 May 2018

General observations

I’m a big fan of the “don’t repeat yourself” (DRY) principle (as I’ve mentioned for instance here a bit). IMO, this very principle should be respected from the very development start. At the same time, the principle is one of the most violated – especially on the front-end. However, lower layer applications and libraries tend to adhere to the principle much more – as it’s much easier to “scare” product owners or managers with a prospect of maintaining and aligning two identical (or even worse – kinda identical) parts of the long-lasting code further on.

But some developers just tend or even like to copy-paste code and their arguments for that are:

  1. it’s faster [for a developer to write];
  2. this way we don’t affect the other (presumably) well-working code parts that we’re copying from;
  3. the refactoring isn’t possible in this case; the other wording for this is “this is not code duplication”.

IMO, not only code duplication isn’t aesthetic – it’s also:

  1. a technical debt;
  2. can pull some implicit bugs or design flows that could lead to further two (or more) place fixing; also, when a good developer does the refactoring, the developer could also review and debug the repeating code parts once more – therefore, make them even more reliable or even more general – e.g. when a DB-querying method can take a lot of optional parameters (to filter the data) instead of querying the same table or join of tables with separate methods for each application.

The third pro-copy-pasting argument is a bit more tricky, as it’s often applied to cases like the following:

... read more

How to get files information efficiently with PowerShell

07 May 2018

Objective

Let’s suppose you want to get files information in Windows using PowerShell. The reason of using PowerShell specifically can vary:

  • you want to pass execution results further to some exclusive PowerShell commands through the pipeline;
  • you’re limited to the usage of certain tools or libraries; or you just want to keep things consistent;
  • you want to utilize PowerShell remote calls;
  • you just want to use a modern and well-maintained tool.

Let’s also state the requirements:

  • you have a full path to each file; let’s also assume that all those files are on one drive to keep things simple; therefore, you also have a base folder where all the files reside (which is, perhaps, more specific than C:\, but still has lots of files);
  • some of those files can be absent – you should just ignore them and only return information of the existing ones;
  • the base folder (where all the files are) can have nested folders (with files of different extensions) and the wanted files could be in each one of those.

Sounds like a real-lifeTM task, doesn’t it?

... read more