Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Wouldn't it be nice if Matlab supported C-style comments?
Replies: 8   Last Post: Mar 12, 2013 6:46 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Paul

Posts: 263
Registered: 2/23/10
Re: Wouldn't it be nice if Matlab supported C-style comments?
Posted: Mar 10, 2013 6:25 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Mar 10, 4:31 pm, TideMan <mul...@gmail.com> wrote:
> On Monday, March 11, 2013 8:02:53 AM UTC+13, Paul wrote:
> > On Mar 10, 1:27 pm, dpb <n...@non.net> wrote:
>
> > >On 3/10/2013 12:04 PM, Paul wrote:
>
> > >> C-style comments are great!  You don't have to comment everything
>
> > >> to the right of the comment character.  You can comment out a
>
> > >> snippet of code in the middle of a line, and keep the rest of the
>
> > >> code to the right of it.
>
> > > OTOH, you have to close the opening comment character or it keeps on
>
> > > going..."there is no free lunch"
>
> > There only no free lunch if you choose one commenting style or the
>
> > other.  C++ allows both.
>
> > >> I (and I suspect others) spread complex statements and expressions
>
> > >> over several lines, using deeper levels of indentation as cognitive
>
> > >> cues to show which parts of the code bind together more tightly due
>
> > >> to higher precedence.  You can't comment out a snippet of code in
>
> > >> middle of those lines.  If you want to experimentally try a number
>
> > >> of variations of the code, you have to duplicate the whole chunk of
>
> > >> code
>
> > > Well, no, you only need duplicate the line which contains the
>
> > > snippet you're trying to experiment is at least one alternative.
>
> > If they are different statements, yes, but I'm talk specifically about
>
> > spreading a complex statement or expression over several physical
>
> > lines so that you can use the indentation levels.  For example, if
>
> > you're experimenting with different forms of a symbolic mathematical
>
> > statement, the form of the right hand side might change a lot, so you
>
> > want to keep the corresponding matlab expression in a form that is
>
> > easily manipulated and easy to detect manual errors.  You need the
>
> > cognitive assistance of indentation levels:
>
> >    function resid = myfunc(llt,u)
>
> >       resid = ...
>
> >          log( ...
>
> >             1 - ...
>
> >             (
>
> >                1 - exp(-exp(llt)) ...
>
> >             ) ...
>
> >             ./ exp(llt) ...
>
> >          ) ...
>
> >          - log(u)
>
> >       ;
>
> >    end % function
>
> > If you use Matlab's comment character to try commenting the one line:
>
> >    % ./ exp(llt) ...
>
> > you get an error message on that line.  It would be nice to be able to
>
> > close the comment.  Using C-style commenting, it might look like:
>
> >    /* ./ exp(llt) ... */
>
> > Like I said, replicating the whole chunk of code just to be able to
>
> > hack one line (while still retaining the original) creates a whole
>
> > valley of experimental code, disrupting the continuity in reading the
>
> > rest of the code.
>
> I'm intrigued that you think your code is more readable than this:
> resid = log(1 - (1 - exp(-exp(llt))) - log(u);
> because I think your code is ugly and not intuitive.  I had to write it on one line to follow it.
>
> With the one-line version commenting is easy:
> % resid = log(1 - (1 - exp(exp(llt))) - log(u);
>
> It just goes to show: different horses for different courses.


I agree. But I think I described the situation for which the spread
out version is very appropriate. Not for looking pretty, as you
suggest, or intuition. It's for when you're doing symbolic
manipulation (say on a piece of paper or Mathematica) and then you
have to keep track of what parts of the Matlab code to manipulate in
order to correspond to the symbolic expression. When you "neaten" up
the code by cramming it all onto one line, it's very easy to lose
track of the level of binding.

I'm not sure why there is such a focus on *whether* this indentation
is appropriate, it's common practice in programming to organize
complex code this way. I believe it's also been suggested to me in
the past in this newsgroup. But that's all really beside the point.
Not everyone uses it, and that's fine. If you don't expect to modify
the expression much, I can see why you would want to compress it onto
one line. Then it becomes more compartmentalized and out of the way
when trying to get one's head around the rest of the code.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.