Created
January 10, 2014 10:30
-
-
Save blair55/8349741 to your computer and use it in GitHub Desktop.
Why State is Dangerous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Number | |
| { | |
| private int _n; | |
| public Number(int n) | |
| { | |
| _n = n; | |
| } | |
| public int Exponential() | |
| { | |
| return Convert.ToInt32(Math.Round(Math.Exp(_n), 0)); | |
| } | |
| public void BadSetter(int n) | |
| { | |
| _n = n; | |
| } | |
| public static void SettingIsReallyBad(Number n) | |
| { | |
| n.BadSetter(20); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var n = new Number(5); | |
| // uncomment the following lines and see how | |
| // the result of the same method call changes! | |
| // 1. here you have some idea that a value might change | |
| // n.BadSetter(10); | |
| // 2. here there is no way of knowing that state has changed | |
| // Number.SettingIsReallyBad(n); | |
| var result = n.Exponential(); | |
| Console.WriteLine(result); | |
| Console.ReadLine(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment