Archive for the ‘Programming’ Category

{specialized, general-purpose} × {text editors, IDEs}

Tuesday, February 11th, 2014

Some thoughts about text editors, IDEs, specialized or general-purpose applications.

Several languages Other tasks Plugins
Specialized text editor No No ?
General-purpose text editor Yes No Yes
Specialized IDE No Yes ?
General-purpose IDE Yes Yes Yes

The above table summarizes how I view the text editors and IDEs field.

Several languages

A specialized application focus only on one programming language, or several languages used together. A general-purpose application can support completely different programming languages and platforms.

Other tasks

A text editor focus only on editing text or source code, while simplifying as most as possible the writing of this code. An IDE integrate other features like building the source code, running it, writing commits, integrate a debugger, and so forth.

Plugins

For obvious reasons, a general-purpose application should have a plugin system. For a specialized application, the need for plugins is less obvious.

Two extremes

The general-purpose IDE with plugins is one extreme. The user interface must be generic enough to suit plugins and different programming languages. And it takes more time to learn and configure the application.

The specialized text editor without plugins is the other extreme. The developer has full control on the user interface, so the application has a potentially better usability. The application requires less time to learn and configure. Ideally, it should Just Work.

Not reinventing the wheel for specialized applications

Write libraries for the common features! Or at least ensure that your code is reusable. This is not limited to text editors and IDEs, I’m sure you can apply the same principles on your favorite field.

Align parameters of a C function declaration, with lineup-parameters

Thursday, July 25th, 2013

Renaming a GObject class can involve a lot of manual work. Once the type is renamed, the parameters of the function declarations are no longer aligned. In the following example, GtkSourceSearch has been renamed as GtkSourceSearchContext:

guint
_gtk_source_search_replace_all (GtkSourceSearchContext *search,
                                const gchar     *replace,
                                gint             replace_length)
{
        /* ... */
}

It is a GNOME convention to align the parameters, so the code must be fixed:

 guint
 _gtk_source_search_replace_all (GtkSourceSearchContext *search,
                                 const gchar            *replace,
                                 gint                    replace_length)
{
        /* ... */
}

As programmers, we don’t like doing such a manual work, and even less when we are renaming a big class. Unfortunately, the Indent program doesn’t have an option for that. If you use Vim, there is the Align plugin, that have the \afnc alignment mapping. But it doesn’t align the parameters on the parenthesis.

So I wrote a script! The result is in my gnome-c-utils repository (containing only the lineup-parameters script for the moment).

To detect the parameters, a few regular expressions are used. There are quite a lot of restrictions, but it should work for most GNOME software. To have a perfect script, libclang could be used, but I’ve never used it, so regular expressions was a quicker solution.

The script can be used to convert an entire file, or can be used from a text editor (stdin is read, and the output is written to stdout). I’ve documented how to add a key binding in Vim. So by pressing a single key, the parameters are correctly aligned!

This is just a small step to improve the developer experience in GNOME. But I hope this will be useful for other developers. I dream of a GNOME C IDE that handles the boilerplate (for renaming classes too), that aligns the parameters and the function prototypes in the headers, with auto-completion for signals and properties names, and so on.

Developing graphical IDEs the UNIX way

Wednesday, July 3rd, 2013

Programming on a UNIX environment generally involves the use of command line tools: vim/emacs, git, grep, sed, screen/tmux, and so on. What is really nice about these tools is that they do only one thing, but do it well. Now, what about people not wanting to learn shell commands? And sometimes (sometimes), graphical interfaces are more convenient.

An IDE is an integrated development environment. What people think about this “environment” is generally a bloated program (*cough* eclipse *cough*). But what if the environment is not one big application, but several small programs? The small programs can interact a little together through D-Bus, if needed.

Let’s take a few examples.

Specialized text editors

To have a good user experience, the text editor should be smart enough to know the language used (C, C++, Python, Vala, LaTeX, etc.) and implement features specifically for the language. A few examples:

  • Code completion;
  • Switching between the call of a function to its definition;
  • Have an overview of the file (the list of functions, or the structure of a LaTeX document).

Having only one text editor for several types of tasks (C and LaTeX for instance) is not convenient. It works with vim or emacs, but at the cost of passing lots of time in the configuration files. If a graphical application have thousands of settings, we end up with something worse than eclipse. No, instead, there should be one text editor for each task. And one another general-purpose text editor that don’t include specialized features.

This is not just about settings. With a single text editor for all tasks, the UI must be generic enough for the plugins, but they will always be restrictions. On the other hand, with an independent application, the UI can be exactly what is needed: not too generic nor too restrictive.

The common features between all those specialized text editors can be shared in a library. A project is to make the gedit source code more reusable. It would have been a nice GSoC for me (see this draft proposal for more details), but instead I work on GtkSourceView, which is also nice.

git

Working on a git repository can be a bit difficult at the beginning. A graphical application that implements the basic features can be useful for some people. But it doesn’t need to be available in the same program as the text editor!

Creating a commit, navigating through the repository history, pull, push, changing branches, … have nothing to do in a text editor, in my opinion. There are some things that can be useful in the text editor though: if there is an integrated file browser, we can mark the modified/added/removed files. And in the margin of the text content, we can mark the modified sections.

Search and replace

The search and replace in a text editor can be limited at one file at a time. The search and replace entries can be shown in the top-right corner of the file, with a little button to show and hide the replace entry.

The search and replace in multiple files is another beast. Integrating it nicely in the text editor is a difficult task. Generally the developer choose a dialog window, because it is the best available option, but it partially hides the file contents…

Why not a small application that do only that? It would roughly select a directory, and search in all files in this directory. The text editor can have a menu item “Advanced search and replace” that would launch the small program with some parameters. The advantage of this small program is that the UI is really better, since it doesn’t have all the text editor features.

When we replace some text, sometimes we want to do small modifications afterwards. But normally, the specialized text editor features would not be needed in the small search and replace application.

The specialized text editors would already have features to edit several files at once. For example, when we change the name of a function, all the calls to the function can be renamed too, and adjusting automatically the indentation of the parameters (should be doable).

Conclusion

The general idea is to have small programs with a clean and simple UI, which don’t require lots of configuration, and that work well together. Ideally, they should Just Work™, like other GNOME applications.

About code quality and maintainability

Wednesday, August 1st, 2012

It can be difficult to write clean code, that is easy to understand, split in nice and well-defined functions that do one thing but do it well, with good variables names, and so on. For the maintainability, it is really important.

In fact, a code is read far more often than it is written. For fixing a bug, adding a feature, or simply see how it is implemented.

General Advices

There are some general advices, for example:

  • A function’s name should be sufficient to know all what the function really does.
  • Hide implementation details, and use the domain-specific vocabulary, no technical words, if possible. An extreme example: get_last_document() is better than get_top_stack().
  • Avoid focusing on performance from the beginning. About 10% of the code is responsible of 90% of the execution time. An optimized code is more difficult to understand, so it is better to optimize only the 10%.

Every Details Matters

Small details can also improve the code readability. Take for example this line:

g_return_if_fail (foo >= min_value && foo <= max_value);

And compare it with this one:

g_return_if_fail (min_value <= foo && foo <= max_value);

Which one do you prefer? In the second sample, we can visually see the foo variable between the minimum and maximum values.

Many such small improvements, taken together, make the code more enjoyable to read.

Code Complete

We can learn these and other advices by reading some (good) existent code, by working with more experienced developers, AND by sitting in a comfortable sofa with a cup of coffee or tea while reading a book.

I personally have read Code Complete last year. It seems the reference on the subject. But there are other similar books, for instance The Pragmatic Programmer.

Although the editor of Code Complete is Microsoft Press, the book is not related to Microsoft or Windows. The author explains sometimes stuff related to open source, UNIX and Linux, but one can regret the total absence of the mention “free software” and all the benefits of freedom: be able to learn by reading other’s code, etc. But if you are here, you already know all of this, because you are on the good side ;)