Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short and Byte best practice

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

can it be considered a best practice to use Integer to map all database integers that are explicitly 8bit or 16bit (MySQL TINYINT and SMALLINT)? I started by using java.lang.Byte and java.lang.Short, but then realized writing new Short((short)12345) was anything near what I consider clean code. Also, the MySQL JDBC driver returns Integer for these two types. It's definitely not a coincidence...

Karsten
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the column is explicitly 8-bit specifically because it models a number which can be between -256 and +255, then you might want to have something in your application which enforces that. Using Byte might help, but even then an arithmetic operation which overflows the byte will silently wrap around, yielding the wrong data.

On the other hand if you used that column type to model a single-digit field and you mostly chose that type to save disk space, then maybe you can just use an Integer internally in your code.

It all depends on how tightly you want to control the mapping between the program data and the database data. Even so it might be simpler to just use Integer for the fields and control the range limits in your code, since using Short or Byte doesn't help with that at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic