7 ms·
Tangent: Looking at those code samples, I wonder whether coming up with the shortest possible, most cryptic variable names is somewhat of a sport amongst C deve
by 9dev 3y ago
Tangent: Looking at those code samples, I wonder whether coming up with the shortest possible, most cryptic variable names is somewhat of a sport amongst C developers. Are you guys still coding in Notepad and need to conserve keystrokes, or where does that reluctance to use proper names come from?
- mauvehaus 3y agoIt's party cultural. Look at the NT kernel mode documentation. In the Windows side of the world, it's perfectly normal for functions to take eight to a dozen parameters, each with a fairly verbose name. Example: https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/fltkernel/nf-fltkernel-fltcreatefile https://learn.microsoft.com/en-us/windows-hardware/drivers/d...
- mixmastamyk 3y agoThe early nineties were a lot different than the early seventies. One poster already mentioned the difference between paper and "glass" teletypes.
- dale_glass 3y agoJust inertia from the times where teletypes were a thing. As in, a mechanical printer that served as your console. When you have that as your interface you want to keep things terse. Even after that you had compilers with limits like 6 character names for a symbol. Those constraints went away, but names made to be comfortable for the users of teletypes and ancient compilers stuck around, and people made more of them because it fit the preexisting theme. And now it's just plain inertia, where it keeps on going because that's what it looks like in the books, so students imitate it.
- mhitza 3y ago> Just inertia from the times where teletypes were a thing. As in, a mechanical printer that served as your console. That's more than inertia, that seems to cross the threshold for ceremony. When was the last time that actual printers/teletypes were used, the 60s? The inwrtia part of it is people imitating what they already see on a project (cause why would you disrupt something as a newcomer on a project) and line length rules which I've seen come up from time to time along the years (not sure if a 120 character line length is enforced nowadays within the kernel)
- MobiusHorizons 3y agoThere are benefits to keeping code less far to the right on the line for easy reading. I review a fair bit of code these days, and I’ve never once wished someone used more characters for their variable names. Long variable names have a tendency to make the code they are part of wrap a lot and become hard to read. I think a lot of people think that a verbose variable name is always better, so they end up lazy and don’t try to figure out what makes the variable interesting in context.
- RhysU 3y agoAs a lingua franca, it's culturally reasonable. After you've grokked it the first time, e.g. "dgemm" is much more convenient than "double precision general matrix multiplication". This is akin to speaking aloud "DC" versus "The District of Columbia". Uses far outweigh learning. Consider even Python calls it a "dict" not a "dictionary" because the latter is a mouthful. Though what was wrong with "map" I often wonder.
- dale_glass 3y ago> Consider even Python calls it a "dict" not a "dictionary" because the latter is a mouthful. Though what was wrong with "map" I often wonder. A conflict with the map() function maybe?
- arp242 3y ago> Just inertia from the times where teletypes were a thing No. I like these names as it's just easier to read. fp or fd is short and to the point, and "filePointer" or variants thereof add noting except more characters to read, more "wall of text"-y code that's harder to scan, etc. And I don't really want to have a discussion about it as such; whatever your preference is that fine. I'll be happy to adjust to whatever works well within a team. But I wish people would stop spreading nonsense like this to invalidate other people's preferences.
- lifthrasiir 3y agoMaybe your tangent is pointing at a wrong direction, because I think there are also a sizable portion of such programmers in many other languages. It seems that anOverlyLongAndInformationFreeIdentifier is widely despised, but once names do have enough information contents, an exactly preferred name wildly varies even in a single code base. For example induction variables in Python generators tended to be shorter than average in my experience.
- cdogl 3y agoThis is particularly true in statically typed languages that require a type declaration. Here’s some go: func copy(f *os.File) { … } I think f is more than clear enough as a parameter name. The same can be said of variables where the type is easily inferred from the declaration or initialization.
- rascul 3y ago> anOverlyLongAndInformationFreeIdentifier Apple has one 82 characters. https://developer.apple.com/documentation/contacts/cnlabelcontactrelationyoungercousinmotherssiblingsdaughterorfatherssistersdaughter https://developer.apple.com/documentation/contacts/cnlabelco...
- xyzelement 3y agoAnd I would say that’s a good case where the long name is meaningful and necessary. On the other hand when you are dealing with a file pointer in a language that deals with file pointers constantly, “fp” is meaningful enough.
- School-Cotton 3y agoCNLabelContactRelationBiaoMei would have been a lot better. People can look up what that means when they need to.
- xyzelement 3y agoI haven't been a dev in a bit, but I'd say between having a longer variable name, and having to crack open the fucking dictionary.... I have a clear preference.
- jstanley 3y agoWhat specifically are you referring to? Do you mean like using `fp` for a file pointer or `fd` for a file descriptor? That is idiomatic, and I would consider calling them `filePointer` or `fileDescriptor` to be an obvious smell that the developer doesn't know what they're doing.
- williamcotton 3y agoI beg to differ... slightly! I'll do something like: int epollFd = epoll_create1(0); So yes, I don't think I have ever typed out "fileDescriptor" but I do label these things to make it more legible! OP has a point. We could do with more discipline when it comes to naming conventions in C. We're not using punch cards any longer. Somewhat related, there's another comment about the 80 char max... clang-format keeps to this. I'm kind of OK with this particular remnant of punch cards because I can get four editor windows open side by side on my ultra wide monitor. The nginx codebase is pretty freaking fantastic and I've learned a lot from just randomly browsing through the source, but I mean, come on: https://github.com/nginx/nginx/blob/master/src/core/ngx_array.c https://github.com/nginx/nginx/blob/master/src/core/ngx_arra... if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { What's p? Oh, ngx_pool_t. Object pool? Memory pool? What's a again? elts? Is that elements? If that read: if ((u_char *) array->elements + array->size * array->nalloc == memoryPool->d.last) { It's time for the old school conventions to change a little bit. C ain't going anywhere. Let's embrace a world where descriptive variable names are not subject to cost/benefit analysis like they were in 1977.
- arein2 3y agoIf you are working within a code base that uses fd and fp, then it's expected to preserve the style. However if it's a new project I would use filePointer and fileDescriptor to have consistent naming scheme along all variables.
- veqq 3y agoTo some extent, I like when people do it in other languages. It exhibits strong exposure to older programming material (where overshort vars abound), evincing great passion for programming, computer science etc. A lot of lisp material features single letter vars, for example. (Actually, var length in prod code bases seems related to scope. So toy examples with obvious context see single letters strewn about. But digging through the classic books will rub off on the budding programmer...
- dezgeg 3y agoBecause for some reason many C programmers still insist on 80-character line length limit and 8-wide tab indentation, so something has to give to fit all on the screen
- IggleSniggle 3y ago8-wide tab is still just 1 char of your 80 allotted. Just sayin'.
- MayeulC 3y agoNah, kernel coding style counts tabs as 8 characters for indentation purposes. It is also discouraged to nest conditional structures too deeply. Here's the very opinionated documentation: https://www.kernel.org/doc/html/v6.5/process/coding-style.html https://www.kernel.org/doc/html/v6.5/process/coding-style.ht... I also want to note that the 80 columns limit was bumped to 100, and is no longer strictly enforced: https://www.phoronix.com/news/Linux-Kernel-Deprecates-80-Col https://www.phoronix.com/news/Linux-Kernel-Deprecates-80-Col
- IggleSniggle 3y agoI wasn't entirely convinced by the opinionated style doc that 80 meant columns and not characters (of COURSE it means columns, the opposite would be nonsense, but sometimes I get stubborn), so I looked up the kernel repo and indeed, my comment didn't pass the sniff test OR the scripts/checkpatch.pl test, where the max_line_length is enforced using calls to expand_tab, which converts tabs into 8 spaces before checking the length of the line.
- enriquto 3y ago> Tangent: Looking at those code samples, I wonder whether coming up with the shortest possible, most cryptic variable names is somewhat of a sport amongst C developers. I definitely make a point of using only single-letter variables in most of my C and python programs. It is a very common usage in scientific computing. In math, all variables are single letters. Always. If a variable has more than one letter, you read it as the product of several variables, one for each constituent letter. When you are translating a formula that reads "y=Ax", you want to write something like y = A * x Writing this formula as output = operator * input or, god forbid, as something like output = linalg.dot(operator, input) is completely ridiculous to any mathematician. Mathematics itself used to be like that in ancient times. But after centuries of distillation, we arrived to the modern efficient notation. Some "programmers" want us to go back to the ancient ways, writing simple formulas as full-sized English sentences. But they will take single-letter variable names from our cold, dead hands! Of course, the first appearance of each single-letter variable must be accompanied by a comment describing what it is. But after this comment, you can use that letter as many times as you want. Encoding that information in the variable name itself would be disturbingly redundant if you use the variable more than once (which will be always the case).
- Terr_ 3y agoThat one lands near the margin-of-error for my sarcasmometer, but either way I'd like to emphasize (non-ironically) that the terse form is indeed very efficient... for someone repeatedly writing/copying it down by hand using a quill and ink. However that particular use-case has become dramatically less significant.
- enriquto 3y agoHeh. No sarcasm at all in my comment! (but I like to write in an over the top way...) Terse notation has nothing to do with manual handwriting. Modern math books and articles are still written by computer using a very terse symbolic notation, which has been developed during the last six centuries. Originally, the symbols +, = were shorthand abbreviations of Latin words. I guess computer scientists want to re-invent everything from scratch. How long will they need to evolve from "LinearAlgebra.matrixVectorProduct(,)" to the empty string? I hope it's less than six centuries!
- crabbone 3y agoYou should try Prolog. That's where single-letter variables are really, really common. C - not so much. In C you are more likely to see acronyms. It's also interesting that for some reason, macro names are spelled out in full, but function names are abbreviated. So, typical C looks like: LONG_SCREAMING_MACRO_NAME_(uh, oh);
- kjs3 3y agoSure...all us oldsters should be using things like ThisIsAVariable_i_like_it_very_much and spend a few hours making sure our bloated IDE highlights the variables we really like in mauve. Bonus points for using ChatGPT to generate the variable names.