Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Variables

Say you have a variable called hotelPropertyNumber. The contents will always be a number but normally be used as a string. Would there be anything "wrong" with declaring it as a string so that you don't have to continually convert it to a string....or is that a bad programming habit?

Thanks.

like image 482
Lenard Avatar asked Jun 19 '26 17:06

Lenard


2 Answers

A number is a mathematical object used in counting and measuring. If you use your hotelPropertyNumber for counting, i.e. apply any arithmetic operations to it, it is number and should be stored as a numeric type.

If not, then it is not a number; it is a string.

like image 179
Igor Krivokon Avatar answered Jun 22 '26 19:06

Igor Krivokon


In some languages you can create a user-defined type such as "class HotelPropertyNumber", which:

  • Supports exactly the methods you need
  • May store its data internally as a string
  • Can validate (in its constructor) that its value has a number-like syntax
  • Can't be confused with other number and/or string type instances which aren't HotelPropertyNumber instances.
like image 41
ChrisW Avatar answered Jun 22 '26 19:06

ChrisW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!