[erlang-questions] Is it a best practice to have table_name and record same?

Jachym Holecek freza@REDACTED
Wed Dec 16 14:18:43 CET 2009


Hello,

[quoted text rearranged for convenience]

# MAthuvathanan Mou. 2009-12-16:
> I have record with two parameters like
> 
> -record(user, {name, password}).
> 
> I am using above record for a user table.
> 
> I need to create another table country_code with country and code.
> 
> Is it all correct to re-use above record for country_code as well

This would indeed be an extremely nasty thing to do.

> Or else, should I create a common record with two parmeters like
> 
> -record(two_params, {first, second}).

Perhaps justifiable in some cases, but not in yours. Users and countries
are entirely different kinds of things, so I'd say treat them as such.

"Premature abstraction is the root of all evil!" ;-)

> or else do I have to create another record like
>
> -record(country_code, {country, code}).

This is the way to go. Gives you readable source code and more flexibility
than the above options (imagine wanting to extend user/country with new
columns later on).

And to answer the general question: Keeping record name same as table name
is convenient and makes code easy to understand, so I'd stick with that
most of the time. Also, nothing wrong with reusing the same record for
multiple tables (if you have a good reason to keep the tables separate),
as long as they host exactly the same kind of things.

Regards,
	-- Jachym


More information about the erlang-questions mailing list