# Code efficency Easy question

I had an argument with my friend about programming efficency, and I’am wondering if I was right or not. We both created a function that did the exact same thing everytime, wondering which one is most efficent and clean?

``````readonly int teenagerPrice = 10;
readonly int adultPrice = 20;
readonly int seniorPrice = 15;

public int GetPriceByAge(int age){

if(age > 65)
return seniorPrice;
if(age > 18)
return adultPrice;

return teenagerPrice;

}

public int GetPriceByAge(int age){

int priceVariable;

if(age > 65)
priceVariable = seniorPrice;
else if(age > 18)
priceVariable = adultPrice;

priceVariable = teenagerPrice;

return priceVariable;

}
``````

First off, the second function doesn’t do the same thing as the first. It will always return a value equal to teenagePrice because no matter which condition is true (age > 65 or age > 18), after the if statement is executed, it always sets priceVariable to teenagePrice.

Second, the first will be slightly more efficient, simply because it doesn’t have to allocate memory, but this difference will be so slight as to be unnoticeable. They both are O(1), meaning no matter the input, they both operate in the same amount of time, which is really what we’re concerned with. Its a given that some operations will simply take longer, but we think about complexity in terms of orders of magnitude. Being able to think about program efficiency is a big part of a programmers job, and you should look into it further. Google “Big O” notation

Third, and the most compelling reason to use the first function, is that it is simply shorter and more legible, which in the long term, will help you write better, more efficient code, as well as help you spot inefficient code more readily.

TL;DR These functions don’t do the same thing. Supposing they were written correctly, they are basically identical in terms of complexity. Use the first because its more legible.