foo: -> foo_() Naming convention for generated C code

Michael Lucas-Smith ich at driftwood.darktech.org
Wed Jul 19 12:09:11 UTC 2000


Paul Fernhout wrote:

> I've been generating some C and Pascal code from Squeak lately related
> to Pointrel. I've realized the Squeak C output naming convention is not
> as good as it could be.
>
> For example, a Smalltalk method like:
>   MyClass>>foo: aString bar: anInteger baz: anArray
>
> produces this smushed together function declaration in 2.7:
>   int foobarbaz(int aString, int anInteger, int anArray);
>
> I think the prejudice :-( against the underscore character shows here.
>
> Underscores are a valid and useful character in C and Pascal (and
> VisualWorks!) programming. I use them to denote units as in
> "compute_rainfall_inches();".
> I also use them to do psuedo-OO programming in C as in
> "MyClass_function(myClassPointer, args);".
> I wrote an AI Expert article on this last naming convention back around
> 1987.
>
> So, if the C code is generated with underscores in place of colons, one
> gets code that compiles under both C and Pascal but looks much more like
> Smalltalk and is more readable in that context.
>
> The alternative result from a modified the CCodeGenerator for
> foo:bar:baz: would be:
>
>   int foo_bar_baz_(int aString, int anInteger, int anArray);
>
> Why you ask would one want to do this? The answer is that the resulting
> code is easier to read and maintain. This is because the underscore
> retains syntactic (and related semantic) information about the intended
> argument useages which was present in the Smalltalk code because of the
> key:word: syntax. There is no guessing about the expected number of
> arguments or their meanings.
>
> Note that the trailing underscore is needed and gives lots of
> information.
> So for example, "size()" might return a size and "size_(value)" might
> set the size. The trailing underscore here is very significant.
>
> The Smalltalk code to do this is simple:
> CCodeGenerator>>cFunctionNameFor: aSelector
>     "Create a C function name from the given selector by omitting
> colons."
>     "Modified by pdf to replace colons with underscores".
>
>     ^self currentClassName, '_', (aSelector copyReplaceAll: ':' with:
> '_').
>
> Oops, you can ignore the "self currentClassName, '_'," part for now.
> That belongs to some other changes. :-)
>
> -Paul Fernhout
> Kurtz-Fernhout Software
> ===============

I like this. It gives the chance to be able to FileIn C/Pascal code back in
to Smalltalk.. as long as the C/Pascal code follows the right coding
'rules'
Perhaps you can write about the implications of including the
currentClassName_ in the method name for the generated code.

Michael.





More information about the Squeak-dev mailing list