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