[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Tabs v.s. spaces

On Tue, Nov 18, 2003 at 11:04:48AM -0800, Steve Lamb wrote:
> H. S. Teoh wrote:
> >Yeah, 'whitespace' about sums up the value of it. Except to Python
> >programmers, of course. :-P :-P
>     Quite the contrary.  First off generally flames are from the 
>     uninformed. Since in most cases the evils of whitespace are spouted off by 
>  those who have never once touched Python it is generally they who are 
> flaming and not those who have actually used it.

First off, note the smileys.

>     Secondly clearly they are deriving some worth from it.  I mean you did 
> post 12 messages to this thread.  More than me, in fact.

Secondly, note that I was merely expressing my personal preferences.

>     Finally I am still waiting for an answer to my two questions posed to 
>     you.
> 1: Have you ever used Python?

Not for any non-trivial task, although I did try to learn it some time
ago. Recently, I had the chance to take another look at it; however, I
found Ruby, which seemed to have the best of both Perl and Python plus
true object-orientation. So when I move on from Perl (which I love, but
which admittedly has its flaws too), it will be to Ruby, not Python.

> 2: Provide an example of such free-style coding that you speak highly of.

Example (Perl):
	  m/\G (void|int|float) /gcx && do { $type = $1 };
	  m@\G /\*([^*]+|\*[^/])*\*\/@gcx && next PARSE;
	  m/\G \s+ /gcx && next PARSE;
	  m/\G \{ /gcx && do {
	    my @statements =  parse_block;
	    push @block, @statements;

OK, this example is a bit contrived (it doesn't really do anything
useful), but it illustrates one situation where you'd like to be able to
stick a block on a single line in one case (e.g., the first match) but
keep it as an indented block in another case (e.g. the last match).

More examples (C++):

	try {
	  obj_handle handle = get_handle();
	  if (handle.error()) throw exception(obj_handle::write_error);

	  if (handle.error()) throw exception(obj_handle::verify_error);

	  if (handle.error()) {
	    if (retry_needed) {

Another contrived example, but shows the same principle: in most cases the
throw can remain on the same line to avoid code clutter, but when
something more involved is needed, you break it up into an indented block.

More Perl examples:
	open FILE, $file1 or die "Unable to open $file1: $!\n";
	while (<FILE>) {
	close FILE or die "Error while reading $file1: $! ($?)\n";
	open FILE, $file2 or do {
	  ... # do something else

Again, the flexibility to use or not use indented blocks help greatly in
readability here.

Next example (C):
	for (i=0; i<n; i++) {
	  struct iterator *it;
	  for (it=get_iter(data.values.list);
	       more_elements(it) && value_of(it)!=search_value;
	  if (value_of(it)==search_value) return 1;

Output example (C/C++):
	if (debug==1)
		printf(gettext("Debug dump:\n"
		               "Current registers: %s\n"
		               "Watch values: %s\n"),

Yes, this one is contrived, but you really *do* want the freedom to indent
in cases such as here in order to emphasize the nested function call.

Finally, constructors example (C++):
	classA::classA(int x, int y) : xcoor(x), ycoor(y) {}
	classB::classB(void (*)(int x, int y),
	               void classA::*callback(int i)) :
		xcoor(x), ycoor(y), cb(callback) {
	  if (callback==NULL) throw exception();

Note how the flexibility to indent makes it possible to write simple
constructors on one line (classA), and layout complex constructors such as
in classB such that separate elements are clearly separated.


Right now I'm having amnesia and deja vu at the same time. I think I've
forgotten this before.

Reply to: