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