X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FManual%2FBestPractices.pod;h=5840910314222204228d2c02a7c95750b5cdc623;hb=49d85bb58216f373e0398f0fd47780db82d30e26;hp=1db01dee610f9d729cf07e5b6caf5af546d8bfe6;hpb=e691bebfeb02a6b789af3a7d4441d9f8d44b5f3e;p=gitmo%2FMoose.git diff --git a/lib/Moose/Manual/BestPractices.pod b/lib/Moose/Manual/BestPractices.pod index 1db01de..5840910 100644 --- a/lib/Moose/Manual/BestPractices.pod +++ b/lib/Moose/Manual/BestPractices.pod @@ -1,8 +1,10 @@ -=pod +package Moose::Manual::BestPractices; + +# ABSTRACT: Get the most out of Moose -=head1 NAME +__END__ -Moose::Manual::BestPractices - Get the most out of Moose +=pod =head1 RECOMMENDATIONS @@ -13,35 +15,32 @@ and using them consistently makes everyone's life easier. Of course, as with any list of "best practices", these are really just opinions. Feel free to ignore us. -=head2 C and immutabilize +=head2 C and immutabilize -We recommend that you end your Moose class definitions by removing the -Moose sugar and making your class immutable. +We recommend that you remove the Moose sugar and end your Moose class +definitions by making your class immutable. package Person; use Moose; + use namespace::autoclean; # extends, roles, attributes, etc. # methods - no Moose; - __PACKAGE__->meta->make_immutable; 1; -The C bit is simply good code hygiene, as it removes all the -Moose keywords from your class's namespace. Once the class has been -built, these keywords are not needed needed. The C -call allows Moose to speed up a lot of things, most notably object -construction. The tradeoff is that you can no longer change the class -definition. +The C bit is simply good code hygiene, as it removes +imported symbols from you class's namespace at the end of your package's +compile cycle, including Moose keywords. Once the class has been +built, these keywords are not needed. -A more generic way to unimport not only L's exports but also -those from type libraries and other modules is to use -L or L. +The C call allows Moose to speed up a lot of things, most +notably object construction. The trade-off is that you can no longer change +the class definition. =head2 Never override C @@ -58,11 +57,10 @@ constructor. Two, you are subclassing a non-Moose parent. If you know how to do that, you know when to ignore this best practice ;) -=head2 Always call C +=head2 Always call the original/parent C -If you override the C method in your class, make sure to -play nice and call C to handle cases you're not -checking for explicitly. +If you C the C method in your class, make sure to play +nice and call C to handle cases you're not checking for explicitly. The default C method in L handles both a list and hashref of named parameters correctly, and also checks for a @@ -88,12 +86,11 @@ I when the default is simply an empty reference of some sort. Also, keep your builder methods private. -=head2 Use C +=head2 Be C -Lazy is good, and often solves initialization ordering problems. It's -also good for deferring work that may never have to be done. If you're -going to be lazy, use C to save yourself some typing and -standardize names. +Lazy is good, and often solves initialization ordering problems. It's also +good for deferring work that may never have to be done. Make your attributes +C unless they're C or have trivial defaults. =head2 Consider keeping clearers and predicates private @@ -133,13 +130,12 @@ of object in the parent class. Don't know what we're talking about? That's fine. -=head2 Use L instead of C +=head2 Use L traits instead of C -The C feature is a bit troublesome. Directly exposing a -complex attribute is ugly. Instead, consider using -L to define an API that exposes those pieces -of functionality that need exposing. Then you can expose just the -functionality that you want. +The C feature is a bit troublesome. Directly exposing a complex +attribute is ugly. Instead, consider using L +traits to define an API that only exposes the necessary pieces of +functionality. =head2 Always call C in the most specific subclass @@ -150,12 +146,8 @@ changing the parents. =head2 Namespace your types -Use some sort of namespacing convention for type names. We recommend -something like "MyApp::Type::Foo". - -If you're intending to package your types up for re-use using -L later, avoid using characters that are invalid in -perl identifiers such as a space or period. +Use some sort of namespacing convention for type names. We recommend something +like "MyApp::Type::Foo". We also recommend considering L. =head2 Do not coerce Moose built-ins directly @@ -198,7 +190,7 @@ Instead, we can create an "empty" subtype for the coercion: =head2 Use coercion instead of unions Consider using a type coercion instead of a type union. This was -covered at length in L. +covered in L. =head2 Define all your types in one module @@ -227,19 +219,4 @@ hand, rather than defining a real coercion, there is no introspectable metadata. This sort of thing is particularly problematic for MooseX extensions which rely on introspection to do the right thing. -=head1 AUTHOR - -Yuval (nothingmuch) Kogman - -Dave Rolsky Eautarch@urth.orgE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2009 by Infinity Interactive, Inc. - -L - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - =cut