Skip to main content

If then else short hand

ඔයාල කවුරුත් ගොඩක් වෙලාවට පාවිච්චි කරන if then else කියන statements ගැන අලුත් දෙයක් කියල දෙන්න තමා අද ලෑස්ති වෙන්නේ.

මුලින්ම අපි බලමු සාමාන්‍යයෙන් අපි if then else ලියන්නේ කොහොමද කියල. අපි මේ වෙලාවේ උදාහරණයක්‌ විදිහට ගමු ඉලක්කම් දෙකකින් ලොකු ඉලක්කම මොකක්ද කියල හොයාගන්න පාවිච්චි කරන සරල function එකක්. අපි කවුරුත් මේ වගේ එව්වා ලියල තියෙනවා programming පුරුදු වෙන කාලේ,

 private int GetMax(int i, int j)  
 {  
    int max;  
    if (i > j)  
    {  
       //condition එක true උනාම කරන්න ඕනේ වැඩේ  
       max = i;  
    }  
    else  
    {  
       //condition එක false උනාම කරන්න ඕනේ වැඩේ  
       max = j;  
    }
    return max;
 }  

එත් ඉතින් මෙච්චර පුංචි දෙයක් ලියාගන්න ඇයි අපි මෙච්චර මහන්සි වෙන්නේ?

ඒ function එකම අපිට ලියාගන්න පුළුවන් පහල පෙන්නලා තියෙන විදිහට.

 private int GetMax(int i, int j)  
 {  
   int max;  
   max = i > j ? i : j;  
   return max;  
 }  

මේ ලියල තියෙන code එක පොඩ්ඩක් විතර කතා කලොත්,

= ලකුණයි ? ලකුණයි අතරට එන්නේ අපිට check කරන්න ඕනේ condition එක,
? ලකුණෙන් පස්සේ තියෙන්නේ check කරපු condition එක true උනොත් මොකද්ද කරන්න ඕනේ කියන එක.
: ලකුණෙන් පස්සේ එන්නේ condition එක false උනොත් මොකක්ද කරන්න ඕනේ කියන එක.

ඔන්න බලන්න වැඩේ හැටි. ඔන්න ඕකට තමා කියන්නේ if then else short hand කියලා.

Comments

  1. ගීතාංග, වැඩේ super , කරගෙන යන්න

    ReplyDelete

Post a Comment

Popular posts from this blog

Adding Unique Constraints with Entity Framework Code First

Entity Framework Code First is a great way to define and maintain your database within your application it self. While it poses a nice set of complementing libraries like Data Annotations and Fluent Configurations which helps you specially in defining phase of your database, it would give you a headache if you try to define a unique constraint on a column. For example in the users table of your application you could probably have an int ID column which would serve as the primary key and you might need to make your Username column a unique one. Since you are using EF Code First you will soon find out there is no direct way to accomplish this requirement. Unfortunately fluent configurations doesn't have syntax like HasUnique(u => u.Username); I asked the same question in stackoverflow , but didn't get a convincing answer. Since there is no direct support from EF for this you could take one of following alternatives to achieve it. Approach 1 :  ...

Building Highly Scalable Web Applications with Windows Azure

Among many other benefits of moving or building your web applications in cloud, for me I think the most important benefit we get is scalability. When it comes to web applications there are two approaches for scalability. Scale out - This means we increase the number of running instances of the application with a load balancer which distributes the requests among those instances Scale up - This means we increase the physical resources on a single application instance, for example we can increase the RAM of the hosted machine In reality preferred way of web application scalability should be to scale out. Because there are obvious hardware limitations in scale up approach. Bearing that in mind if we look at what Windows Azure provides out of the box, for a certain level it supports scale up approach and it has a great amount of support for scale out approach. Scaling your application out Windows Azure supports both automatic and manual scale out of the ap...

HTML INPUT enhancement with Regular Expressions and Java Scripts

අද කථා කරන්න යන්නේ බොහොම සරල දෙයක් ගැන. සරල උනාට ඉතින් මට මේක කරගන්න ඕන වෙලා හොයන කොට නම් ඉතින් හොයාගන්න ටිකක් කරදර උනා, ඒ නිසාම තමා මම හිතුවේ මේක ගැන ලියන්න ඕනෙ කියල. අපි හැමෝම HTML forms validate කරන්න Java Script පාවිච්චි කරනවා නේ, එක අලුත් දෙයක් නෙමේ. ඒ වගේම තමා Regular Expressions කියන්නෙත් අපිට අලුත් දෙයක් නෙමේ. අපි බලමු මේ දෙකම පාවිච්චි කරලා HTML textbox එකකට අපිට ඕනෙ characters විතරක් enter කරන්න දෙන්නේ කොහොමද කියල. මෙන්න මේක තමා අපි පාවිච්චි කරන HTML page එක. <!DOCTYPE html> <html> <head> <title>JS and RegEx</title> </head> <body> <label for="txtUsername">Username</lable> <input type="text" id="txtUsername" placeholder="Enter Username"/> </body> </html> මේ තියෙන page එක html විදිහට save කරලා browser එකේ බැලුවම මෙන්න මේ වගේ තමා පෙන්නේ. දැන් හිතන්න අපිට මේ username එකට @,#,%,$ වගේ...