Recently in Code craft Category

Perl::Critic finds annoying little bugs in your code.

| No Comments

My work colleague Mike O'Regan created a policy for the latest version of Perl::Critic.

Now if you have a line of code like this:


my $n += somefunc();
# Should be my $n = somefunc();

Perl::Critic will tell you

Augmented assignment operator '+=' used in declaration at line X, column Y. Use simple assignment when initializing variables.

If you haven't let Perl::Critic loose on your code yet, now's a great time to try.

To the loyal Perl::Critic users, what's the nastiest bug Perl::Critic found for you? Let me know in the comments.

Perlbuzz news roundup for 2011-12-19

| No Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Perlbuzz news roundup for 2011-11-14

| No Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Perlbuzz news roundup for 2011-11-07

| 2 Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Perlbuzz news roundup for 2011-05-09

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Perlbuzz news roundup for 2011-02-17

| No Comments

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Develop sensible coding habits on the path to true Laziness

| 7 Comments

Whenever I walk in my front door, I walk straight to the kitchen, open the first drawer, put in my keys and wallet and cell phone, and close the drawer. Always, without fail, no matter how bad I might have to take a leak. It is no less convenient for me to do this than to drop my stuff any old place. It is a habit that I have maintained for close to 30 years now. And I never ever ever have to wonder where I put my keys, or my phone, or my wallet. Ever. Having to do so would be extremely inconvenient. In fact, it would be EXTRA brain cycles on my part to put them where they do not belong.

When the phone rings, I know where it is. I don't run around wondering "Where's my phone?" trying to triangulate position. It's always there in the kitchen drawer. Always.

I also always put on my seat belt in the car. I never wonder if it's "necessary". I just do. Thinking about if it is "necessary" is also inconvenient. It takes brain cycles that I don't need to spend. I also know that the one time I need my seat belt and I don't have it on, I'm screwed.

Trying to decide to save three seconds by not buckling my seat belt is false laziness. And so is worrying about when you can leave out warnings and strict from your Perl programs.

Yesterday in IRC, someone was lamenting that Perl 6 effectively has warnings and strict on by default. All variables must declared before use. This person was one of those programmers who tried for the premature optimization of saving some typing. He forgot that typing is the least of our concerns when programming. He forgot that programmer thinking time costs many orders of magnitude more than programmer typing time, and that the time spent debugging can dwarf the amount of time spent creating code.

He also forgot that he's a human.

Checks like warnings and strict are there because we are fallible humans. We type $totl instead of $total, and we want Perl to tell us that. We try to dereference scalars and since we're no longer using Perl 4, we want Perl to tell us about that, too. We want Perl, which is a program and doesn't get sloppy and forget things, to catch us when we are humans and get sloppy and forget things.

Buy and read The Pragmatic Programmer and Code Complete and develop a solid set of programming habits from them. Sensible habits that are followed even when you might be able to get away without them are the true Laziness.

ack 1.94 released

| No Comments

Version 1.94 of ack, my source code searcher designed for programmers, has been released. This is the first release in almost a year.

You can install it as App::Ack from the CPAN shell, or you can use one of the installation methods described at http://betterthangrep.com/

Here are the changes.

Fixes

--man and --version now return with an exit code of 0. They used to exit with a 1. Thanks to Bo Borgerson.

Fixed ack misbehavior when using --match or not specifying a regex:

ack --match Sue
cat foo | ack --perl

Fixed issue 74: analog to grep, 'ack --count regex file' now only outputs the number of found matches, if only one file is given.

Fixed issue 76: Giving both --line and a regex (with --match) now leads to an error. The same is true for -f or -g in conjunction with --match. (Torsten Blix)

Fixed issue 80: Piping into ack --count now works as expected. ack always returned 0 when piped into, no matter how many matches where found. (Torsten Blix)

Fixed issue 81: .ackrc now ignores leading/trailing whitespace. (Torsten Blix)

File type updates

Added --verilog, --vhdl and --clojure.

Files that match *.mk and *.mak as well as GNUmakefile are now included in the ack filetype 'make' (issue 88).

Added RSpec's .spec type to the --ruby list.

Added support for Go and Delphi.

Ignores Monotone's _MTN directories.

Added .xsl to the list of --xml files.

Enhancements

The --ignore-dir option now can ignore entire paths relative to your current directory. Thanks to Nick Hooey. For example:

ack --ignore-dir=t/subsystem/test-data

Added --invert-file-match switch to negate the sense of the -g/-G switches. Thanks, Lars Dieckow.

Updated the README to Markdown format. Thanks to Mark Szymanski for the idea.

Added docs for -r, -R, --recurse options.

Added new switch --color-lineno and environment variable ACK_COLOR_LINENO, which allow setting the line number color. (Marq Schneider)

Added option --show-types to output the types that ack considers a file to have. (Torsten Blix)

ack --count --no-filename regex doesn't output a list of numbers but a sum of all occurences. This is NOT what grep does but is the more sensible behavior.

Run PHP tests in your Perl test suite

| 5 Comments

Sometimes you've got a big codebase that isn't just Perl. Maybe you've got PHP mixed in with it, and you want to test the PHP along with all the Perl code, too. Perl's prove program doesn't care if the testing results it parses are from Perl, PHP or even static files, so long as they're in the TAP format. However, actually getting prove to run those PHP programs takes a little doing. Fortunately, Test::Harness 3.xx has hooks for source handlers.

David Wheeler has written about running PostgreSQL tests under prove in his blog and I stole from his code shamelessly to create TAP::Parser::SourceHandler::PHP, released to the CPAN this morning.

So now, to run the Perl .t files and the PHP .phpt files all in one swell foop, here's what we do at work:

prove -r \
    -I/home/alester/proj/Lib \
    --source=Perl --ext=.t \
    --source=PHP \
    --ext=.phpt \
    --php-option=include_path=/home/alester/proj/Class \
    --php-option=extension=.phpt

That --source=PHP tells prove to load up TAP::Parser::SourceHandler::PHP. The --php-option tells prove to pass those options through to the SourceHandler. If we had PostgreSQL tests or MySQL tests, we could use the SourceHandlers for those that David Wheeler has written as well.

Now we can test everything all in one run, and we get all the benefits of Test::Harness 3.xx, like parallel tests and TAP archiving and so on.

To InformationWeek re: static code analysis

| 1 Comment

Sent to editor of DrDobbs/InformationWeek

I enjoy Sid Sidner's article on static code analysis tools, but was surprised to see two big omissions, especially as they may provide a low-cost point of entry to the organization just starting to look at static analysis.

First, PC-Lint is a relatively low-cost tool that does a fine job of C/C++ analysis. It's been around for years, and has found many C bugs in my code back in the early 90s. I've also been using the open source Splint, for years on the Perl 5 and Parrot open source projects. Although Splint's not nearly as complete a package as Coverity's Scan product (Coverity runs Scan on dozens of open source projects for free as a service to the community), it's a great introduction to the power of static code analysis tools. I also suggest readers check the "List of tools for static code analysis" page on Wikipedia.

Second, one crucial point missed is how any tool is going to require tuning. Splint will generate hundreds of errors in each source file on its first run on your code, since nobody in the real world is as pedantic as the tool is. Each organization will have to decide which policies are worth following, and which are just noise.

Finally, static code analysis isn't strictly for C++ and Java. Many dynamic languages have similar tools. For example, Perl::Critic is a fantastic tool for analysis of Perl code, as well as an extensible framework that lets each organization create custom policies to fit its own development practices.

« Business | Main Index | Archives | Community »