Skip to content

Commit 4c15a54

Browse files
author
Ben Christel
committed
Add 'Primitive Obsession' smell and refactoring
1 parent a7881cd commit 4c15a54

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Replace Primitive With Object
2+
3+
## Example
4+
5+
### Before
6+
7+
```ruby
8+
def protocol(url_string)
9+
url_string.match(/^([^:]+):/)[1]
10+
end
11+
```
12+
13+
### After
14+
15+
```ruby
16+
def protocol(url)
17+
url.scheme
18+
end
19+
20+
# ... elsewhere ...
21+
22+
url = URI.parse(url_string)
23+
```

docs/smells/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Fowler's book, _Refactoring: Improving the Design of Existing Code_.
4848

4949
- [**Null Check**](null-check.md). Fix with [Introduce Null Object](../refactorings/introduce-null-object.md) or [Introduce Continuation [BC]](../refactorings/introduce-continuation.md).
5050
- [**Feature Envy.**](feature-envy.md) Fix with [Move Method](../refactorings/move-method.md), possibly preceded by [Extract Method](../refactorings/extract-method.md).
51-
- [**Primitive Obsession.**](http://wiki.c2.com/?PrimitiveObsession) Fix using [Replace Primitive with Object](). See also [Avoid Hashy Syntax In Ruby](http://wiki.c2.com/?AvoidHashySyntaxInRuby) for a language-specific refactoring technique.
51+
- [**Primitive Obsession.**](primitive-obsession.md) Fix using [Replace Primitive with Object](../refactorings/replace-primitive-with-object.md). See also [Avoid Hashy Syntax In Ruby](http://wiki.c2.com/?AvoidHashySyntaxInRuby) for a language-specific refactoring technique.
5252
- [**Multiple Responsibilities.**](http://wiki.c2.com/?OneResponsibilityRule) See also [God Class](http://wiki.c2.com/?GodClass). TODO: how to fix?
5353
- [**Switch on Type.**](http://wiki.c2.com/?SwitchStatementsSmell) Fix using [Replace Conditional with Polymorphism]().
5454
- [**Middleman.**]() Fix using [Inline Method](https://refactoring.com/catalog/inlineFunction.html).

docs/smells/primitive-obsession.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Primitive Obsession
2+
3+
Code manipulates strings and hashes directly instead of
4+
using domain objects.
5+
6+
## What It Looks Like
7+
8+
```ruby
9+
protocol = url.match(/^([^:]+):/)[1]
10+
```
11+
12+
```ruby
13+
username = user["name"]
14+
```
15+
16+
## Why It Hurts
17+
18+
- Since you can't define new methods on primitive data types
19+
(at least not without causing a great deal of confusion),
20+
logic for manipulating them gets scattered around the
21+
codebase.
22+
- Your code becomes coupled to the underlying
23+
structure of the data, making it harder to test and reason
24+
about.
25+
- You have to think about error cases more often. For
26+
example, when using a string to represent a URL, you have
27+
to think about the case where the string is not a
28+
well-formed URL every time you do something with it.
29+
30+
## How To Fix It
31+
32+
[Replace Primitive With Object](../refactorings/replace-primitive-with-object.md).

0 commit comments

Comments
 (0)