background preloader

Programming

Facebook Twitter

Software

Msdn. Controls. Regex. Ibm. Remoting. Md5. Arrays. Mr. Neighborly's Humble Little Ruby Book. Immutable types: understand their benefits and use them. There is a powerful and simple concept in programming that I think is really underused: Immutability Basically, an object is immutable if its state doesn’t change once the object has been created. Consequently, a class is immutable if its instances are immutable. There is one killer argument for using immutable objects: It dramatically simplifies concurrent programming. Think about it, why does writing proper multithreaded programming is a hard task? Because it is hard to synchronize threads accesses to resources (objects or others OS things). Of course I simplify here so let’s dig a bit. A famous immutable class There is one famous immutable class: System.String. String str = “foofoo”; str.Replace(“foo”, “FOO”); …where we need to write instead: str = str.Replace(“foo”, “FOO”); Of course, doing so comes at the cost of creating multiple string objects in memory when doing some intensive string computation.

So why .NET engineers decided that string should be immutable? String str1 = “foofoo”; Rails API with the AJAX flavor. Algorithms and Data Structures. Algorithm: a process or set of rules used for calculation or problem-solving, esp. with a computer.Program: a series of coded instructions to control the operation of a computer or other machine. Example Problem: Find the greatest common divisor (GCD) of two integers, m and n.Euclid's Algorithm: while m is greater than zero: If n is greater than m, swap m and n. Subtract n from m. n is the GCD Program (in C): Correctness Why do we believe that this algorithm devised thousands of years ago, is correct? Given m>0 and n>0, let g = gcd(m,n). So the algorithm is correct, provided that it terminates. Termination At the start of each iteration of the loop, either n>m or m≥n. So the algorithm does terminate. Testing Having proved the algorithm to be correct, one might argue that there is no need to test it.

Debugging code be inserted to print the values of m and n at the end of each iteration to confirm that they behave as expected. Complexity Time If m=n, there is just one iteration; this is the best-case.