Commit | Line | Data |
fd8a7262 |
1 | =head1 RECOMMENDATIONS |
2 | |
3bfacd05 |
3 | Moose has a lot of features, and there's definitely more than one way |
4 | to do it. However, we think that picking a subset of these features |
5 | and using them consistently makes everyone's life easier. |
6 | |
7 | Of course, as with any list of "best practices", these are really just |
8 | opinions. Feel free to ignore us. |
9 | |
10 | =head2 "No Moose" and Immutabilize |
fd8a7262 |
11 | |
12 | We recommend that you end your Moose class definitions by removing the |
13 | Moose sugar and making your class immutable. |
14 | |
15 | package Person; |
16 | |
17 | use Moose; |
18 | |
19 | # extends, roles, attributes, etc. |
20 | |
21 | # methods |
22 | |
23 | no Moose; |
24 | |
25 | __PACKAGE__->meta->make_immutable; |
26 | |
27 | 1; |
28 | |
3bfacd05 |
29 | The "no Moose" bit is simply good code hygiene, and making classes |
30 | immutable speeds up a lot of things, most notably object construction. |
31 | |
fd8a7262 |
32 | =head2 Always call SUPER::BUILDARGS |
33 | |
3bfacd05 |
34 | If you override the C<BUILDARGS> method in your class, make sure to |
35 | play nice and call C<SUPER::BUILDARGS> to handle cases you're not |
36 | checking for explicitly.q |
37 | |
38 | The default C<BUILDARGS> method in L<Moose::Object> handles both a |
39 | list and hashref of named parameters correctly, and also checks for a |
40 | I<non-hashref> single argument. |
41 | |
42 | =head2 Don't Use the initializer Feature |
43 | |
44 | Don't know what we're talking about? That's fine. |
45 | |
46 | =head2 Use builder Instead of default Most of the Time. |
47 | |
48 | Builders can be inherited, they have explicit names, and they're just |
49 | plain cleaner. |
50 | |
51 | However, I<do> use a default when the default is a non-reference, |
52 | I<or> when the default is simply an empty reference of some sort. |
53 | |
54 | Also, keep your builder methods private. |
fd8a7262 |
55 | |
3bfacd05 |
56 | =head2 Use lazy_build |
fd8a7262 |
57 | |
3bfacd05 |
58 | Lazy is good, and often solves initialization ordering problems. It's |
59 | also good for deferring work that may never have to be done. If you're |
60 | going to be lazy, use I<lazy_build> to save yourself some typing and |
61 | standardize names. |
fd8a7262 |
62 | |
3bfacd05 |
63 | =head2 Consider Keeping clearers & predicates Private |
fd8a7262 |
64 | |
3bfacd05 |
65 | Does everyone I<really> need to be able to clear an attribute? |
66 | Probably not. Don't expose this functionality outside your class |
67 | by default. |
b6477964 |
68 | |
3bfacd05 |
69 | Predicates are less problematic, but there's no reason to make your |
70 | public API bigger than it has to be. |
fd8a7262 |
71 | |
3bfacd05 |
72 | =head2 Default to read-only, and Consider Keeping writers Private |
fd8a7262 |
73 | |
3bfacd05 |
74 | Making attributes mutable just means more complexity to account for in |
75 | your program. The alternative to mutable state is to encourage users |
76 | of your class to simply make new objects as needed. |
fd8a7262 |
77 | |
3bfacd05 |
78 | If you I<must> make an attribute read-write, consider making the |
79 | writer a separate private method. Narrower APIs are easy to maintain, |
80 | and mutable state is trouble. |
b6477964 |
81 | |
541027c5 |
82 | =head2 Think Twice Before Changing an Attribute's Type in a Subclass |
83 | |
3bfacd05 |
84 | Down this path lies great confusion. If the attribute is an object |
85 | itself, at least make sure that it has the same interface as the type |
86 | of object in the parent class. |
87 | |
88 | =head2 Use MooseX::AttributeHelpers Instead of auto_deref |
89 | |
90 | The C<auto_deref> feature is a big troublesome. Directly exposing a |
91 | complex attribute is ugly. Instead, consider using |
92 | C<MooseX::AttributeHelpers> to define an API that exposes those pieces |
93 | of functionality that need exposing. Then you can expose just the |
94 | functionality that you want. |
541027c5 |
95 | |
96 | =head2 Namespace Your Types |
97 | |
3bfacd05 |
98 | Use some sort of namespacing convention for type names. We recommend |
99 | something like "MyApp.Type.Foo". I<Never> use "::" as the namespace |
100 | separator, since that overlaps with actual class names. |
541027c5 |
101 | |
102 | =head2 Coercion Instead of Unions |
103 | |
3bfacd05 |
104 | Consider using a type coercion instead of a type union. This was |
105 | covered at length in L<Moose::Manual::Types>. |
106 | |
541027c5 |
107 | =head2 Define All Your Types in One Module |
3bfacd05 |
108 | |
109 | Define all your types and coercions in one module. This was also |
110 | covered in L<Moose::Manual::Types>. |
111 | |
112 | =head1 AUTHOR |
113 | |
114 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
115 | |
116 | =head1 COPYRIGHT AND LICENSE |
117 | |
118 | Copyright 2008 by Infinity Interactive, Inc. |
119 | |
120 | L<http://www.iinteractive.com> |
121 | |
122 | This library is free software; you can redistribute it and/or modify |
123 | it under the same terms as Perl itself. |
124 | |
125 | =cut |