views:

58

answers:

4

Which naming convention for conversions is standard across most languages?

convert_from_typea_to_typeb(arg)

or

convert_to_typeb_from_typea(arg)

Or is there another standard?

A: 

I guess in C(++) you would do it with cast operators, so none of these names would fit.

In Objective-C I've seen something like NSPointFromCGPoint(...), or you would give the old type simply in the constructor.

The only thing that I've seen very rarely are those functions with convert_x_to_y :-)

Eiko
C doesn't have conversion operators and for C++ there are often code guidelines that prohibit them as it makes code hard to maintain when conversions are not spelled out explicitly.
Georg Fritzsche
+1  A: 
Kris
+1  A: 

My answer, and purely subjective, would be the one that flows logically with source to destination.

ConvertStringToInt()

or

ConvertVisitorToCustomer()
p.campbell
A: 

There is no cross-language standard for this kind of thing. Most languages either provide a built-in way to do this (C/C++) or encourage you to find another way around it (Java/C#).

In most object-oriented languages you shouldn't need this sort of functionality. You should try to restructure your program so that you do not need to do any explicit conversions.

However, if you really need to do this, I'd say drop the "convert" at the beginning and do <source>_to_<target>. Examples: string_to_int, int_to_float, foo_to_bar, etc. (Of course, write these in whatever case your language prefers. For example, in C#: StringToInt; in JavaScript: stringToInt.)

musicfreak