Skip to content

Commit da179df

Browse files
committed
Revive ghost from #43
1 parent 66ce03a commit da179df

File tree

7 files changed

+108
-3
lines changed

7 files changed

+108
-3
lines changed

anti_patterns/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Anti-patterns
22

3-
TODO: add description/explanation
3+
An [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern) is a solution to a "recurring problem that is usually ineffective and risks being highly counterproductive". Just as valuable as knowing how to solve a problem, is knowing how _not_ to solve it. Anti-patterns give us great counter-examples to consider relative to design patterns. Anti-patterns are not confined to code. A process can be an anti-pattern.

functional/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Functional Usage of Rust
2+
3+
Rust is an imperative language, but it follows many functional programming paradigms. One of the biggest hurdles to understanding functional programs when coming from an imperative background is the shift in thinking. Imperative programs describe __how__ to do something, whereas declarative programs describe __what__ to do. Let's sum the numbers from 1 to 10 to show this.
4+
5+
## Imperative
6+
7+
```rust
8+
let mut sum = 0;
9+
for i in 1..11 {
10+
sum += i;
11+
}
12+
println!("{}", sum);
13+
```
14+
15+
With imperative programs, we have to play compiler to see what is happening. Here, we start with a `sum` of `0`. Next, we iterate through the range from 1 to 10. Each time through the loop, we add the corresponding value in the range. Then we print it out.
16+
17+
| `i` | `sum` |
18+
| --- | --- |
19+
| 1 | 1 |
20+
| 2 | 3 |
21+
| 3 | 6 |
22+
| 4 | 10 |
23+
| 5 | 15 |
24+
| 6 | 21 |
25+
| 7 | 28 |
26+
| 8 | 36 |
27+
| 9 | 45 |
28+
| 10 | 55 |
29+
30+
This is how most of us start out programming. We learn that a program is a set of steps.
31+
32+
## Declarative
33+
34+
```rust
35+
println!("{}", (1..11).fold(0, |a, b| a + b));
36+
```
37+
38+
Whoa! This is really different! What's going on here? Remember that with declarative programs we are describing __what__ to do, rather than __how__ to do it. `fold` is a function that [composes](https://en.wikipedia.org/wiki/Function_composition) functions. The name is a convention from Haskell.
39+
40+
Here, we are composing functions of addition (this closure: `|a, b| a + b)`) with a range from 1 to 10. The `0` is the starting point, so `a` is `0` at first. `b` is the first element of the range, `1`. `0 + 1 = 1` is the result. So now we `fold` again, with `a = 1`, `b = 2` and so `1 + 2 = 3` is the next result. This process continues until we get to the last element in the range, `10`.
41+
42+
| `a` | `b` | result |
43+
| --- | --- | --- |
44+
| 0 | 1 | 1 |
45+
| 1 | 2 | 3 |
46+
| 3 | 3 | 6 |
47+
| 6 | 4 | 10 |
48+
| 10 | 5 | 15 |
49+
| 15 | 6 | 21 |
50+
| 21 | 7 | 28 |
51+
| 28 | 8 | 36 |
52+
| 36 | 9 | 45 |
53+
| 45 | 10 | 55 |

idioms/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# Idioms
22

3-
TODO: add description/explanation
3+
[Idioms](https://en.wikipedia.org/wiki/Programming_idiom) are commonly used styles and patterns largely agreed upon by a community. They are guidelines. Writing idiomatic code allows other developers to understand what is happening because they are familiar with the form that it has.
4+
5+
The computer understands the machine code that is generated by the compiler. The language is therefore mostly beneficial to the developer. Since we have this abstraction layer, why not put it to good use and make it simple? Remember the KISS principle.
6+
7+
> Code is there for humans, not computers, to understand.

intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Introduction
22

33
## Design patterns
4+
When developing programs, we have to solve many problems. A program can be viewed as a solution to a problem. It can also be viewed as a collection of solutions to many different problems. All of these solutions work together to solve a bigger problem.
45

56
What are design patterns? What are idioms? Anti-patterns.
67

78
## Design patterns in Rust
89

910
Why Rust is a bit special - functional elements, type system - borrow checker
11+
There are many problems that share the same form. While the details are different, since they have the same form they can be solved using the same methods. [Design patterns](patterns/README.md) are methods to solve common problems when writing software. [Anti-patterns](anti_patterns/README.md) are methods to solve these same common problems. However, while design patterns give us benefits, anti-patterns create more problems. There are some problems that we don't need to solve because [Rust rocks](rust_rocks.md)! [Idioms](idioms/README.md) are guidelines to follow when coding. They are social norms of the community. You can break them, but if you do you should have a good reason for it. [Refactoring](refactoring/README.md) is the process by which you convert code that works, but is hard to understand, into code that works and is easy to understand.

patterns/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
# Design Patterns
22

3-
TODO: add description/explanation
3+
[Design patterns](https://en.wikipedia.org/wiki/Software_design_pattern) are "general reusable solutions to a commonly occurring problem within a given context in software design". Design patterns are a great way to describe some of the culture and 'tribal knowledge' of programming in a language. Design patterns are very language-specific - what is a pattern in one language may be unnecessary in another due to a language feature, or impossible to express due to a missing feature.
4+
5+
If overused, design patterns can add unnecessary complexity to programs. However, they are a great way to share intermediate and advanced level knowledge about a programming language.
6+
7+
# Design patterns in Rust
8+
9+
Rust has many very unique features. These features give us great benefit by removing whole classes of problems. For more about this, read why [Rust rocks](/rust_rocks.md)! There are also some patterns that are unique to Rust.
10+
11+
- [Builder](patterns/builder.md)
12+
13+
TODO: Add links to all the pattern files.
14+
15+
# YAGNI
16+
17+
If you're not familiar with it, YAGNI is an acronym that stands for You Aren't Going to Need It. It's an important software design principle to apply as you write code.
18+
19+
> The best code I ever wrote was code I never wrote.
20+
If we apply YAGNI to design patterns, we see that the features of Rust allow us to throw out many patterns. For instance, there is no need for the [strategy pattern](https://en.wikipedia.org/wiki/Strategy_pattern) in Rust because we can just use [traits](https://doc.rust-lang.org/book/traits.html).
21+
22+
TODO: Maybe include some code to illustrate the traits.

refactoring/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Refactoring
2+
3+
Refactoring is very important in relation to these topics. Just as important as the other topics covered here, is how to take good code and turn it into great code.
4+
5+
We can use [design patterns](patterns/README.md) to DRY up code and generalize abstractions. We must avoid [anti-patterns](anti_patterns/README.md) while we do this. While they may be tempting to employ, their costs outweigh their benefits.
6+
7+
> Shortcuts make for long days.
8+
We can also use [idioms](idioms/README.md) to structure our code in a way that is understandable.
9+
10+
## Tests
11+
12+
Tests are of vital importance during refactoring.
13+
14+
## Small changes

rust_rocks.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Rust Rocks!
2+
3+
Consider this the unofficial Rust sales pitch.
4+
5+
# Types
6+
7+
Rust has strong static types. This can be very different than what you are used to if you are coming from a loosely-typed language. Don't worry, though. Once you get used to them, you'll find the types actually make your life easier. Why? Because you are making implicit assumptions explicit.
8+
9+
TODO: Show an example that illustrates the importance of data types.
10+
11+
# Ownership
12+
13+
Ownership is arguably the central concept of Rust. The ownership system is __the__ reason for the safety guarantees in Rust.

0 commit comments

Comments
 (0)