why ; ?

bearophile bearophileHUGS at lycos.com
Fri May 9 16:59:18 PDT 2008


Nick Sabalausky:
> How are dynamic languages are considered to be easier/faster to spot/fix bugs?

Well:
- here I talk about Python, because I usually talk about the things I know.
- They often have a faster write code-test code loop because there's no compilation phase.
- Python has an interactive shell, you usually use that to build and try and test little complex pieces of code, for example to be sure complex slices are correct, etc. You can use that to test the code (a little piece, one line of code, a function, a class, a module, the whole program) interactively (with the debugger too, if you want).
- When you have done some tests of the method/function/class/module in the shell, you can just copy the text of that interactive session and paste it into a docstring of that function/class (and you may want to clean it a bit), that's a doctest already (adding two lines in the "main" too). Then you can add more tests like that very quickly. It's not easy to find a faster and simpler way to test code. With that you can avoid most little bugs from your code. I miss doctests in D.
- There are lints for Python too, you may want to use them too if you are developing a large code, etc. I generally don't use them for small programs.
- Python code is very readable and it has very little noise/clutter. And its formatting is very uniform across different developers. That helps you spot/avoid bugs. It helps you focus on the algorithm instead on the syntax/noise.
- Python has good builtins, and their API is very well designed, with easy short names, and it's easy to remember even if you don't use an IDE. And they are designed to often warn you if you use them in the wrong way (try using sum() to sum strings). So using them you can write code faster, and put less bugs in. D phobos std.string API shows some examples where its API can be improved, like the string cropping that's confusing between two very similarly named functions. Those things may increase your bug count.
- Like D python has many fail-safe, for example there is no ++ or while b=a+12:
etch, those details (and other parts of the Python syntax) are designed specifically to avoid you the most common bugs, even if that may cost you few extra chars of code.
- Unlike C, lot things are safe(r), there are no pointers, etc.
- Python has some higher-order functions and tools (like itertools, map, list comprehensions, apply, keyword arguments, zip, generator, iterators, decorators, etc) that help you write less code, write it in a higher level, avoiding bugs, making code more readable, etc. I have tried to put some of those things into my D libs I show here now and then, and I think I have partially succeed.
- It has a very uniform syntax and semantics, everything is managed by reference, classes and functions are objects of some metaclass, so you have much less things to think about, and you program faster putting less bugs in. Corner cases (C++ is *full* of them) slow you down and make you put *tons* of bugs into your code. Python is slower at running time than compiled languages, but it's simpler. This allows faster coding and give you less things to think of while you code or while you debug, so you have more free brainpower to think about writing the good/correct algorithm or avoiding more logical/higher-order bugs.
- having shorter names and methods everywhere allows you to write Python code with a bigger font, so your eyes can read code better, and you can spot syntax/typo bugs better. I use a smaller font when I use languages that burn a lot of horizontal space.
- Python is very well debugged (I have found only two bugs in Python so far, while I have found about 15 in DMD, and I have used D for much less time) and it has a large std lib, full of many useful things you can use quickly in simple ways to do most common things, that are almost bug-free, usually with a simple and logical API. All this speeds up your coding, because you use those things instead of re-writing them yourself, avoids you bugs, allows you to use algorithms by Knuth, etc. When you spot a bug it's nearly always yours.
- Python module system avoids you TONS of compilation/linking problems you may find in C++. D module system looks like a partially copy of the Python module system, but it has some bugs/missing parts/bad designed things that I have discussed in a nearly ignored post of mine in this newsgroup. Such module system keeps things tidy, and avoids you many bugs. (Import is dynamic, that helps for other things).
- Probably there are other things I have missed. Each of those things can be found in other languages (and D copies some of them), but when you sum them all you may find your coding speed rather high and your bug count low enough to allow you to write systems like Zope (I think it's about 1e5 - 2e5 lines long).

Now back to D!

Bye,
bearophile



More information about the Digitalmars-d mailing list