The content of this post has been moved here. Please check this link to view the contents.
Sorry for the inconvenience.
Thanks!
All Day, I Dream about Codes
Error handling is always a nightmare for every programmer. Because, testing you application in your development lab and exposing the application to a real-world lies in two extremities. The very reason where everyone go for alpha/beta and finally the RC release. Hey…Hey…I’m not going into core-concepts.
Recently, I’s re-optimizing a 6-month old c#-code (written in .NET 2.0) to .NET 3.5. Re-optimizing the code is a tough task, specially when the code was written by someone else. It deals with studying the code, understanding the logic and also good, if you can preview the test-cases encountered while re-designing.
When digging deep into the logic, I came across code-blocks where, even if you catch an Exception, you can’t continue because that might cause an abnormal exception at some other point. Huh! and I’s forced to kill the application at that point, using one-line code.
1: 2: Environment.Exit(1); 3: Remember, use this code safely. Use it only when you came across a do-or-die situation. Killing an application unnecessarily is bad way of programming.
Happy Programming.
.NET framework is a tremendous installment from MSFT. No doubt! Every day-to-day life methodologies are wrapped into a Namespace/Class form. Say, Mail Sending class (System.Net.Mail), and so on. With the release of .NET 3.0, the number of Namespace/Class simply grew and they’ll.
A major installment in .NET 3.0 was Lambda Expressions. That even reduced the Line of Codes(LOC). In a nutshell, Lambda expression converts a bunch of codes into a single line of code, which we(I) may call as one-liner codes. Hard to believe, wait!
How, it affected a programmer? Does it made their work easier or does it made them lazy? Anyway, it made me lazy a bit. Because, whenever I start to implement repeating/familiar codes, say Checking Odd/Even values, etc, I always look for one-liners. Because, I don't want to repeat the same LOC every time. But, its better to know, what’s the internal implementation of one-liner, for Performance and Optimization reasons.
Recently, I’s developing an application for one of my client, in which I’ve to implement a checksum feature for a 9-Digit UniqueID (generated from another code-block).
The checksum technique was simple. My checksum has 3-Digits;
a) 1st Digit = Total Sum (of 9 Digits)
b) 2nd Digit = Even Sum (sums up the digits in the even place)
c) 3rd Digit = Odd Sum (sums up digits in the odd place)
Please note, Chances are that the Total Sum, Even Sum or Odd Sum may have 2 Digits, which you’ve to sum up again to a single digit.
The traditional mode of generating Even Sum, Odd Sum and Total Sum deals with a lot of mathematical computations and all, which is quite tedious to repeat every time. Here comes Lambda Expression, as a handy feature in .NET 3.0.
Here is the code segment for :
a) Total Sum
1: int totSum = uniqueId.ToString().Sum(c => c - '0');
b) Even Sum
1: bool flag = false;
2: int evenSum = uniqueId.ToString().Sum(c => (flag = !flag) ? 0 : c - '0');
c) Odd Sum
1: bool flag = false;
2: int oddSum = uniqueId.ToString().Sum(c => (flag = !flag) ? c - '0' : 0);
I bet, this is quite handy.