Forums

Unintended Consequences

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Unintended Consequences

Postby dennis » Sat Feb 16, 2008 2:20 am

One of the consequences of a syntax that doesn't include curly braces or EndFoo is that this:

# Cobra
if a = b
print a

print c


is very different from:

# Cobra 
if a = b
print a

print c


with the only difference a missing tab.

In this sample:

Code: Select all
// C
if( a == b)
{
    printf( a )
}

printf( c )


accidentally deleting or omitting the closing brace would cause a compiler error and only moving it would cause the change in results seen in the Cobra examples above.

I like the clean appearance of Cobra code and the idea of not having to match braces, but it introduces a new potential source of errors that must be carefully watched.
dennis
 
Posts: 21

Re: Unintended Consequences

Postby khjklujn » Sat Feb 16, 2008 7:25 am

True enough. I can remember having a similar concern when I first started using Python. And in Python I do occasionally place a chunk of code in the wrong block. But in practice I've found I do it less frequently in Python than I do in languages that use explicit blocking.

It's most likely to occur in situations where you have nested blocks. In the following examples, doSomething7 is supposed to be used in the j loop. Is it correct?
Code: Select all
for(i = 0; i < 10; ++i)
{
    doSomething1();
    a = someFunction1();
    if(a == 5)
    {
        doSomething2();
        for(j = 0; j < 10; ++j)
        {
            doSomething3();
            b = someFunction2();
            if(b == 5)
            {
                doSomething4();
                for(k = 0; k < 10; ++k)
                {
                    doSomething5();
                    c = someFunction3();
                    if(c == 5)
                    {
                         doSomething6();
                    }
                }
            }     
        }
        doSomething7();
    }     
}

Code: Select all
for i = 0 .. 10
    doSomething1
    a = someFunction1
    if a == 5
        doSomething2
        for j = 0 .. 10
            doSomething3
            b = someFunction2
            if b == 5
                doSomething4
                for k = 0 .. 10
                    doSomething5
                    c = someFunction3
                    if c == 5
                        doSomething6
        doSomething7

One of the things you'll discover when you start working with a language that uses indentation for block level definition is that explicit blocking was actually creating more errors than it was preventing.
khjklujn
 
Posts: 29

Re: Unintended Consequences

Postby dennis » Sat Feb 16, 2008 5:46 pm

I can't say which system I think is more prone to errors. My point is that they are possible in both but they take different forms. If you're used to watching for one it may take some practice to get used to the other. Forewarned in forearmed.

At the very great risk of instigating yet another rehash of the C bracket style religious war, I must reveal that my usual style is of the form:

Code: Select all
// C
if( ) {
    dosomething
    if( ) {
        dosomething
        for( ) {
            dosomething
            }
        }
        dosomething7
    }
dosomething


which is visually similar to the indentation style. And when there's a long block, I'll add something like "// end for j" after the closing brace.

There's no substitute for a good editor/IDE in any case.
dennis
 
Posts: 21

Re: Unintended Consequences

Postby Charles » Sat Feb 16, 2008 7:32 pm

The most infamous example of the weakness of the curly syntax is this:
Code: Select all
if (x < y)
    if (name == null)
        foo();
else
    bar();


The programmer intended bar() to execute if not "x < y" as evidenced by his indentation. That is not what will happen as the parser puts the "else" with the nearest "if".

Here's an interesting post on Python's indented blocks: http://www.secnetix.de/~olli/Python/block_indentation.hawk

I agree with the author that mixing spaces and tabs in the same line for indentation is a bad idea. So much so that Cobra will give an error with what sequence of characters it found:

foo.cobra(5): error: Cannot mix tabs and spaces in indentation. [TAB][TAB][SPACE]...

I disagree that tabs are to be avoided. I use tabs in all my code except for some test cases to make sure the space people will be happy. (And that space bug with doc strings is fixed for the next release.)

The earliest language I know of with indentation, not counting make files, is Python's ancestor, ABC.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Unintended Consequences

Postby khjklujn » Sun Feb 17, 2008 7:36 am

Code: Select all
// C
if( ) {
    dosomething
    if( ) {
        dosomething
        for( ) {
            dosomething
            }
        }
        dosomething7
    }
dosomething

Hmm, in the context of the example I provided, I think I may have to change my religious affiliation. :oops: I rather like the convention you presented. It'll be a hard sell in C shops, but I can think of a couple of javascript people who could be converted.
khjklujn
 
Posts: 29

Re: Unintended Consequences

Postby Thorsten » Fri Feb 29, 2008 7:45 pm

Code: Select all
// C
if( ) {
    dosomething
    if( ) {
        dosomething
        for( ) {
            dosomething
            }
        }
        dosomething7
    }
dosomething


This is the worst thing you can do, when using a curly-brace language.

Because things like this are allowed:
Code: Select all
if (a)
    doSomething1();
else
    doSomething2();


Using curly braces in the same line like the condition forces you to read extremely carefully the code to distinguish the cases shown. Therefore I do not recommend to put curly braces into the same line like the condition. In fact, I really hate it, to be honest. :D

Thorsten
Thorsten
 
Posts: 2

Re: Unintended Consequences

Postby Charles » Fri Feb 29, 2008 8:25 pm

Doesn't matter since Cobra uses indentation-only, like Python. :D
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 58 guests