Posts Tagged ‘code-alignment’

Code alignment website

Lots of changes have been happening in code alignment recently. I’ve mentioned some of them in random places, but haven’t given a lot of details. But today I want to focus on the brand new code alignment website.

I’m really happy with the website, it’s nothing fancy, but looks very pleasing. From now on this will be the central point for code alignment news. It also features an issue tracker, feel free to use it to suggest any improvements.

You’ll also notice that code alignment has a fancy logo and a branding style. The main logo is designed my friend Michael Csikos who shares a lot of my ideas about formatting code. After he had made the logo we were talking more about how to brand the individual programs. I suggested replacing the = with a symbol to represent the different IDEs and the end result looks great (The Visual Studio sign was created by another friend of mine – Martin Krestan)


Donations & Advertisements

Before I continue I want to make it clear, Code alignment is, and will always be a completely free tool.

I’ve donated a lot of free time to develop code alignment during the last 2 years now. During this period I’ve never thought about trying to make any money off it. But when making the website I decided to add a ‘donate’ button and put an unobtrusive advertisement  down the bottom, completely separate from the content.

Future versions of code alignment will have an about box with a donate button on it. That’s as far as it will go. Don’t fear me getting greedy and creating popup ads or constantly bugging you to donate.

Personally, I find code alignment an essential tool for software development. The amount of time it has saved me would easily be measured in days by now. I know many others feel the same. If you enjoy code alignment, then please donate.

Code alignment 3

It’s been a while since I’ve released an update to Code alignment. But I’ve been working on it on and off since that release and I’m ready to release a significant update – Big enough to warrant being version 3.

When Version 2 was released it had some important changes

  • It became its own toolbar instead of just adding them to the edit one. This was useful for people like me who knew the shortcuts and wanted an easy way of hiding them from the toolbar.
  • It separated the align by string and align by position into different functions. This was great as the old way you had to default the position to zero,  but when you actually wanted to align by position you wanted it to be at the caret location.
  • A lot of streamlining – The big one was align by space not asking for position, just using your caret location.

Looking at the changes from Version 2 to Version 2.9 it’s amazing how much it has improved. My favourite additions are

  1. Align by space aligning the last space in the first space block – With this align by space became the most awesome feature of the extension.
  2. Changed the selection logic for XML style documents
  3. Releasing a Notepad++ version

So now we come to the list of what’s new in version 3…

Lots of backend changes.


    1. There is now a common assembly which is shared between the Notepad++. Before the code was common but as shared files. This was to overcome some problems in Notepad++.
    2. More decoupling -  allows for various behaviours to be changed.

Regex support

You can now specify regular expressions to look for. Even better you can (optionally) add a named group ‘x’ to specify where to perform the alignment. For instance, the regex


will align by the first character after a semicolon. This has allowed a lots of simplifications in the backend. Don’t expect this to work with tabs though – I’ll explain that in a future blog.

New ‘Align by string’ screen

Unknown to most people, Code alignment started off as a macro. A macro I didn’t write myself, but had constantly improved over the years. When Visual Studio 2010 came out with its improved extension model I took the plunge and completely rewrote it from scratch. The entire approach was different (and much better).

The only remnant of the old macro was the ‘Align by string’ screen. Pretty much it was a basic VB dialog. I can’t remember if this was in the original macro or something I added to it. But when I rewrote it, I just add a reference to the VB dll (in my c# project) and used the same code. Why? It was simple, fast and did what I needed.

But that last remnant is gone. The new screen not only allows you to use regular expressions but also keeps a history of the strings you’ve chosen to align by – not just the last one. There’s also a check box to align from caret position.

Align by position is gone replaced by align from caret position

Let’s face it, the only position ever used was the current caret position. Entering the column number was just too annoying. Sure, there could be other ways of choosing the location, but the caret location works so damn well.

Slight change to Align by space

Now that code alignment allows regexes I could simplify the code for a few things. One was the align by space. Before it could be defined as “align the last space in the first space block” this has ever so slightly changed to “Align the first character after the first space block”.

What does this mean? Most of the time nothing. The only difference is it will ignore whitespace at the end of lines (as there is no character after the first space block).

Behaviour in Align by string for align by space or equals has changed

Before if you were in the Align by string dialog and you entered a space or a equals, it would perform the same as if you had done an “Align by space” or “Align by equals”. It no longer does this. You now have full control over the box and what you enter is what you will search for.

Why my style for AND and ORs is clearly superior

When it comes to the style of code a lot of it is personal preference, and unfortunately we too often focus on these preferences as opposed to the parts that can be clearly reasoned as a better style. The obvious example is curly braces…

public void MyMethod()
public void MyMethod() {

In terms of readability one isn’t really any better than the other. You can reason why one is better, but it really is just a personal preference. Personally I prefer it on a new line. Why? Well, the biggest benefit to me is it works better with my code alignment extension.

But there are ways of formatting our code which is clearly superior. As I’ve been saying for years, the most important one is code alignment – lining up your code makes it so much more readable.

But I wanted to focus on one case today and that’s ANDs and  ORs. It’s something that a lot of people aren’t too disciplined with. When it’s simple and on a single line it’s fine but as soon as it needs to be split, people don’t know where to put the && and ||. The first way is

if (myObject != null &&
    myObject.Kind == Kind.MyKind &&
    myObject.HasValue &&
    myObject.Value >= myObject.MinValue)

this is what I think most developers do. But it sucks. The Ands are hidden if one was an OR it could be easily missed. So the first thing we can do is align the Ands.

if (myObject != null             &&
    myObject.Kind == Kind.MyKind &&
    myObject.HasValue            &&
    myObject.Value >= myObject.MinValue)

It reads nicer but it still has problems. First if it add a new value the ands need to be moved, our eyes are attracted to the left first, not the right. And when we talk, we say X… AND Y. The AND is more of a prefix than a suffix. This leads to…

if (myObject != null
&&  myObject.Kind == Kind.MyKind
&&  myObject.HasValue
&&  myObject.Value >= myObject.MinValue)

Here the AND is clearly shown as a continuation of the if. Our eyes quickly jump to the symbol.

And in case you’re wondering it works with anything not just an if

bool value = condition1
          || condition2;

return value1
    || value2
    || value3;


Return top


The occasionally interesting thoughts & insights of a software developer