|  | 
|  | 1 | +--- | 
|  | 2 | +authors: | 
|  | 3 | +- CyrilFerlicot | 
|  | 4 | +title: "Pro tips" | 
|  | 5 | +subtitle: "List of developer knowledge to master Moose" | 
|  | 6 | +--- | 
|  | 7 | + | 
|  | 8 | +Moose is a big platform and hoards a lot of secrets to help developers work with it. The goal of this page is to discribe some of this knowledge.  | 
|  | 9 | + | 
|  | 10 | +<!-- TOC --> | 
|  | 11 | + | 
|  | 12 | +- [Attributes and Cache](#attributes-and-cache) | 
|  | 13 | +  - [Attributes](#attributes) | 
|  | 14 | +  - [Caches](#caches) | 
|  | 15 | + | 
|  | 16 | +<!-- /TOC --> | 
|  | 17 | + | 
|  | 18 | +## Attributes and Cache | 
|  | 19 | + | 
|  | 20 | +Moose models can be really complexe and it happens a lot that we need a specific information that are not planned in the variables of our objects. In order to make it easier to save datas, we have two systems: | 
|  | 21 | +- attributes: allow to save some info that should never be flushed | 
|  | 22 | +- caches: allow to cache some info for performance reason | 
|  | 23 | + | 
|  | 24 | +All subclasses of `MooseObject` are comming with those two mecanisms. | 
|  | 25 | + | 
|  | 26 | +> In Moose 13 the names are "Attribute" and "Cache". We are planning to rename "Attribute" into "Property" in Moose 14, but we cannot do it now because the name "property" was used for another concept. | 
|  | 27 | +
 | 
|  | 28 | +### Attributes | 
|  | 29 | + | 
|  | 30 | +The typical usecase of attributes is to save information we cannot compute in Moose. For example, if you have a tool producing metrics and you want to manipulate them in your moose model you could implement it like this: | 
|  | 31 | + | 
|  | 32 | +```smalltalk | 
|  | 33 | +myMetric | 
|  | 34 | +	^ self attributeAt: #myMetric ifAbsent: [  self notExistentMetricValue ] | 
|  | 35 | +``` | 
|  | 36 | + | 
|  | 37 | +and | 
|  | 38 | + | 
|  | 39 | +```smalltalk | 
|  | 40 | +myMetric: aValue | 
|  | 41 | +    ^ self attributeAt: #myMetric put: aValue | 
|  | 42 | +``` | 
|  | 43 | + | 
|  | 44 | +The info added to attributes are not flushable.  | 
|  | 45 | +If you declare new Moose properties like we did here, be careful to not add the `<derived>` pragma in order to export the value of this property in JSON files: | 
|  | 46 | + | 
|  | 47 | +```smalltalk | 
|  | 48 | +myMetric | 
|  | 49 | +	<FMProperty: #myMetric type: #Number> | 
|  | 50 | +	<FMComment: 'Description'> | 
|  | 51 | +
 | 
|  | 52 | +	^ self attributeAt: #myMetric ifAbsent: [  self notExistentMetricValue ] | 
|  | 53 | +``` | 
|  | 54 | + | 
|  | 55 | +### Caches | 
|  | 56 | + | 
|  | 57 | +The typical usecase of caches are to speed up some features by caching their values. | 
|  | 58 | + | 
|  | 59 | +For example: | 
|  | 60 | + | 
|  | 61 | +```smalltalk | 
|  | 62 | +numberOfMethods | 
|  | 63 | +	^ self cacheAt: #numberOfMethods ifAbsentPut: [ self types inject: 0 into: [ :sum :each | sum + each numberOfMethods ] ] | 
|  | 64 | +``` | 
|  | 65 | + and  | 
|  | 66 | + | 
|  | 67 | +```smalltalk | 
|  | 68 | +numberOfMethods: aNumber | 
|  | 69 | +	self cacheAt: #numberOfMethods put: aNumber | 
|  | 70 | +``` | 
|  | 71 | + | 
|  | 72 | +Be careful to not save info you cannot recompute in this cache because it can be flushed by calling `flush` on the instance of your moose object. | 
|  | 73 | +Also, if you declare a property, don't forget to add the `<derived>` pragma to not export this value that can be recomputed. | 
|  | 74 | + | 
|  | 75 | +```smalltalk | 
|  | 76 | +numberOfMethods | 
|  | 77 | +
 | 
|  | 78 | +	<FMProperty: #numberOfMethods type: #Number> | 
|  | 79 | +	<FMComment: 'The number of methods in a package'> | 
|  | 80 | +	<derived> | 
|  | 81 | +	^ self cacheAt: #numberOfMethods ifAbsentPut: [ self types inject: 0 into: [ :sum :each | sum + each numberOfMethods ] ] | 
|  | 82 | +``` | 
0 commit comments