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