5 Moose::Cookbook::Style - How to cook Moose with style
9 Please annotate all bad examples with comments so that they won't be copied by accodent
13 =head1 Benefits of Good Style
15 Good Moose style, as defined by this document, helps ensure your code has the
16 following desirable properties:
20 =item Play well with others
22 Your code will be more reusable and easier to extend.
24 =item Ease maintenance
26 The code will be easier to understand because it follows an accepted set of
27 conventions and idioms.
29 This will help others maintaining your code, and also help you to get support
32 =item Help Moose generate better code
34 By using the most appropriate features, the generated code will be safer and
37 =item Benefit from meta programming
39 Code that operates on the metaclass will benefit from clean meta definitions.
41 If you are manually converting argument types with C<around 'new'> there is no
42 meta data explaining your intention. If on the other hand you use coercions,
43 there is introspectable meta data that makes this clear.
45 This means that e.g. MooseX extensions that work by introspecting your class
46 will be able to do the right thing more often, because they don't need to
51 =head1 Don't change C<new>
53 It is generally considered bad style to override L<Moose::Object/new> for a
56 The first reason is consistency. Subclasses of your class and code
57 instantiating your class would be simpler if your constructor works closer to
60 The second reason is performance. By calling C<make_immutable> on your metaclass:
62 __PACKAGE__->meta->make_immutable;
64 And opting out of any class definition changes from that point on, you allow
65 Moose to create more efficient versions of certain generic methods. Moose will
66 generate a tight, optimal C<new> for you, based on the minimal set of features
69 Moose provides many features that allow you to do common object construction
70 tasks at the right level of abstraction.
72 When attributes have the ability to provide the necessary functionality, use
73 that. If that isn't sufficient, L<Moose::Object> has numerous features you can
74 use at construction time.
76 =head2 Use C<BUILD> instead of custom initialization or overriding C<new>
78 Instead of changing C<new>, do initialization in C<BUILD>.
80 The construction parameters are passed in, so you don't need to replicate
81 C<BUILDARGS>, and since C<BUILD> is called for each superclass that defines it,
82 you will never forget to invoke your initializers if you extend them.
84 =head2 Use C<default>, C<builder> or C<lazy_build>
86 To initialize attributes there is a plethora of methods preferable to assigning
87 the value at initialization time.
89 If you want to translate parameter data, use coercions.
91 If you want to ensure a parameter can't be overridden by the constructor, set
92 the C<init_arg> to C<undef> instead of overwriting it in C<BUILD>.
94 =head2 Use C<BUILDARGS> to alter C<@_> processing
96 If you need to change the way L<@_> is processed, use C<BUILDARGS>, instead of
97 wrapping C<new>. This ensures the behavior is subclassible, it keeps this logic
98 independent of the other aspects of construction, and can be made efficient
99 using C<make_immutable>.
101 =head1 Don't pollute the global type registry
103 =head2 Use fully qualified type names for your own data
105 L<MooseX::Types> provides a convenient method to do this.
113 where { $_->can("name") },
116 Then the global name C<Person> is registered, and this could conflict with
117 other bad usage of the sort.
119 Instead, prefix type name with your project namespace, or class name:
121 subtype 'My::Foo::Person' => (
123 where { $_->can("name") },
126 Or with L<MooseX::Types>:
129 -declare => [qw(Person)],
132 subtype Person() => ( # note parenthesis, "Person" is a function, not a string
134 where { $_->can("name") },
137 =head3 Coerce in a subtype
139 Likewise use fully qualified subtypes of other types for defining coercions, so
140 that they won't affect unrelated code, causing action at a distance.
142 This is important because the type registry is global, kind of like the symbol
145 This means that code like:
150 from Str => via { [ split /,/ ] },
153 Will add a coercion to B<all> attributes like: