6 ms·
A dictionary of single-letter variable names
- johnea 2y agoSingle letter names? hmm... Didn't they make a song about that? a, b, c, d... e, f, g... Something like that?
- hi-v-rocknroll 2y agoWhile some people argue for verbose, semantic-meaning variable and function names, I believe taking this to the extreme is counterproductive, especially for internal code that could otherwise be far more terse. Like antipatterns in Java, C++, or Rust, the more stuff there is at a lower density on the screen, the harder it is to follow. For internal bits, single letter variables should be favored.
- sproketboy 2y ago[dead]
- t-3 2y agoI like to use 'n' as an index when decrementing the counter.
- Dwedit 2y agoC# basically forces "e" to be the name of EventArgs in any event handler. You can change it, but it will always default to "e".
- ZeroClickOk 2y agoI have a hard time normalizing existing codebases where coders insist on using `catch (Exception e)` instead of the default `catch (Exception ex)` and then there are clashes in event handlers with try/catch due to the same variable name...
- rerdavies 2y agoWhich is a PITA when you have a method that takes some event, e, as an argument.
- chaboud 2y agoI thought this was going to be a bit of dry humor before I followed the link. It’s actually serious, and fairly informative (albeit a bit scary). The first place I worked had a soft-but-hard rule (enforced by ridicule) that single-letter variable names not be used. Loop counters like i, j, and k were frowned upon. However, counters like ii, jj, and kk were fine for matrix operations, xx and yy for image loops, etc. There were a couple of key guidelines to follow: 1. If it can have more meaning than sample, pixel, or matrix position, give it more meaning, but two characters repeated is fine for loop counters (again, with meaning). 2. Whatever you use should be quickly findable with text search tools of the era (the late ‘90’s). Someone shouldn’t have to use source inspection assistive coding tools to parse the code you write. When someone else (or you, in a few months) comes upon this code and needs to understand it, they should be able to use find to do it.
- cryptonector 2y agoClearly that rule (no single-letter variables) is pretty dumb. Instead you want something like TFA so that you can easily understand and adhere to the local conventions for such variable names.
- leni536 2y agoOptimizing for grep is fair, however searching for whole words is typically possible in most contexts. I don't have a problem for grepping single letter variable names, although I admit that whole word or word boundary search is not as obvious and slightly less convenient than just plain substring search.
- chaboud 2y agoThat’s basically it. Using double-letter loop counters is a very simple courtesy that one can pay to other developers or oneself in the future.
- gus_massa 2y agoWhy is ii better than i??? I may understand that instead of x,y it may be better to use px,py for positions and vx,vy for speed. Perhaps xx,xy for position. But I'd never use yy.
- artemonster 2y agoAs someone who is used to code in python and typescript explain to me in layman terms why the HELL would you need something like this: "...in lenses or other optics. The simplest complete example is a Lens s t a b: it can extract an a from an s and overwrite it with a b. Doing so would produce a value of type t."
- kristopolous 2y agoHaskell is by mathematicians and for mathematicians. I know this sounds absurd but many actually find it to be the easiest programming language for them. I've met mathematicians who claim they were not able to code until they found Haskell.
- cstrahan 2y agoSure, I can do that. Why the HELL would you need objects and object oriented programming? Wherever you do foo.blah(), you could just do foo_class__do_blah(obj)… so what do objects really grant you that you can’t do with basic procedural programming? Why do you need Python’s function decorators? You can just manually copy and paste whatever logic you need into each and every function, instead of decorating them. Why have an iterator protocol? Every datum can have it’s own bespoke way of iteration that you simply have to memorize on a case by case basis (and can not be abstracted over in any common way), why make everything conform to the same interface? Why have anonymous functions that can close over their lexical scope (what people usually refer to as “closures”)? Consider: let x = 0; let inc = () => x++; You could just write: function inc(bindings) { return bindings.x++; } And then remember to pass along bindings whenever you need to call inc. Besides, that’s more or less what’s going on behind the scenes. I could keep going. We have these facilities not because they are essential (go program in assembler, and you’ll see how few programming techniques and concepts your processor actually cares about), but because they are nice to have. They are nice to have because they provide us tools for abstraction, so by solving the general case, you solve a host of concrete cases. Lenses let you abstract over data traversal and projection. Maybe you don’t appreciate that, and that’s fine. There are C programmers who don’t mind writing 100 different hash tables for different keys and elements. If you are a Python/TS developer, I’m pretty sure you’d find that surprising — if you want a dictionary/hashtable, you just use a dict and call it done — you wouldn’t go build a new dictionary class for each and every key-type/value-type pair. Different people have different tolerances for the drudgery of solving the same problem for the 100th time. However: while I don’t understand why someone would choose vanilla ice cream if they’re given the option of chocolate, I wouldn’t act flabbergasted in such a scenario. Why would I? Why should I expect my desires to be the same as everyone else’s? The only reason I can think of that someone would express shock at someone choosing vanilla over chocolate is if they think that the preference for vanilla indicates something is fundamentally wrong them. Or in other words: being a condescending bigot. That’s kinda how you’re coming off. In the event that you were actually asking in good faith: if you like to broaden your skills so you can finish programming work faster, I’d recommend reading up on lenses. If you don’t care about that, that’s fine too, but I’d recommend against expressing shock at the fact that others do care about these types of things.
- vips7L 2y agoPart of the reason why Haskell never took off. This stuff is incomprehensible.
- dmkolobov 2y agoI know this is a troll comment, but the situations you see these variables are in highly-polymorphic( generic ) code in which there are no more descriptive names. It’s like the use of `T` as a name in most Java or C++ generics.
- yCombLinks 2y agoI don't see it as a troll comment
- broodbucket 2y agoThis stuff is utterly incomprehensible to me as well, but I don't think Haskell is trying to be a mainstream use-it-for-everything language. It does get plenty of real-world usage and the people who like it really like it, so for a language so complicated to get as much use as it does sounds like it "took off" perfectly fine
- semi-extrinsic 2y agoFortran's implicit typing sends its regards from an IBM 704.
- microtherion 2y agoYes, I was thinking of this. Picturing a bastard child of Haskell and Fortran right now, where the type of a variable is determined again by the first letter of its name.
- sitkack 2y agoHasktran is a much better name.
- dvt 2y agoI know this is mostly for Haskell, but I would add (used in various languages): e: event k: key in hash map, dictionary, etc. o: object or reflected object types v: value in hash map, dictionary, etc. t, u, v, w: vectors (borrowed from math) Would be fun to think about this for a while and come up with a more serious dictionary with examples and links to code.
- sph 2y agoAlso r: Reader and w: Writer, especially in languages with such generic interfaces (Rust, Go)
- larodi 2y agoHow come are this specific for Haskell and not universal? My errors are caught as ‘e’, files are ‘f’, apparently indices are i, j, k, for as long as people been writing C. K/V are keys and values, and, well, ‘t’ is time. I’m sure you can guess all other… I really don’t understand how is this a specific language’s thing.
- dvt 2y ago> How come are this specific for Haskell and not universal? The author says as much. There's overlap for sure, but all the monad stuff, for one, is mostly Haskell-(or functional language rather)-specific.
- PaulHoule 2y agoPeople used I,J,K,L for integers in FORTRAN and BASIC for that matter, in fact early FORTRAN determined the type based on the first letter of the name.
- kergonath 2y agoGOD is REAL except if declared otherwise :)
- _jackdk_ 2y ago(Author here.) I think the amount of polymorphic code Haskell enables means that single-letter variables are more common than in other languages (where they're usually confined to narrow scopes), and there's a lot less to say about them. Another HN commenter on this post mentioned IEnumerable<T>, which is very similar: you know nothing about T, but you need to call it _something_, and there's a common understanding in the community that you call such type variables <T>. Haskell's typeclasses then mean you've got a lot of different concepts that could show up in a type signature, but they're still polymorphic and there's not much you _can_ say beyond "this is Foldable", or "this is a Monad". So an even broader convention has emerged.
- yawnxyz 2y agojs land: $: jQuery _: lodash, underscore libraries e: usually events like keyboard, button clicks, form submits, when preventing form actions... any other js quirks?
- gabrielsroka 2y ago$: querySelector $$: querySelectorAll https://developer.chrome.com/docs/devtools/console/utilities#querySelectorAll-function https://developer.chrome.com/docs/devtools/console/utilities...
- yawnxyz 2y agowow I didn't know the humble $ escaped jQuery. Thanks for sharing!! It's about time js/browsers gave selectors better DX. Even simple things like reactivity and stores should be core to JS and not be up to frameworks
- deleted 2y ago[deleted]
- o11c 2y agoEdit: I'm trying to merge in all the ones other people have added; most are original still though. I don't Haskell, thankfully, but a lot of these are applicable broadly. Some notes/additions I've seen in other languages (but far more often these are obvious). (only vaguely sorted, since many aren't contiguous): a can mean "argument" particularly; also "arg" a,b,c,d can be anything, but slightly more common for floats and/or coefficients, or matrices a is often "array" a can be ascii (either as string in general, or in specific) a can be "allocator" or "allocation" b for bytes is not shameful, but sometimes it means "buffer", "blob", or "bits" instead. Can be used for just an element or for a sequence of them. b can be "builder" (a utility class used to construct values instead of using "new" directly) b can be "base" b,e for begin/end (often for iterators/indices) c is often used for "character", "code unit", "codepoint"; ch/cu/cp can disambiguate c can also be "constant" (either a value or a modified type) c can also be "container" c can also be "condition" (a boolean) c can also be "context", but more often ctx or ctxt d is rarely used for "data", but more often it's expanded or v is used instead d can be "direction", but more often dir d,s can be destination/source e can also be "event"; ev if needed e can also be "element" (member of an array, member of an XML-like tree) f is common for floats (even if called "double" or "real") or fixed-point numbers f is often file; fp/fo may be common depending on the language g for graph h for handle i can also be "instruction" i,o,r,w can be input/output i,j,k are common as indices *or* iterators, corresponding to x, y, z if meaningful. l is rare but can be a 4th index or iterator if needed, usually does not related to w though. l is probably more common as "length"/"limit" l can be "long" in various senses l,h can be low/high, but more often lo/hi l,r can be left,right (also lhs,rhs), especially of a binary operator (or occasionally a unary one, e.g. to disambiguate prefix/suffix) m = mode? n,+m are often specifically the upper bound of a range, similar to l n can be node (of a tree/graph) o is an arbitrary object (usually, when explicitly not a primitive or string) o can be "origin" p,+q are not rare as "predicate" p,q can also be prime numbers p is for pointer p = part? p,q = percent chance (often normalized to 0-1 though) q for queue q for query/request q for "quad" q,r,n,d for fractions r (and rv/res) is sometimes used ambiguously - should it be the return value of a function I called, or the value I'm planning to return? Can also stand for "reply". r can be "range" r can be "rectangle" r can be "register" r,c for row, column; origin is top-left and axes are flipped r,g,b,a are color components; other letters exist for other colorspaces s is often an arbitrary string s can also be symbol (sy/sym depending on context), statement (stmt), or set (often given a different kind of name) s can be "stack" t for table t for time t,u,v (usually uppercase) are arbitrary types in generics u,g,o for user/group/other v is often just an arbitrary data value (not meaningfully numeric/string/object), even without a container v,w,t,u can also mean "vector" specifically w = word u,v,s,t,q are used for 2D texturing u can be unicode w can be wide character, but good riddance w is for window x can also be "expression" in particular x,o,d,b can be hex, octal, decimal, or binary bases x,y,w,h are start/size of a rectangle x,y,z,w are used for 3D objects; w is often normalized to 1 _ as a function or string affix means "call gettext on this now"; N_ means "remember to call it later" _,$ are sometimes injected as a general-utility global variable in languages with deficient standard libraries Mostly not mentioned, several of these are used uppercase for generic type arguments. I'm also ignoring all the various units and physical constants. I'm pretty sure about a dozen more passed through my head while writing this but I forgot them before I finished writing the previous entry, and I can't be bothered to actually start scanning code.
- w10-1 2y agoI agree with the semantics, but restrict usage to the very narrow case where the variable has no other use than to convey a value from a visible outer scope to a visible inner scope, where both together are less than a few lines. In that case I prefer the single character over a longer name precisely because it is clearer. Note that x,y,z don’t really qualify as abbreviations since they are the full name of the component, so they can be used when the outer scope is not visible — eg as a parameter.
- phforms 2y agoClojure developers also often make use of single-letter variable names, following conventions in `clojure.core`: https://guide.clojure.style/#idiomatic-names https://guide.clojure.style/#idiomatic-names My rule of thumb is to only use short names in the local scope of a function and have them follow conventions within the language community. Constrained like this, I see no harm in using them, since their meaning is clear within the context of their usage and there is usually no need to search for them. Of course, if the variable has a more specific meaning, I choose a more specific name.
- actuallyalys 2y agoI had the thought of making one for Clojure. I didn’t realize one already existed, so thanks!
- valbaca 2y agoHere's a similar list for Clojure short-name conventions, many are single-letter: https://gist.github.com/valbaca/a55cf9cdcb500e203cadc9ef3555daa0 https://gist.github.com/valbaca/a55cf9cdcb500e203cadc9ef3555...
- __MatrixMan__ 2y agoI'd love to see this idea expanded into various branches of math and science and various other languages. I remember the moment I learned that phi and theta were used by both mathematicians and physicists for rotational components of a spherical coordinates system, but that their roles are switched: math: theta=polar phi=azimuthal physics: phi=polar theta=azimuthal It felt like there should be a place for that kind of knowledge. I'm all for violating convention with purpose, but so often we violate convention because we don't know any better. That's just needless complexity.
- taeric 2y agoHave you gone down the rabbit hole of why the y coordinate is inverted in many computer graphics? :D
- retromagik 2y agoIt's because CRT displays draw from top to bottom, right?
- taeric 2y agoLargely that seems to be the consensus, yes.
- deleted 2y ago[deleted]
- gweinberg 2y agoI think it's more because printers (used to) print going down. I don;t see why it would matter in the least that crts draw the top first, if that is indeed the case. But for old printers where the paper is continuous, down is "forward" and there is no going back.
- taeric 2y agoI think the printers and the old monitors reason are basically the same? Scanlines used to be a "you have now to set this pixel, if you missed it, you get next scan." It is an oddly accurate mental model to think of the screen as printing an image every scan.
- durumu 2y agoIMO using a lowercase l as a numeric variable is cursed. It is way too easy to read it as 1 in many monospace fonts.
- sitkack 2y agoI totally agree. Maybe a font with boxes around the numbers? Or some other way to disambiguate them.
- rozab 2y agoPerhaps some sort of system could be implemented to cause the teleprinter to use a different variety of ink, depending on the nature and syntactic context of the symbol
- sitkack 2y agoI like this, for bold it could backup and hit that char again.
- gsck 2y agoOr just get a font that draws the i with a line and a dot correctly, and a 1 with the horizontal line at the bottom and the half width line at an angle at the top. You are using a bad font.