octave-maintainers
[Top][All Lists]
Advanced

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

Re: Default arguments


From: Andreas Romeyke
Subject: Re: Default arguments
Date: Fri, 15 Dec 2006 09:04:28 +0100
User-agent: Icedove 1.5.0.8 (X11/20061116)

-----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]