Ivan Čukić

Header/Source switching in Vim

I’ve been using the vim-fswitch plugin for switching between .h and .cpp files for a long time now. The thing I was really missing was the inability to switch to private headers and implementations (filename_p.h and filename_p.cpp)

Recently, I discovered the (more than awesome) CtrlP plugin. I am not going to explain here what it is – I strongly advise you to check it out!

I’ve written a small extension for it which provides the header/source/private files switcher.

Switcher screengif

VIM: “Hiding” C++11 lambdas

One of my favourite C++11 features are lambdas.

The syntax is a bit cumbersome, but it was the best approach the committee could take without creating a new sub-language. Every part of the syntax has a reason for why it exists.

But, it still is a bit ugly, and can influence readability of the surrounding code quite a bit.

Lambdas in C++11

The thing that annoys me the most is the lambda head – the capture block and the arguments it takes. Those are very important when writing the code, but not (that much) when reading it.

My solution for this? The conceal feature of Vim.

C++11 Lambdas concealed

The good thing about lambdas is that they are (meant to) be used as local anonymous functions. That means that, while reading other parts of the code, you don’t actually need to know what the lambda is capturing, nor which are its arguments. So, it doesn’t hurt to hide them, right?

Naturally, when you want to edit the lambda head, Vim shows the actual contents of line, and not just some strange Greek symbol. :)

This also lowers the desire to use the potentially problematic [&] and [=] as the capture block, instead of explicitly capturing the variables that you need.

Edit: The code to achieve this:

.vimrc:  
    set conceallevel=1
.vim/after/syntax/cpp/cpp.vim
    syn match cpp11_lambda "[[a-zA-Z0-9&= ,]*] *(.*)( *{)\@=" conceal cchar=λ
    syn match cpp11_lambda "[[a-zA-Z0-9&= ,]*]( *{)\@=" conceal cchar=λ

Which one is clearer – the resolution

If you haven’t seen the previous post, I’ve posted a question of what style would you prefer (I’ve simplified it a bit here, for the original, go for the link above):

(A)                        | (B)
                           |
if (ratio < 1.5) {         | result = (ratio < 1.5) ? 1.0 :
    result = 1.0;          |          (ratio < 2.0) ? 1.5 :
} else if (ratio < 2.0) {  |          (ratio < 3.0) ? 3.0 :
    result = 1.5;          |          /* otherwise */ ratio;
} else if (ratio < 2.5) {  |
    result = 2.0;          |
} else if (ratio < 3.0) {  |
    result = 3.0;          |
} else {                   |
    result = ratio;        |
}

As expected, people are split into two similarly sized groups. It is a matter of style after all.

Neither of these is better, but they both have their pros and cons. Lets see what we have collected so far.

Firstly, lets deal with the elephants in the room:

- Is this an academic question? (as suggested by krake)

Yes and no. Yes, because it is not really that important – people will continue using what they used before. No, because it might influence the different aspects of the resulting code. (see below)

- You can use if-return format: (suggested by cyber_fusion)

if (...) return ...;
if (...) return ...;
...

This seems to be a popular variant of (A) when the purpose of the result variable is to be returned from the function we’re in (as it was the case in the original post). It does not work in other cases.

While it is a good approach (and preferred by a significant number of people), there was one statement for it, that is not correct – (Alex) “it shows structure, it will not execute if statements after a successful one”. All the proposals behave the same – neither executes statements after the first successful one.

The issue some people pointed out is the lack of braces for the if body which is frowned upon by a significant number of coding styles used across the world. If somebody questions why would missing braces would ever be a problem, just search for the ‘apple goto fail’. When you add braces, the solution stops being as visually attractive.

Now, we can start with the reasons people wrote for one or the other. In no particular order, to avoid the trap of actually counting pros and cons.

(shamaz) The big difference here is that the 2nd solution is ONE statement. This means that it’s less simple to debug : you won’t see which branch is chosen (similar comment by Morten)

It is a single statement, but it has multiple sequence points[↗] (or whatever they are called in the new standard). The same sequence points an if/else construct does. So it will show up (tested in gcc+gdb1) as separate steps while debugging2. You will be able to see where you left the statement.

Edit: 1 clang does not create the same debugging info, so it does treat it as one execution step.

2 It has a big uglier debugging output, but still is quite usable.

(Lihnell) I’d like to add that putting the else in a comment for readability is a clear indicator that the language used is not suitable to the person using it

I do agree. It essentially boils down to the fact that ?: syntax (for this) is not something people are used to. So adding ‘else’ or ‘otherwise’ is a nice way to make it more readable.

(konqoro) I guess it was how we were told to better understand the if-else statements.

There were a few similar comments to this one.

Yes, if-else approach is ubiquitous, and therefore understandable by everyone. IMO, it is the strongest reason for going for it – people are not used to (B) even if (Jeff) “the notation fades into the background, and the structure of the actual operation pops out”.

(dhardy) I find the second much easier to read. My preferences are (a) avoid re-assigning variables

Thanks to some pure functional languages like haskell, the concept of immutability started to spread out to other paradigms. The second approach is also usable in C++11 constexpr functions, while the first one will be allowed in C++14.

One of the approaches to make the result variable const for the (A) variant would be to use lambda-initialization, thus localising the mutable variable inside the lambda. Like:

const double result = [] { ... }();

(Alex Elsayed) I doubt you care overmuch, but PHP will not do what you expect on the second one

You are right that I do not. But, this is a useful information for the future – if I get to hack on some PHP code. Thanks!

(shamaz) For the 2nd solution, if someone try to format your code automatically with his IDE, it may become hard to read.

Yes, unfortunately. I’ve tested it with clang-format and it essentially killed it. I guess if it became a common idiom, formatters would learn to deal with it. :)

Epilogue:

Personally, I prefer the second option although I don’t use it that often mainly because it is not that common and people are not used to it.

I do prefer it on the technical grounds, though. It is perfect for const initialization / constexpr functions, the syntax stops you making code-evolution mistakes similar to the ‘goto fail’, and different mistakes that the original code (which inspired me to write this) had. (the original code was posted for half an hour in the previous post, but I decided to edit it to show only the styling differences, without other issues)

Which one is clearer?

This is a excerpt from a real project, just with variables renamed.

EDIT: * I’ve removed the issues from the original code so that this can be a real style vote, without sidetracking the readers.

Although, by fixing the code, I removed one of the things that I think the advantage of the second one is – it is much harder to make errors and dead code.

const qreal ratio = someFunction();

if (ratio < 1.5) {
    result = multiplier;
} else if (ratio < 2.0) {
    result = multiplier * 1.5;
} else if (ratio < 2.5) {
    result = multiplier * 2.0;
} else if (ratio < 3.0) {
    result = multiplier * 3.0;
} else {
    result = multiplier * ratio;
}

return result;

This was the usual imperative code that you’d find almost everywhere. And this is the alternative (functional) way of doing the same:

const qreal ratio = someFunction();

return multiplier * ( 
    (ratio < 1.5) ? 1.0 :
    (ratio < 2.0) ? 1.5 :
    (ratio < 2.5) ? 2.0 :
    (ratio < 3.0) ? 3.0 :
    /* else */      ratio
);

The second one is kinda what you’d do in maths:

       /  1.0     x < 1.5
       |  1.5     1.5 ≤ x < 2.0
f(x) = <  2.0     2.0 ≤ x < 2.5
       |  3.0     2.5 ≤ x < 3.0
       \  x       otherwise

What do you find more readable?

Activities API for QML

I had an unfortunate intermission in kactivities development due to a hard drive failure.

Fortunately, I managed to salvage significant parts of my work, but without git history. Now, it is ready for prime-time.

You are now able to access the activity manager without the need for the ‘clumsy’ activities data-engine. Just import org.kde.activities and create an instance of the ActivityModel.

  import org.kde.activities 0.1 as Activities

  ListView {
      id: main

      model: modelMain

      Activities.ActivityModel {
          id: modelMain
      }
  }

But, this is not all. You are not limited only to retrieve the data, it is also possible to create, delete or alter them. The service control API is asynchronous. Therefore, all methods, apart from regular arguments, receive a continuation function that is called when the method is executed.

Something along these lines:

  modelMain.setCurrentActivity(idOfTheActivity,
    function(result) {
      console.log("Activity switched")
    }
  )

  modelMain.createActivity(nameForTheActivity,
    function(result) {
      console.log("Activity id is: " + result)
    }
  )

Well, that is all for now. I’m thinking of adding overloads without callbacks, but these methods are not really meant to be called by everybody, so I’m not sure how important a more pleasant API actually is. :)

SQL results in range-for

I’ve been irritated for the way a QSqlQuery result-set is supposed to be processed for some time now – with while(query.next()) and getting the results via query.value(...). I was always thinking it would be nice if it were more accessible… namely, accessible using iterators.

To be able to do something like this:

  for (auto &record: query) {
      // do something with
      record[0]
      // or
      record["ID"]
  }

You have a simple .h file to include, and it will just work. [1]

The issue is that it is only tailored to match only this use-case – it can be improved to do more, and to allow more iterators over the same query, but I don’t really care to do that :)

[1] https://gist.github.com/ivan-cukic/7346513

Activity switching questionnaire

I have a few questions that I’d like to hear an answer to from a wider user (and developer) community. Those revolve around the activity switching UI and the things it could have in plasma 2.

Currently, the switcher shows the name and the icon of an activity, and allows you to:

  • switch activities (duh)
  • create / delete activities
  • start / stop activities
  • change the name and icon

This is rather rudimentary. We have more information for the activities that somebody could find useful. We know:

  • which windows are currently open in an activity
  • which documents are open in an activity (for applications that support that kind of thing)
  • which documents have been opened in an activity in the past, along with which were most popular (kept open the longest, accessed multiple times etc.)
  • what is the wallpaper in the current activity, for running activities maybe even the screenshots of the windows

These are some of the things we could have:

  • marking activity as private in the sense of the private mode web browsing which would stop the usage tracking, etc. (not to be confused with encrypted activities we planned for Plasma Active which proved to be infeasible)
  • instead of creating blank or templated activities, we could optionally create activities containing the work from the last hour or so. This would move the windows and documents you are currently working on to the new activity (and move all the related recent usage statistics along with them)
  • some kind of search (for what?)

All comments on above, ideas for new things and even mock-ups are welcome!

boost.range as a dependency

I am coming close to adding boost.range as a dependency to non-library parts of KActivities.

Every time I have to filter out a list, join the elements in a non-standard manner.

So far, I am resisting the temptation, but I have no clue for how long I will be able to manage it :)

Plasma calendar mock-up

I’m working on different ideas for the activity switcher for plasma 2. Unfortunately, I haven’t had much inspiration for that. The only thing I had a real inspiration for was the calendar.

I really dislike out current calendar widget. It is confusing, and the layout is overcrowded and asymmetric (in a bad way :) ). It often confuses me when it shows events – I always think everything is today.

So I made this. It is just a mock-up, no real code yet. Opinions?

cal-mock

Slides from Qt Dev Days: continuations, asynchronous APIs, etc.

Presentation Announcement

I’ve been asked a few times to post the slides of my talk online. Lets hope we’ll have the videos soon as well.

The slides are now available on SlideShare.