update Changes
[gitmo/Moose.git] / lib / Moose / Manual / BestPractices.pod
CommitLineData
696cf5df 1=pod
2
f7435595 3=head1 NAME
4
5Moose::Manual::BestPractices - Get the most out of Moose
6
fd8a7262 7=head1 RECOMMENDATIONS
8
3bfacd05 9Moose has a lot of features, and there's definitely more than one way
10to do it. However, we think that picking a subset of these features
11and using them consistently makes everyone's life easier.
12
13Of course, as with any list of "best practices", these are really just
14opinions. Feel free to ignore us.
15
d67ce58f 16=head2 C<no Moose> and immutabilize
fd8a7262 17
18We recommend that you end your Moose class definitions by removing the
19Moose sugar and making your class immutable.
20
21 package Person;
22
23 use Moose;
24
25 # extends, roles, attributes, etc.
26
27 # methods
28
29 no Moose;
30
31 __PACKAGE__->meta->make_immutable;
32
33 1;
34
5481b153 35The C<no Moose> bit is simply good code hygiene, as it removes all the
36Moose keywords from your class's namespace. Once the class has been
37built, these keywords are not needed needed. The C<make_immutable>
38call allows Moose to speed up a lot of things, most notably object
dd8a021c 39construction. The trade-off is that you can no longer change the class
5481b153 40definition.
41
42A more generic way to unimport not only L<Moose>'s exports but also
43those from type libraries and other modules is to use
44L<namespace::clean> or L<namespace::autoclean>.
e62951ce 45
1ad2aa8e 46=head2 Never override C<new>
47
48Overriding C<new> is a very bad practice. Instead, you should use a
49C<BUILD> or C<BUILDARGS> methods to do the same thing. When you
50override C<new>, Moose can no longer inline a constructor when your
19320607 51class is immutabilized.
1ad2aa8e 52
46c52442 53There are two good reasons to override C<new>. One, you are writing a
54MooseX extension that provides its own L<Moose::Object> subclass
55I<and> a subclass of L<Moose::Meta::Method::Constructor> to inline the
56constructor. Two, you are subclassing a non-Moose parent.
1ad2aa8e 57
58If you know how to do that, you know when to ignore this best practice
59;)
60
d67ce58f 61=head2 Always call C<SUPER::BUILDARGS>
fd8a7262 62
3bfacd05 63If you override the C<BUILDARGS> method in your class, make sure to
64play nice and call C<SUPER::BUILDARGS> to handle cases you're not
0f62a437 65checking for explicitly.
3bfacd05 66
67The default C<BUILDARGS> method in L<Moose::Object> handles both a
68list and hashref of named parameters correctly, and also checks for a
69I<non-hashref> single argument.
70
1ad2aa8e 71=head2 Provide defaults whenever possible, otherwise use C<required>
3bfacd05 72
1ad2aa8e 73When your class provides defaults, this makes constructing new objects
74simpler. If you cannot provide a default, consider making the
75attribute C<required>.
76
77If you don't do either, an attribute can simply be left unset,
78increasing the complexity of your object, because it has more possible
79states that you or the user of your class must account for.
3bfacd05 80
d67ce58f 81=head2 Use C<builder> instead of C<default> most of the time
3bfacd05 82
83Builders can be inherited, they have explicit names, and they're just
84plain cleaner.
85
86However, I<do> use a default when the default is a non-reference,
87I<or> when the default is simply an empty reference of some sort.
88
89Also, keep your builder methods private.
fd8a7262 90
0c39debe 91=head2 Use C<lazy_build>
fd8a7262 92
3bfacd05 93Lazy is good, and often solves initialization ordering problems. It's
94also good for deferring work that may never have to be done. If you're
e691bebf 95going to be lazy, use C<lazy_build> to save yourself some typing and
3bfacd05 96standardize names.
fd8a7262 97
d67ce58f 98=head2 Consider keeping clearers and predicates private
fd8a7262 99
3bfacd05 100Does everyone I<really> need to be able to clear an attribute?
101Probably not. Don't expose this functionality outside your class
102by default.
b6477964 103
3bfacd05 104Predicates are less problematic, but there's no reason to make your
105public API bigger than it has to be.
fd8a7262 106
d67ce58f 107=head2 Default to read-only, and consider keeping writers private
fd8a7262 108
3bfacd05 109Making attributes mutable just means more complexity to account for in
110your program. The alternative to mutable state is to encourage users
111of your class to simply make new objects as needed.
fd8a7262 112
3bfacd05 113If you I<must> make an attribute read-write, consider making the
114writer a separate private method. Narrower APIs are easy to maintain,
115and mutable state is trouble.
b6477964 116
8a68781d 117In order to declare such attributes, provide a private C<writer>
118parameter:
119
120 has pizza => (
743e0199 121 is => 'ro',
122 isa => 'Pizza',
123 writer => '_pizza',
8a68781d 124 );
125
d67ce58f 126=head2 Think twice before changing an attribute's type in a subclass
541027c5 127
3bfacd05 128Down this path lies great confusion. If the attribute is an object
129itself, at least make sure that it has the same interface as the type
130of object in the parent class.
131
1ad2aa8e 132=head2 Don't use the C<initializer> feature
133
134Don't know what we're talking about? That's fine.
135
e4a134ed 136=head2 Use L<Moose::Meta::Attribute::Native> traits instead of C<auto_deref>
3bfacd05 137
dc747917 138The C<auto_deref> feature is a bit troublesome. Directly exposing a
3bfacd05 139complex attribute is ugly. Instead, consider using
e4a134ed 140L<Moose::Meta::Attribute::Native> traits to define an API that exposes only
141necessary pieces of functionality.
541027c5 142
053c63e0 143=head2 Always call C<inner> in the most specific subclass
144
145When using C<augment> and C<inner>, we recommend that you call
146C<inner> in the most specific subclass of your hierarchy. This makes
147it possible to subclass further and extend the hierarchy without
148changing the parents.
149
d67ce58f 150=head2 Namespace your types
541027c5 151
3bfacd05 152Use some sort of namespacing convention for type names. We recommend
7b307c3e 153something like "MyApp::Type::Foo".
154
155If you're intending to package your types up for re-use using
289ea8f2 156L<MooseX::Types> later, avoid using characters that are invalid in
157perl identifiers such as a space or period.
541027c5 158
1ad2aa8e 159=head2 Do not coerce Moose built-ins directly
160
161If you define a coercion for a Moose built-in like C<ArrayRef>, this
162will affect every application in the Perl interpreter that uses this
163type.
164
165 # very naughty!
166 coerce 'ArrayRef'
167 => from Str
ff51bdc6 168 => via { [ split /,/ ] };
1ad2aa8e 169
170Instead, create a subtype and coerce that:
171
e9be9f68 172 subtype 'My::ArrayRef' => as 'ArrayRef';
1ad2aa8e 173
e9be9f68 174 coerce 'My::ArrayRef'
1ad2aa8e 175 => from 'Str'
ff51bdc6 176 => via { [ split /,/ ] };
1ad2aa8e 177
178=head2 Do not coerce class names directly
179
180Just as with Moose built-in types, a class type is global for the
181entire interpreter. If you add a coercion for that class name, it can
182have magical side effects elsewhere:
183
184 # also very naughty!
185 coerce 'HTTP::Headers'
186 => from 'HashRef'
ff51bdc6 187 => via { HTTP::Headers->new( %{$_} ) };
1ad2aa8e 188
189Instead, we can create an "empty" subtype for the coercion:
190
e9be9f68 191 subtype 'My::HTTP::Headers' => as class_type('HTTP::Headers');
1ad2aa8e 192
e9be9f68 193 coerce 'My::HTTP::Headers'
1ad2aa8e 194 => from 'HashRef'
ff51bdc6 195 => via { HTTP::Headers->new( %{$_} ) };
1ad2aa8e 196
197=head2 Use coercion instead of unions
541027c5 198
3bfacd05 199Consider using a type coercion instead of a type union. This was
200covered at length in L<Moose::Manual::Types>.
201
d67ce58f 202=head2 Define all your types in one module
3bfacd05 203
204Define all your types and coercions in one module. This was also
205covered in L<Moose::Manual::Types>.
206
1ad2aa8e 207=head1 BENEFITS OF BEST PRACTICES
208
209Following these practices has a number of benefits.
210
211It helps ensure that your code will play nice with others, making it
212more reusable and easier to extend.
213
214Following an accepted set of idioms will make maintenance easier,
215especially when someone else has to maintain your code. It will also
216make it easier to get support from other Moose users, since your code
217will be easier to digest quickly.
218
219Some of these practices are designed to help Moose do the right thing,
220especially when it comes to immutabilization. This means your code
221will be faster when immutabilized.
222
223Many of these practices also help get the most out of meta
224programming. If you used an overridden C<new> to do type coercion by
225hand, rather than defining a real coercion, there is no introspectable
dab94063 226metadata. This sort of thing is particularly problematic for MooseX
1ad2aa8e 227extensions which rely on introspection to do the right thing.
228
3bfacd05 229=head1 AUTHOR
230
1ad2aa8e 231Yuval (nothingmuch) Kogman
232
3bfacd05 233Dave Rolsky E<lt>autarch@urth.orgE<gt>
234
235=head1 COPYRIGHT AND LICENSE
236
2840a3b2 237Copyright 2009 by Infinity Interactive, Inc.
3bfacd05 238
239L<http://www.iinteractive.com>
240
241This library is free software; you can redistribute it and/or modify
242it under the same terms as Perl itself.
243
244=cut