Saturday, October 13, 2012

Notepad++ and Python

Python is very sensitive to white space because it interprets code blocks by indentation level instead of curly braces.  It is vital to use uniform spacing.  I agree with those programmers who use 4 spaces for Python indentation and avoid use of tabs.  You can setup Notepad++ to help maintain this standard.  Here is how:

Open Notepad++.  Select Settings > Preferences > Language Menu/Tab Settings
Leave Tab Size at 4, but check the box that says "Replace by space".
Then go back to the main menu and select View >  Show Symbol > Show White Space and Tab.
If you look closely you will see faint dots for the spaces.
If you open a file that has tabs you will see them displayed with arrows so you know you have them.

You can do a Control-f to open the find window.  It has a radio button in search mode that has "extended" and allows you to search for the \t character, which is a tab.  You can use the "replace" tab in the find window to replace all tabs (\t) with 4 spaces.  This is a convenient way to clean up files with tabs.

If you work on projects with multiple people you will see all manner of tabbing and spacing, which can break Python code.  Python code is sensitive to indentation errors.  It is important to be consistent.  This sensitivity to indentation errors makes Python code brittle in a large programming environment.

James Coplien had this to say:  "Back in my days working in the Bell Labs Software Production Research organization ... we had  some back-of-the-envelope findings that suggested that consistent indentation style was one of the most statistically significant indicators of low bug density."[1]

Robert

[1] Forward by James Coplien, p. xxii, in Clean Code, A Handbook of Agile Software Craftsmanship by Robert C. Martin

No comments:

Post a Comment

Comments require my approval before they appear. Be patient. It will take a day or two for your comment to appear.