Beauty of declarations.

M

Malcolm McLean

I've been programming mainly in Matlab now for a couple of months.

Like many high-level languages, Matlab allows variables to be created
on the fly. This is a real nuisance. If you make a spelling mistake,
the function will execute. Then if the mispell is a lvalue it will
happily assign to the wrong variable. If it is a rvalue then the code
stops, at that point.
The problem is that some functions take hours to run. So a quick
modificiation, say in a routine to print out the results, can blow the
whole thing away. Testing on small data sets can sometimes be easier
said than done.

The C system, whereby a misspelt variable name will result in a
compiling or linking error, is far more convenient.
 
J

Julienne Walker

Like many high-level languages, Matlab allows variables to be created
on the fly. This is a real nuisance. If you make a spelling mistake,
the function will execute. Then if the mispell is a lvalue it will
happily assign to the wrong variable. If it is a rvalue then the code
stops, at that point.

This is such a common (and ancient) problem that I'd call it a quality
of implementation issue for the compiler/interpreter. The issue is
subtle enough and serious enough that I'd probably implement something
like a fuzzy comparison on identifiers in scope and if there's a close
enough match between two of them without being exact, throw up a
warning:

warning -- line (xxx): <identifier_a> is similar to a previously used
identifier in this scope (<identifier_b>) by 95%. <identifier_a> may
be a misspelling of <identifier_b>.
 
M

Malcolm McLean

On Mar 1, 9:54 am, Malcolm McLean <[email protected]>
wrote:

This is such a common (and ancient) problem that I'd call it a quality
of implementation issue for the compiler/interpreter. The issue is
subtle enough and serious enough that I'd probably implement something
like a fuzzy comparison on identifiers in scope and if there's a close
enough match between two of them without being exact, throw up a
warning:
To be fair Matlab does put red lines under assignments to variables
which are then unused. However it thinks that r values must come from
the global workspace, and it can't detect misspelt functions. I tend
not to see these, however.
 
B

bartc

Julienne said:
This is such a common (and ancient) problem that I'd call it a quality
of implementation issue for the compiler/interpreter. The issue is
subtle enough and serious enough that I'd probably implement something
like a fuzzy comparison on identifiers in scope and if there's a close
enough match between two of them without being exact, throw up a
warning:

warning -- line (xxx): <identifier_a> is similar to a previously used
identifier in this scope (<identifier_b>) by 95%. <identifier_a> may
be a misspelling of <identifier_b>.

It doesn't need to be that complex: just a summary listing of all
identifiers used, perhaps in alphabetical order, can quickly show up rogue
names.

(And your example is not typical: probably you *do* have identifier_a and
identifier_b; the problem would be when writing identifer_a by mistake; you
scheme would show up many false positives because many names *are* similar)
 
J

Julienne Walker

It doesn't need to be that complex:  just a summary listing of all
identifiers used, perhaps in alphabetical order, can quickly show up rogue
names.

(And your example is not typical: probably you *do* have identifier_a and
identifier_b; the problem would be when writing identifer_a by mistake; you
scheme would show up many false positives because many names *are* similar)

I'm not sure I understand what you're trying to accomplish here. Your
nitpicking effort would be better spent on something other than a ten
second speculative solution for a problem that I'm not seriously
trying to solve and is off-topic in clc. ;-)

Just take it for what it is: a quick idea to supplement the claim that
not diagnosing the stated problem suggests a quality of implementation
issue.
 
K

Kaz Kylheku

I've been programming mainly in Matlab now for a couple of months.

Like many high-level languages, Matlab allows variables to be created
on the fly. This is a real nuisance. If you make a spelling mistake,
the function will execute. Then if the mispell is a lvalue it will
happily assign to the wrong variable. If it is a rvalue then the code
stops, at that point.

Mature dynamic languages solve these problems with compile time inference.
The C system, whereby a misspelt variable name will result in a
compiling or linking error, is far more convenient.

Somehow we are not likely to see a mass migration of Matlab users to C.
 
B

Bruce C. Baker

Kaz Kylheku said:
Mature dynamic languages solve these problems with compile time inference.

Mature, statically-typed imperative programming languages require the
programmer to explicitly declare all variable types, lifting the burden of
determing those types from those who subsequently need to modify or extend
the code.
Somehow we are not likely to see a mass migration of Matlab users to C.

They'd do much better to migrate to Java and/or C#.
 
M

Malcolm McLean

Somehow we are not likely to see a mass migration of Matlab users to C.
Matlab has C and Fortran interfaces. So you can write functions in C.

Pretty much the first thing I did on getting my Matlab system was to
root out the C subsytem and start coding functions in C.
However actually the gain in speed doesn't seem to be that great. It
doesn't interface nicely with the rest of the system, and it's very
difficult to debug. A segfault will take down Matlab, without a
readable error message, which makes testing very tedious.

However I think that at least some Matlab programmers will go in the
other direction, taking up C to write processor-intensive library
functions.
 
B

bartc

Malcolm McLean said:
I've been programming mainly in Matlab now for a couple of months.

Like many high-level languages, Matlab allows variables to be created
on the fly. This is a real nuisance. If you make a spelling mistake,
the function will execute. Then if the mispell is a lvalue it will
happily assign to the wrong variable. If it is a rvalue then the code
stops, at that point.
The problem is that some functions take hours to run. So a quick
modificiation, say in a routine to print out the results, can blow the
whole thing away. Testing on small data sets can sometimes be easier
said than done.

The C system, whereby a misspelt variable name will result in a
compiling or linking error, is far more convenient.

The same problem can occur in C:

double x=0,y=0;

x=fn();

printf("Result = %f\n",y);

After hours waiting for the function to execute, the result is placed in the
wrong variable.

That is, if the wrong name coincides with another declared name of the right
type.

But yes, typing a non-existent name is more difficult in C and other
languages that work the same way. Although, you might have expected Matlab
to have warned that the variable assigned to was not used anywhere else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top