in reply to what is the meaning of ();

In this case, it's just a useless use of (). You could just do my @incdirs;, and accomplish the same thing (my theory is that it's common for new perl programmers to add the "= ()" when declaring an empty array). When you want to empty an existing array, it can be useful to do @incdirs = ();, without the my.

Replies are listed 'Best First'.
Re^2: what is the meaning of ();
by MidLifeXis (Monsignor) on Jul 26, 2008 at 15:09 UTC

    It could also be cargo-cultism. I have seen coding style documents that require explicitly initializing all variables. No defaults allowed.

    Update: Bah. Rethinking this one. I understand and (strangly for me, normally opinionated) agree with both sides (could be cargo cult and initializing variable, even to the default, is good) on this one. I need to do some introspection on this to see if they are mutually exclusive. I am not certain that they are.

    --MidLifeXis

      Are you saying that following coding style documents is cargo-cult programming? I tend to think that one should follow coding guidelines, no matter how stupid. If they suck, change the guidelines or change your job.

      I'm sure you meant that it could be cargo-cult OR it could be adherence to a 'no defaults' style guideline/preference. The only way to know for sure would be to ask the original programmer why he wrote the code that way.

      Consider the following code:

      my $foo = undef;

      The initialization to undef totally unnecessary, but it does communicate an idea: $foo really should be undef, it's not just an oversight.

      While I don't use either unnecessary initialization, I don't see them as harmful.

      Despite my personal preferences, I can see why someone would choose to explicitly initialize ALL their variables. It's not a bad habit to be in.

      I guess the main thing I am trying to say is, don't be in too much of a rush to call something cargo-cult. What we have here could be CC code or it could be the result of a decision based on some painful lessons.


      TGI says moo

        No, I was saying that it could be a rule carried over from another language without judging the appropriateness of the rule in the new language.

        If the rule, or use, or meme has a valid reason behind it, then it is not cargo-cult.

        --MidLifeXis

        Programming should follow some rule-of-thumb, and explicitly initializing variable is one of them (no matter what languages we use). For expert (and to ourselves), it is good as reminder. For beginner, it is good for learning. Let the(good) compiler do the optimization.

        "The giving hands is better than the one accepting" - Muslim's cleric
      IMO it's always a good idea to initialise your variables, even if you initialise them to the same value as what the language would have done in the first place. It's a good idea because it teaches you good habits for when you use a language which doesn't have sensible defaults, such as C.