Skip to content

Commit 2e9862d

Browse files
authored
Merge pull request #85 from moosetechnology/pro-tips
Init pro tips page
2 parents dd375f6 + 7072f0a commit 2e9862d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

astro.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ export default defineConfig({
142142
url: "https://alesshosry.github.io",
143143
picture: "https://alesshosry.github.io/assets/myImage2.jpeg"
144144
},
145+
CyrilFerlicot: {
146+
name: "Cyril Ferlicot-Delbecque",
147+
title: "Research engineer at Inria",
148+
url: "https://ferlicot.fr",
149+
picture: "https://avatars.githubusercontent.com/u/9519971"
150+
},
145151
},
146152
}),
147153
starlightSidebarTopics(
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)