lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I figured I'd add my $0.02:

As an acquaintance of mine which is in charge of a team who writes development tools including compilers likes to express in various ways, identifiers in c only last the first few fractions of a second when compiling c code.   It doesn't matter to the compiler whether an identifier (and especially the name of a typedef) is a single character long or hundreds of characters long.  The sole reason for using an identifier is to put a name on something.   And, as many many programmers are likely to tell you (and especially once you start learning about topics such as clean code and code smells), it is best for an identifier to be spelled out, not subject to interpretation, and doesn't necessarily need to be short.   The only space you're saving is on the disk of the developer (or anyone else who deals with the source code), and that is cheap and plentiful.

In addition, I personally find typedefs which just reiterate the type to be useless.   If you're going to go to the bother of defining a type, define a type, not just an alias for a existing type.

Something like "typedef const char *" ccp  is horrinbly wrong on so many levels.  Better would be something like "typedef cost char * constCharPtr" but even that is just regurgitating what you already are saying.    Far better is to describe the use something which describes the data itself, i.e. "typedef const char * constNullTerminatedStringPtr" if that's what it is used for.  If it's pointing to a different type of data, use an appropriate type name for that.

That way when someone encounters something like

constNullTerminatedStringPtr yourVariableName

It's obvious your intent.

To head off some nit-picking about my suggestions above:   I want to be clear about my intent here:  I'm trying to point out the advantages of well thought out typedef (and other identifier) names, not state that the above are necessarily well thought out.  They might not be.  In particular, I'm not sure whether I'd include Ptr in the name or not.   I've also known myself to add 'Type' at the end of the identifiers for typedefs which some people like and some hate.  Once could also argue about where to place 'const', etc.   But, those discussions are secondary to the key point here:  use identifiers which are clear in their intent and are easy to read, and don't just regurgitates the underling C code.   

I'd highly recommend reading about refactoring and code smells and the like.   Also the video series by Robert C. Martin is excellent if you have access to them for free through some method (such as safari books online).
 

On Sun, Feb 4, 2018 at 4:25 AM, Paige DePol <lual@serfnet.org> wrote:
I was doing a bit of a code audit when I had a thought...

Why not create a typedef for 'const char*' to save some space in the code:

typedef const char* l_str

There were already some functions that began with 'l_str' for conversion or
for Lua versions of C string utility functions, so it seemed logical.

As there were 697 matches in my current code base and the typedef is 6 or 7
letters shorter, depending on the position of the *, that saves nearly 5000
characters. Yes, not really a big deal, but what it does do is help things
fit into 80 columns better by providing extra space in functions with a lot
of 'const char*' parameters, or variables, or typecasts. I have made this
change to my code base and it really does make a number of functions tidier
due to the extra space.

I did some searching and generally it is advised to not hide pointers behind
typedefs, unless the data type is opaque. I would say that C strings should
qualify for this treatment and in my opinion it really works well.

I also note that Lua itself has a typedef that also hides the pointer:

typedef TValue *StkId;

I don't see any reason why this shouldn't be an okay thing to do, but in
my searching I mostly found warnings about not doing it... usually for other
data types, and not const char*... so thought I'd check to see what opinions
there were, or to see if I am violating some unwritten coding style rule! ;)


I also found a potential missing 'const' in gc_state() from 'ltests.c':

  static const char *statenames[] = {"propagate", "atomic", "sweepallgc",

Shouldn't that be as follows to match all other static lists of C strings:

  static const char *const statenames[] = {"propagate", "atomic", "sweepallgc",


Unless there is some serious reason for not typedefing 'const char*' I think
I will go with it as I do think it is an improvement in code readability!

~Paige





--
Forrest Christian CEO, PacketFlux Technologies, Inc.
Tel: 406-449-3345 Address: 3577 Countryside Road, Helena, MT 59602
forrestc@imach.com http://www.packetflux.com