octave-maintainers
[Top][All Lists]
Advanced

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

Re: Default arguments


From: Shai Ayal
Subject: Re: Default arguments
Date: Fri, 15 Dec 2006 10:36:43 +0200

While I agree with you in principle, I think we all need some quick
and dirty ways to quickly code what we want. While it is good to have
features in octave to enable the building of large applications in a
structured and maintainable way, (functions, containers, classes in
the future), it is also the case that a lot of octave users aren't
into producing large applications but rather small m-files to automate
repetitive tasks, and for them the quick yet bad in CS terms methods
are very helpful.

As pointed out, many (maybe even most) built-in m-files use default
arguments in a way which lets you use the most "generic" arguments w/o
having to specify them, so command line usage of octave is less
tedious.

Shai

On 12/15/06, Andreas Romeyke <address@hidden> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi John,


> We already have default values for arguments for many functions, so
> it's a little late to say that the language should not support them.
> Currently, you have to write something like
>
>   function foo (arg)
>     if (nargin < 1)
>       arg = 42;
>     end
>     ...
>
> which is somewhat verbose and easy to screw up.  Wouldn't it be better
> to be able to write
>
>   function foo (arg = 42)
>     ...
>
> instead?

Sure. But default arguments will  be crap. There is no real reason to
use them I think. It is mostly a sign of smelling code and bad
interface design. In your example you deny compiler hints, because if
you unintentional call foo() without arguments.

Do you have a good example where defaults really useful?

- From a functional point of view the right way to rewrite your example
above is:

function foo(arg)
...

function bar()
    foo(42)
...

This avoids complexity too, isolates code, is more readable and has a
clear interface. You will also be warned from Octave if you have
forgotten to call foo() with arguments.

Therefore I would say, if anyone use defaults it should be punished by
extra code lines. :)

Bye Andreas

- --
Software Developer / Dipl. Inform. (FH)
Max Planck Institute for Human Cognitive and Brain Sciences
Department of Psychology
Stephanstr. 1a, 04103 Leipzig, Germany

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFglcKGBhgJGthyeERAo0WAKC/NpcWmHQqxfvXAED/QmHIxGru6QCfTvto
AXOUV3qFUUzeSCQY3AFV8zQ=
=RxsI
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]