FrontendWalla

Solutions for Real World Frontend Problems.

Loose vs Strict Typed Languages and a Code Bug

If somebody has worked in in a strict type language like java he would definitely know that function in java have to return type.



So lets say we need to define a function for sum of two numbers.


We would say:


public int add(int a,int b){
return a + b;
}


Here we are very sure that we will be returning a value of type integer. but in cases we may want to return a integer and a boolean or a string.


For example say a very common function in String Manipulation indexOf the function returns the index of matching substring.


int indexOf(String str) 


The return type is integer. So to find “Fox” index in “One Sharp Fox” you will write.


“One Sharp Fox”.indexOf(“Fox”); and will get 10 index.


But what if the match is not found. In these cases you might want to return “No Match Found” or false. But you cannot because the return type is an integer. And therefore you will have to return a number and in this case it is “-1”.


and your no found comparision goes like


if(“One Sharp Fox”.indexOf(“Lion”) == -1 )


In Case of loosely typed languages we do not have this restriction and we can have more then one type in return.


I was working through one particular bug in our code where we have used a php function


array_search()


Now php being a loosely typed language can have more than one return type.


Array_search function returns the Key of the Search String in the array in case the match is found. But in case the match is not found it will return false instead of -1 as we have seen in case of java code example.


So for example if we have an array storing car brands.


$cars=array(“Saab”,”Volvo”,”BMW”,”Toyota”);


and if we do 


array_search(“BMW”,$cars);

It will return 2 as a search index.

But if we search “Hyundai” in this array

array_search(“ Hyundai“,$cars);

We will get false as a return value.

The interesting bug that i found in our code was we had to find the Role of a user.Roles were available in an array having several values and we had to return true if user had “Business Admin” roleThe function that we have used was like 

function adminType($arr){

if(array_search(” Business Admin”,$arr)){
return true;
}else{
return false;
}

}

At first glance it looked perfect to me and was working fine. Until a user complained that She is a Business Admin and is not able to see a particular functionality.

On debugging the code I found that She just had one role and that was “Business Admin” 

So now if we go through the function.

-> $arr = {“Business Admin”}
array_search(” Business Admin”,$arr)

will evaluate to 0 the index of the “Business Admin” and   

if(0) will let enter into else statement and return false. So the code will not work as desired.

On searching further I found that there is another function in_array($arr) which will just return true of false if a match is found or not. And i corrected our function by replacing array_search with in_array. 

Overall I believe working in two different programming languages shows you features and their advantages and disadvantages both. Here in PHP though being loosely typed gives us an advantage of having any return type. But then we have to take care of our comparisons very well.


Loose vs Strict Typed Languages and a Code Bug

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top