Tuesday, September 22, 2015

vim plugins that I cant live without!

Here is a list of vim plugins, which are absolute time-savers!


How many times, you'd have copied and pasted a line from gcc error/warning, in the form
and then had to remove the :line and :col before editing it in vim!
this nice little plugin will open into the same line and place the cursor at the right column!


For C, of course the built-in % key does the trick of taking you to the matching brace/paren, but what about languages where you have begin .. end, or if .. fi - that is where this is really useful - overloads the % key for keyword-pairs!


Align anything! declarations, comments, function-headers ... saves a lot of time!


I use git, and no day goes by without looking into git blame of one or the other source file! Its neat to see that from within vim and can navigate to complete diff [if I have to] by just hitting enter!


I like to keep everything under version-control, so all my docs are in text (Markdown) and checked into git. I use DrawIt to create cool ASCII diagrams :)

Tuesday, September 08, 2015

Enforcing Coding-Style checks on diffs

Have you seen how most of the code review [comment]s look like?

- This line exceeds 80 columns ...
- No space after ...
- No comma here ...
- Conditionals must have a block ...

Though these look insane, they ensure that code looks saner, when everyone follows the company's coding guidelines - unfortunately its not easy to enforce! Everyone has their own favorite editor and their own settings, add to this - there will be third-party code which has its own different style.

The best one can do is - when new code is added, can warn if the delta violates coding guidelines or not. Wait, delta? - diff ? How do we run checks on a diff!? Its not easy, but not too complicated either! at least for most common checks which the author should have figured out himself/herself before posting the code for review!

My approach: Simple Perl regex checks on diff hunks!

(If the mention of Perl + regex makes you feel nauseatic - stop here. :-))

Note: I tried this for C code diffs only.

We're not dealing with a function in its entirety, its just a diff, so how do we go about ?
of course, line-by-line :-/ duh!

  • Use unified diff, to get the context (file name, line range) 
  • Do some basic line merging logic when we are sure its not complete [1]
  • Check line by line, for basic style enforcement
    • length > 80?
    • trailing white-space ?
    • if/else not followed by a block ({) ?
    • etc...
  • Keep track of line numbers so that meaningful messages can be printed, pointing to the exact line where the issue is!

And how do we run this script automatically ?
Since I use git, git hooks! (just make it part of pre-commit hook)

Does it work well?
surprisingly well! :-)

[1] I used parentheses balance as a check to know if I need to merge lines or not.


Do we need to do any extra work, than what's written above?
We do have to take care of  /* comments */ , and "strings"! regexes can be easily fooled, if we don't take enough care!

Can we make this fool-proof?
No! this can only be best-effort