const main args?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Aug 12 18:48:01 PDT 2011
On Saturday, August 13, 2011 02:10:35 Andrej Mitrovic wrote:
> On 8/13/11, bearophile <bearophileHUGS at lycos.com> wrote:
> > Andrej Mitrovic:
> >> void main(string[] args)
> >> {
> >>
> >> args.popFront; // get rid of name
> >>
> >> foreach (arg; args)
> >> {
> >> }
> >>
> >> }
> >
> > If I see code like that I probably rewrite it like this:
> >
> > void main(in string[] args) {
> >
> > foreach (arg; args[1..$]) {
> > }
> >
> > }
>
> And what benefit is there to that? I pop the argument and then don't
> have to worry about it ever again. Whereas you're using [1..$], and
> later on in your argument parsing code you might introduce a bug like:
>
> auto userArgumentCount = args.length; // woops, you'll get 1 extra
> here due to app name.
It depends on what you're doing. If all you're doing is iterating over the
arguments once, then there's no reason to pop an element off. It's more code
and is slightly less efficient. However, if you're going to do more than that
with the arguments, then it may very well just be better to pop off the front
before doing anything else with them. Either approach could be better
depending on what else you're doing in your code.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list