5 Moose::Cookbook::Style - The latest in trendy Moose cuisine
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>:
128 use MooseX::Types::Moose qw(Object);
131 -declare => [qw(Person)],
134 subtype Person() => ( # note parenthesis, "Person" is a function, not a string
135 as Object, # MooseX::Types::Moose exported it
136 where { $_->can("name") },
139 =head3 Coerce in a subtype
141 Likewise use fully qualified subtypes of other types for defining coercions, so
142 that they won't affect unrelated code, causing action at a distance.
144 This is important because the type registry is global, kind of like the symbol
147 This means that code like:
152 from Str => via { [ split /,/ ] },
155 Will add a coercion to B<all> attributes like:
164 =head1 Clean up your package
166 Use C<namespace::clean> or C<no Moose> to remove the sugar exports.
168 This will make sure the sugar isn't accidentally called as methods on your objects.
174 will return true, even though C<has> is not a method.
176 =head1 Accept no substitutes
178 By substitutes I mean hacks instead of "proper" solutions.
180 When you have a tricky requirement, refrain from abusing Moose or MooseX:: or
181 whatever it is you are using.
183 Instead, drop by IRC and discuss it. Most of the time a crazy idea can either
184 be simplified, or it will spawn a clean, reliable feature to whatever package
187 This will improve your code and also share the benefit with others.
191 Yuval (nothingmuch) Kogman
193 =head1 COPYRIGHT AND LICENSE
195 Copyright 2006-2008 by Infinity Interactive, Inc.
197 L<http://www.iinteractive.com>
199 This library is free software; you can redistribute it and/or modify
200 it under the same terms as Perl itself.