Thursday, August 21, 2014

Using C# Ternary (?) operator statement as a method parameter


The '?' operator is one of the oldest conditional operators that has been coming along with object oriented languages such as C++, Java and C#. The most significant advantage of using it is it will reduce a couple of lines of code than using the traditional if-else or switch statements, where there is a need to return values.

I first came across the functionality and power of using this just a couple of years ago, while assigned with a task to re-factor some existing code of a senior. More often I have seen the use of this being used with assigning the results to variables. And subsequently my coding behaviour also followed suit. 


Recently I was wondering if the usage could only be limited to assigning variables, and tried adding the statement directly to a method passing it as a variable. It just works fine, since as long as the returning type matches the variable type of the method parameter, such an assignment succeeds. Though this maybe already a straight forward understanding for C# or Java experts, yet this explanation could serve for those in doubt, and to understand the beauty of the language. Ok, with out any further exaggeration, let's have a look at what I am talking about.

Lets assume you have a method to display a name by passing in a name parameter like the following:


Code:
void DisplayName(string name)
{
//
}

Now simply let's assume Two names, 'Ahmed' and 'Jack', and we are to display the name which has less than 5 characters and pass that name to the above method to be further processed. Using the ternary operator the approach would be:


Code:
string name1 = "Ahmed";
string name2 = "Jack";
var nameLessThanFour = name1.Length < 5 ? name1: name2;
DisplayName(nameLessThanFour);

Eliminating the intermediate variable assignment, this could be achieved also as the following:


Code:
DisplayName(name1.Length < 5 ? name1: name2);


Note : This approach would work only for methods that pass arguments by value and not for those that pass arguments by reference (i.e the arguments with ref and out keywords will not work).

Personally I would not recommend the over usage of this approach on method parameters as it could negate the very purpose of simplicity of reading (i.e You might end up with a longer line in the method parameters mixed up with '?'s, ':'s and ","s thereby making it complex to read).

I find the following scenarios where you can advocate this approach:
  1. Methods that have parameters which have a need to pass arguments which require results from a condition.
  2. Using this approach on no more than two or three arguments of a method to reduce reading complexity.
  3. Using this approach on methods that could be called on parts of other lengthy conditional statements such as switch or if-else. This way we could save a lot of unnecessary variable assingments prior to method calls.
That's it. I would like to hear opinions of others on this. Happy Coding ahead...!

Tuesday, August 5, 2014

Operation Protective Edge இல் கொல்லப்பட்ட ஷஹீத் ஒருவருக்கான இரங்கல் செய்தி


Abdul-Hamid Mohammad Abdul-Hamid Al-Maghrabi, 31 வயதான இவர் கடந்த 2014, ஜூலை 26 ஆம் திகதி காஸாவில் கொல்லப்பட்டார். கடந்த ஜூன் மாதம் ரமழானுக்காக காஸாவை விட்டும் வெளியேறுவதற்கு இவருக்கு அனுமதி கிடைத்தும் அதை மறுத்தவராக "இந்த வருடம் எமது பள்ளிகளுக்கு இமாமாக இருக்க விரும்புகின்றேன்" என்றும் தெரிவித்திருந்தார். இந்த அருமையான ஷஹீதைப் பற்றி சகோதரி ஸைனப் அமீன் என்பவருடைய வலைப்பூவில் பின்வருமாறு அனுதாபச் செய்தி வழங்கப்பட்டிருந்தது:
சுப்ஹானல்லாஹ்..! இந்த காணொளி பல தரங்களில் சிறப்பு பெறுகின்றது.
1) முதலாவதாக, இந்த மனிதர் காஸாவை விட்டும் வெளியேறுவதற்கு அனுமதி வழங்கப்பட்டும் அதனை மறுத்துள்ளார். ஏன்? எதற்காக? அவரது வரிகளில்:
"இந்த வருடம் எமது பள்ளிகளுக்கு இமாமாக இருக்க விரும்புகின்றேன்" . அல்லாஹு அக்பர்...!
2) இரண்டாவதாக, எமது படைப்பாளனிடம் சிறந்த நிலையில்( நோன்பு நோற்றவராக), சிறந்த மாதத்தில் மீண்டுள்ளார். அல்லாஹ் அவரைப் பொருந்திக் கொள்வானாக..அல்லாஹு அக்பர்...!
3) மூன்றாவதாக, இந்த குர்ஆன் வரிகளை திட்டவட்டமாக ஓதுவதற்கு என்ன சாத்தியங்கள் இருந்தன:
"அல்லாஹ்வின் பாதையில் போரிட்டுக் கொல்லப்பட்டவர்களை மரித்தவர்கள் என்று நிச்சயமாக எண்ணாதீர்கள் - தம் ரப்பினிடத்தில் அவர்கள் உயிருடனேயே இருக்கிறார்கள் - (அவனால்) அவர்கள் உணவளிக்கப்படுகிறார்கள்." (3:169)
இது தற்செயலாக நடைபெற்றது அல்லவே. மாற்றமாக இந்த மனிதர் சந்த்ரப்பத்துக்கு ஏற்ற மிகப் பொருத்தமான குர்ஆன் வசனங்களையே ஒதுகின்றார்.
4) நான்காவது, அவரது குரல். ஒரே வரியில் கூறுவதென்றால் "பிரம்மாதம்". வேறு எப்படி என்னால் விபரிக்க முடியும்?
'தியானி', 'பிரதிபலி', 'செயற்படுத்து'. அல்லாஹ் தான் தன்னுடைய ரப்பு என்றும், ரிzஸ்க்கும், வெகுமதிகளும் அவன் பாலிருந்தே வருகின்றன என்பதனை அறிந்திருப்பதனை விட வேறு என்ன சந்தோஷம் ஒரு இறை நம்பிக்கையாளருக்கு இருக்க முடியும். நிச்சயமாக அவன் நம்பிக்கையாளர்களின் செயல்களை வீணாக்குவதில்லை. இந்த ஆயத்துகள், முஸ்லிம் உலகம் தற்போது இருக்கும் நிலைக்கு மிகவும் பொருத்தமாக இருக்கின்றன.
"யா அல்லாஹ்! இன்னாரை மன்னித்து அன்பைச் சொரிவாயாக, இவரை மன்னித்து கிருபை செய்வாயாக, அவரது மீட்சியை அருள்பாளிக்கப்பட்டதாக்குவாயாக. அவரது நுழைவை விசாலமாக்கி, நீர், பனி, மற்றும் பனிக்கட்டிகள் மூலமாக அவரை கழுவி விடுவாயாக, மேலும் கறை படிந்த வெண்ணிற ஆடையை தூயமையாக்குவது போல அன்னாரது பாவங்களிலிருந்தும் அன்னாரை அகற்றிவிடுவாயாக. அன்னாரது வதிவிடத்துக்கு பதிலாக சிறந்த வதிவிடத்தையும், அன்னாரது குடும்பத்துக்கு பதிலாக சிறந்த குடும்பம் ஒன்றையும், அன்னாரது துணைவிக்கு பதிலாக சிறந்த துணைவியையும் மாற்றீடாக வழங்குவாயாக. மேலான சுவர்க்கத்தில் அவரை அனுமதிப்பதோடு, கப்ரின் வேதனை மற்றும் நரக நெருப்பின் தீங்கிலிருந்தும் அவரை பாதுகாப்பாயாக"
எனது சமூகமே..! அநியாயக்காரர்களின் கரங்களில் சிக்குண்டு தவிக்கும் எமது உம்மத்தினருக்காக பிரார்த்திப்பதனை மறந்து விட வேண்டாம். அவர்களை உங்களில் ஒருவராக கருதி அவர்களுக்காக பிரார்த்தியுங்கள்! இன்ஷா அல்லாஹ்..!
சுப்ஹானல்லாஹ்..! அல்லாஹ் அவர்களை ஏற்றுக் கொள்வானாக, அல்லாஹ் அவர்களை ஜென்னதுல் பிர்தவ்சில் அவர்களை மீண்டும் இணைப்பானாக. அவர்களை பிரிந்து வாடும் அவர்களின் அன்புக்குரியவர்களுக்கு அல்லாஹ் பொறுமையை வழங்குவானாக. அடுத்தவருடைய மறுமை வாழ்வுக்காக உழைக்கும் மனிதர்களில் நின்றும் எம்மை வல்ல அல்லாஹ் திகழச் செய்வானாக. மேலான சுவனத்தில் எமது சகோதர சகோதரிகளை சந்திக்கும் மிகப்பெரிய பாக்கியத்தை எமக்கு வல்ல அல்லாஹ் தந்தருள்வானாக...!
மூலம்: http://goo.gl/Kyz5ut