21 ms·
Greppability is an underrated code metric
- x3n0ph3n3 2y ago> No AI tooling was used in the creation of this article. That was refreshing.
- jimmaswell 2y agoIt just doesn't have that genuine artisan smell to it when someone uses ̶a̶ ̶p̶r̶i̶n̶t̶i̶n̶g̶ ̶p̶r̶e̶s̶s̶ a̶ ̶c̶o̶m̶p̶u̶t̶e̶r̶ ̶w̶i̶t̶h̶ ̶a̶u̶t̶o̶m̶a̶t̶i̶c̶ ̶t̶y̶p̶e̶s̶e̶t̶t̶i̶n̶g̶ s̶p̶e̶l̶l̶c̶h̶e̶c̶k̶ W̶i̶k̶i̶p̶e̶d̶i̶a̶ AI to help write their article.
- jrd259 2y agoThe ability to search the code base is one reason I insist on correct spelling even in comments when doing code reviews, and also keep adding to my IDE's dictionary so it will catch my spelling errors.
- codedokode 2y agoCSS preprocessors are often used in a way that hurts "greppability". For example, SCSS developers often break identifiers like .product { &-promo { I always felt it was a bad practice but nevertheless it is often used. Glad to find out that I am not the only one who noticed this. Of course, one might say that I should have used a specialized tool for this. But it would be more convenient if I could search anything with text search. Also, there is no tool that allows to search over identifiers that are calculated in runtime in a program.
- krawczstef 2y agoYep. When I was designing https://github.com/dagworks-inc/hamilton https://github.com/dagworks-inc/hamilton part of the idea was to make it easy to understand what and where. That is, enable one to grep for function definitions and their downstream use easily, and where people can't screw this up. You'd be surprised how easy it is to make a code base where grep doesn't help you all that much (at least in the python data transform world) ...
- eschluntz 2y agoTotally agree! I wrote this a few years ago: https://blog.cobaltrobotics.com/you-cant-read-code-that-you-cant-find https://blog.cobaltrobotics.com/you-cant-read-code-that-you-...
- maaaaattttt 2y agoFrom a true e-commerce story. If you have to somehow deal with product conditions, don't use the condition "new" for new vs. mint, used, damaged products. This has a nightmarish greppability… I'll never do that mistake again.
- kmoser 2y agoThe example for "Don't split up identifiers" is impractical when you need to construct a string on the fly. Author's example might work for the simple case of two potential values, but as soon as addressType can take on more than a trivial number of values, the code becomes very non-DRY. Also, the example completely glosses over the egregious use of magic constants. Wouldn't it be better to declare addressType an enum? The example for "Use the same names for things across the stack" only works if you control the whole stack. Sometimes you have to get fetch data from a 3rd party. And even if you do own the whole stack, sometimes it's nice to be able to search for 'streetName' when you're looking for the JS symbol, and 'street_name' when you're looking for the DB column. So I don't think this rule is always beneficial. At the end of the day, if you really want to make your code grepable, add some verbose comments that include useful keywords. (I've found it useful to include keywords that aren't even in the code but which devs may search for.) This lets you grep regardless of how the code is written or formatted.
- up2isomorphism 2y agoNon of these matters if 99% companies use managers who don’t write code to evaluate software engineers.
- VMtest 2y agothose who said using an IDE is better for tasks like the article describes, I would say, not really I have experienced a number of cases where IDEs were opened but they were useless, and I had to grep+find+vim+tmux for everything in front of ex-colleagues who didn't know better 1. a python codebase written for web scraping, this project can single-handedly be used to teach people how to not write python 2. production service in linux bare-metal servers where nginx + lua files were used for load balancing, backend written in go a few years ago, and there were php files as well 3. php and typescripts large codebases, debuggers were used as well, and no, no one could reason with the problematic areas, particularly on multiple inheritance diamond problems. I was never contented with my successful attempts to adding code in them I don't know how useful IDEs can be in these cases, but no one other than me in those ex-companies could handle them. I even lent my laptop with IDE and full environment to some supposedly-very-senior colleagues, they could only avoid the problems at best edit: and for a project like chrome, [Fixing a bug in Google Chrome as a first-time contributor](https://news.ycombinator.com/item?id=41355303 https://news.ycombinator.com/item?id=41355303)
- JoshTriplett 2y agoThis is the reason many coding styles and tools (including the Linux kernel coding style and the default Rust style as implemented in rustfmt) do not break string constants across lines even if they're longer than the desired line length: you might see the string in the program's output, and want to search for the same string in the code to find where it gets shown.
- bobbylarrybobby 2y agoIf I recall, rustfmt had a bug where long string literals (say, over 120 chars or so — or maybe if it was that the string was long enough to extend beyond the gutter when properly indented?) would prevent formatting of the entire file they were in. Has this been fixed?
- JoshTriplett 2y agoNot the whole file, but sufficiently long un-line-breakable code in a complex statement can cause rustfmt to give up on trying to format that statement. That's a known issue that needs fixing.
- knodi123 2y agoMy team drives me bonkers with this. They hear the general principle "really long lines of code are bad", but extrapolate it to "no characters shall pass the soft gutter no matter what". Even if you have, say, 5 sequential related structs, that are all virtually identical, all written on one line so that the similarities and differences are obvious at a mere glance... Then someone comes through and touches my file, and while they're at it, "fix" the line that went 2 characters past the 80 mark by reformatting the 4th struct to span several lines. Now when you see that list of structs, you wonder "why is this one different?" and you have to read carefully to determine, nope, it just contained one longer string. Or god forbid the reformat all the structs to match, turning a 1-page file into 3 pages, and making it so you have to read and understand each element of each struct just to see what's going on. If I could have written the rule of thumb, I would have said "No logic or control shall happen after the end of the gutter." But if there's a paragraph-long string on one line- who cares?? We all have a single keystroke that can toggle soft-wrap, and the odds that you're going to need to know anything about that string other than "it's a long string" are virtually nil. Sorry. I got triggered. :-)
- abc-1 2y agoA lot of this reads like code search tools could and should be a lot better. They probably will be with AI finding its way into everything. In the old days, people would Hungarian prefix types, but now the IDE mitigates that with color codes.
- klodolph 2y agoDo you have some ideas for how to make code search better? Right now, code search is basically just text search. If you think code search tools “could and should” be a lot better, what kind of improvements are you thinking about? How would those improvements work?
- Terr_ 2y agoNot OP, but we wouldn't need to worry so much about picking out distinct greppable names if (big if) there were tools that parsed the code to draw out concepts for us, ex: 1. The popular "Find Usages" which varies widely in accuracy and reliability by language, IDE, and codebase meta-quirks. 2. Tools that show Callee/Caller trees, and sometimes possible data-flows between variables. 3. DSLs to search hierarchies, like how XPath lets you find XML elements based on nesting, rather than relying on a distinctly greppable single tag-name for the leaf you're interested in. (e.g. `<Product><Name>` vs `<ProductName>`) When things go well, the actual variable name no longer needs to restate certain aspects and relationships that can instead be found through metadata. For example, `GiftCard.purchaser_customer_uuid` is nicely greppable, but you could relax that to `GiftCard.purchaser` if it had a static type of `UUID<Customer>`. Or perhaps you could go to the `Customer.uuid` definition and say "Show me all variables that can populate or be-populated-by this one, up to X steps out, and excluding ones that are function scoped." That said, I do advocate for "greppability" as a general practice, since I seldom trust that languages, tools, or institutions will come together in a way that makes it unnecessary.
- klodolph 2y agoI guess I wasn’t thinking of “find usages”, but as the article points out, it’s hard to find usages if the usages are dynamic. The solution—to write code which is less dynamic—helps code search and features like find usages.
- ralusek 2y agoEspecially in untyped languages, working with an old or unfamiliar codebase, sometimes the only way to know "was anything else using this code" is just to search for the name of a function or whatever.
- adpirz 2y agoI've seen some pretty wild conditional string interpolation where there were like 3-4 separate phrases that each had a number of different options, something akin to `${a ? 'You' : 'we'} {b ? 'did' : 'will do' } {c ? 'thing' : 'things' }`. When I was first onboarding to this project, I was tasked with updating a component and simply tried to find three of the words I saw in the UI, and this was before we implemented a straightforward path-based routing system. It took me far too long just to find what I was going to be working on, and that's the day I distinctly remember learning this lesson. I was pretty junior, but I'd later return to this code and threw it all away for a number of easily greppable strings.
- ctxc 2y agoTangential: I love it when UIs say "1 object" and "2 objects". Shows attention to detail. As opposed to "1 objects" or "1 object(s)". A UI filled with "(s)", ughh
- petepete 2y agoMoreso when it's not tripped up by "1 sheeps" or "1 diagnoses".
- gnuvince 2y agoI like the more robotic "Objects: 1" or "Objects: 2", since it avoids the pluralization problems entirely (e.g., in French 0 is singular, but in English it's plural; some words have special when pluralized, such as child -> children or attorney general -> attorneys general). And related to this article, it's more greppable/awkable, e.g. `awk /^Objects:/ && $2 > 10`.
- ajuc 2y agoFun fact - I had to localize this kind of logic to my language (Polish). I realized quickly it's fucked up. This is roughly the logic: function strFromNumOfObjects(n) { if (n === 1) { return "obiekt"; } let last_digit = (n%10); let penultimate_digit = Math.trunc((n%100)/10); if ((penultimate_digit == 0 || penultimate_digit >= 2) && last_digit > 1 && last_digit <= 4) { return "obiekty"; } return "obiektów"; } Basically pluralizing words in Polish is a fizz-buzz problem :) In other Slavic languages it should be similar BTW
- RodgerTheGreat 2y agoone of the strangest and most grep-hostile approaches to identifiers that I have ever observed is Nim ignoring both case and underscores in an effort to allow everyone to write code in their preferred style: https://nim-lang.org/docs/manual.html#partial-caseminusinsensitivity_1 https://nim-lang.org/docs/manual.html#partial-caseminusinsen...
- uasi 2y agoNim even provides a dedicated grep-like tool to search for identifiers regardless of the style https://nim-lang.org/docs/nimgrep.html https://nim-lang.org/docs/nimgrep.html
- planetis 2y agoAnd it works pretty well, coming from 6+ years of experience. It's not that strange if consider case insensitive filesystems and email addresses. But on the internet you only hear the opinion of the loudest minority.
- dblotsky 2y agoHard agree with the idea of greppability, but hard disagree about keeping names the same across boundaries. I think the benefit of having one symbol exist in only one domain (e.g. “user_request” only showing up in the database-handling code, where it’s used 3 times, and not in the UI code, where it might’ve been used 30 times) reduces more cognitive load than is added by searching for 2 symbols instead of 1 common one.
- Noumenon72 2y agoNot to mention the readability hit from identifiers like foo.user_request in JavaScript, which triggers both linters and my own sense of language convention.
- emn13 2y agoBoth of those are easy to fix. You'll adapt quickly if you pick a different convention. Additionally, I find that in practice such "unusual" code is actually beneficial - it often makes it easy to see at a glance that the code is somehow in sync with some external spec. Especially when it comes to implicit usages such as in (de)serialization, noticing that quickly is quite valuable. I'd much rather trash every languages' coding conventions than use subtly different names for objects serialized and shared across languages. It's just a pain.
- plorkyeran 2y agoI’ve also found that I sometimes really like when I grep for a symbol and hit some mapping code. Just knowing that some value goes through a specific mapping layer and then is never mentioned again until the spot where it’s read often answers the question I had by itself, while without the mapping code there’d just be no occurrences of the symbol in the current code base and I’d have no clue which external source it’s coming from.
- runevault 2y agoProbably depends on how your system is structured. if you know you only want to look in the DB code, hopefully it is either all together or there is something about the folder naming pattern you can take advantage of when saying where to search to limit it. The upside to doing it this way is it makes your grepping more flexible by allowing you to either only search the one part of the codebase to see say DB code or see all the DB and UI things using the concept.
- ajayvk 2y agoJust spent an hour trying to figure out how a Hugo theme was picking up a shortcode definition. Grep did not help. Turned out the shortcode name is based on the file name rather than file contents.
- db48x 2y agoRust and Javascript and Lisp all get extra points because they put a keyword in front of every function definition. Searching for “fn doTheThing” or “defun do-the-thing” ensures that you find the actual definition. Meanwhile C lacks any such keyword, so the best you can do is search for the name. That gets you a sea of callers with the declarations and definitions mixed in. Some C coding conventions have you split the definition into two lines, first the return type on a line followed by a second line that starts with the function name. It looks ugly, but at least you can search for “^doTheThing” to find just the definition(s).
- CGamesPlay 2y agoNot JavaScript. Cool kids never write “function” any more, it’s all arrow functions. You can search for const, which will typically work, but not always (could be a let, var, or multi-const intializer).
- lispisok 2y agoAm I the only one who hates arrow functions?
- spartanatreyu 2y agoI did, until I used them enough where I saw where they were useful. The bad examples of arrow functions I saw initially were of: 1. Devs trying to mix them in with OOP code as a bandaid over OOP headahes (e.g. bind/this) instead of just not using OOP in the first place. 2. Devs trying to stick functional programming everywhere because they had seen a trivial example where a `.map()` made more semantic sense than a for/for-in/for-of loop. Despite the fact that for/for-in/for-of loops were easier to read for anything non-trivial and also had better performance because you had access to the `break`, `continue` and `return` keywords.
- mewpmewp2 2y agoAnother benefit of using for instead of array fns is that it is easy to add await keyword should the fn become async. But many teams will have it as a rule to always use array fns.
- recursivecaveat 2y agoA small, but underappreciated benefit of grammar changes like from the form `mytype myfun()` to `keyword myfun() sigil mytype`
- binary132 2y agoThis can even be as simple as not using multi-line error strings, or expanding variables in them.
- mashlol 2y agoGreppable commit messages and descriptions are also important, for a similar reason. If you want to learn where a feature exists in the codebase, searching the commits for where it was added is often easier than trying to grep through the codebase to find it. Once you've found the commit or even a nearby commit, it's much easier to find the rest.
- WalterBright 2y agoThat's why D has a cast keyword: ubyte c = cast(ubyte)i; instead of: unsigned char c = (unsigned char)i; Casts are a blunt instrument that subvert the type system, and so they need to be greppable. Having the cast keyword also removes the grammatical ambiguities in the expression syntax.
- jenadine 2y agoDo you often grep for casts? I never do that.
- aa-jv 2y agoTry to think about why you might want to do that. It makes a lot of sense, but if you're not doing it, that might be enlightening...
- WalterBright 2y agoI regard every cast as a bug in my own code and try to refactor it so there aren't any. I can't get rid of all of them, but they're always worth a second look. I don't normally grep for them, but others have told me they did. P.S. one thing about D is you can do things like this: ubyte b = i; // error, losing bits ubyte b = cast(ubyte)i; // ugly cast ubyte b = i & 0xFF; // no cast, no error! It's just one of the nice little details that making programming in D a pleasure.
- Too 2y agoThese things are better checked automatically with a static linter, that presumably already has a full parser and AST representation of the language and knows what is a cast and not. Nobody should keep a checklist of "100 things to grep for when doing a code review".
- SnowflakeOnIce 2y agoWhen doing appsec review in C or C++, yes!
- 2y ago
- jackphilson 2y agoI wonder - why isn't this talked about more? We have had tens of thousands of software companies, each with probably a dozen people focused on hyperoptimizing everything. Why hasn't this point been talked about more on the internet to the point where it's obvious today? And it's not specifically about this, it's more in general. Do people just learn this on their own, and not say anything? Or is the discussion related to this topic buried in some old forum somewhere?
- throwaway984393 2y ago[dead]
- mrkeen 2y agoIt's talked about, just in the opposite direction. I've left hardcoded strings (think Kafka event type names) in my source for this very reason, but after a round of code review they get squirreled away as constants in separate files because string repetition is bad or something.
- jimmaswell 2y agoWithout constants, it's too easy to let a typo sneak in or have inconvenience later replacing one "event" but not replacing an unrelated "event". I'll only do it if the string is used two times at most, but usually I'll make a constant the first time and it doesn't feel like any loss.
- mrkeen 2y agoYes, this is exactly what I was fighting against. If I have three classes that interact with "MyTable", then I can grep for places that interact with "MyTable" and I get back three classes. After refactoring, the class which now knows about "MyTable" is Constants.java, which has no business knowing about "MyTable". Grepping it now turns up a false-positive and finds 0 of the actual usage sites (3 false-negatives).
- 2y ago
- amingilani 2y agoI agree that code searchability is a good thing but I disagree with those examples. They intentionally increase the chance of errors. Maybe there’s an alternative way to achieve what the author set out but increasing searchability at the cost of increasing brittleness isn’t it for me. In this example: const getTableName = (addressType: 'shipping' | 'billing') => { return `${addressType}_addresses` } The input string and output are coupled. If you add string conditionals as the author did, you introduce the chance of a mismatch between the input and output. const getTableName = (addressType: 'shipping' | 'billing') => { if (addressType === 'shipping') { return 'shipping_addresses' } if (addressType === 'billing') { return 'billing_addresses' } throw new TypeError('addressType must be billing or shipping') } Similarly, flattening dictionaries for readability introduces the chance of a random typo making our lives hell. A single typo in the repetitions below will be awful. { "auth.login.title": "Login", "auth.login.emailLabel": "Email", "auth.login.passwordLabel": "Password", "auth.register.title": "Login", "auth.register.emailLabel": "Email", "auth.register.passwordLabel": "Password", } Typos aren’t unlikely. In a codebase I work with, we have a perpetually open ticket about how ARTISTS is mistyped as ATRISTS in a similarly flat enum. The issue can’t be solved easily because the enum is now copied across several codebases. But the ticket has a counter for the number of developers that independently discovered the bug and it’s in the mid two digits.
- Noumenon72 2y agoTypos are find-and-fix-once, while unsearchability is a maintenance burden forever. I don't think coupling variable names by making sure they contain the same strings is the best way to show they're related, compared to an actual map from address type to table name. There might be a lot of things called 'shipping' in my app, only some of which are coupled to `shipping_addresses`. Shouldn't a linter be able to catch that there is no enum member called MyEnum.ATRISTS, or is it not an actual enum?
- ctxc 2y agoAgree with you. What happens when translation files get too big and you want to split and send only relevant parts? Like send only auth keys when user is unauthenticated? `return translations[auth][login]` is no longer possible. Or just imagine you want to iterate through `auth` keys. _shudders_
- semiinfinitely 2y agowhy two 'p's - grep only has one
- wging 2y agoEnglish inserts an additional ‘p’ in some cases; for precedent consider “stoppable”, “unflappable”, “skippable”. See https://english.stackexchange.com/questions/30001/why-is-shippable-spelled-with-2-ps https://english.stackexchange.com/questions/30001/why-is-shi...
- Hackbraten 2y agoSee also: https://en.wikipedia.org/wiki/Digraph_(orthography)#Double_letters https://en.wikipedia.org/wiki/Digraph_(orthography)#Double_l...
- deleted 2y ago[deleted]
- mckn1ght 2y agoThat’s usually how it’s done with words that end in one consonant when adding a suffix that starts with a vowel, so as not to change the pronunciation of the short vowel in the root word due to english’s rules around long and short vowels. See also map->mapped, bat->batted, tap->tappable etc
- throwaway984393 2y ago[dead]
- loeg 2y agoYeah, this is also a benefit of e.g. C identifiers vs C++, where namespace, class, and method/variable can all be listed in separate places, breaking the ability to locate non-unique method/variable names with grep.
- jongjong 2y agoThis is a great point. One of my pet peeves is seeing an error in the logs which I cannot find in the code for various reasons. Sometimes the error message is constructed in a complicated way with variables concatenated together or the error message is extremely generic and I get matches in 100 different places. I'm an advocate for the idea that any aspect of a system which communicates either with end users or with sysadmins should be given high exposure in the code base. Typically, this means constructing abstractions in such a way that higher-level business logic and log messages are easily traceable from a single file. I make it so that the business layer sits above all other layers, as close to the program's entry point as possible.
- rezaprima 2y agoI had been bitten by ruby's metaprogramming on this.
- elijahbenizzy 2y agoHeh, this was very much the design philosophy behind Hamilton (github.com/dagworks-inc/hamilton). The basic idea was that if you have a data artifact (columns for dataframes initially), you should be able to ctrl-f and find it in your codebase. 1:1 mapping of data -> function. People take a long time to figure out that the readability gains from having greppability is worth whatever verbosity that comes, largely because they think of code too much as a craft (make it as small/neat as possible) and not documentation for a live process...
- j45 2y agoCode that is not written for others in the future may have a limited future
- lucumo 2y agoGrepping for symbols like function names and class names feels so anemic compared to using a tool that has a syntactic understanding of the code. Just "go to definition" and "find usages" alone reduce the need for text search enormously. For the past decade-plus I have mostly only searched for user facing strings. Those have the advantage of being longer, so are more easily searched. Honestly, posts like this sound like the author needs to invest some time in learning about better tools for his language. A good IDE alone will save you so much time.
- wglb 2y agoA bit on the other side of the argument, I use grep plus find plus some shell work to do source code analysis for security reviews. grep doesn't really understand the syntax of languages, and that is mostly OK. I've used this technique on auditing many code bases including the C family, perl, Visual Basic, C# and SQL. With this sort of tool, I don't need to look for language-particular parsers--so long as the source is in a text file, this works well.
- umvi 2y agoInterface-heavy languages break IDEs. In .NET at least, "go to definition" jumps you to the interface definition which you probably aren't interested in (vs. the specific implementation you are trying to dig into). Also with .NET specifically XAML breaks IDE traceability as well.
- ilrwbwrkhv 2y agoI tried a good IDE recently: Jetbrains IntelliJ and Webstorm. Considered the topdog of IDEs. Was working on a typescript project which uses npm link to symlink another local project into the node_modules of current project. The great IDEs IntelliJ and Webstorm stopped autosuggesting completions from the symlinked project. Open up Sublime Text again. Worked perfectly. That is why Jetbrains and their behemoth IDEs are utter shite. Write your code to have symmetry and make it easy to grep.
- 71bw 2y ago>I tried a good IDE recently: Jetbrains IntelliJ Having dealt with IntelliJ for 3 years due to education stuff - I laughed out here. Even VS is better than ideaj.
- mrb 2y agoI will always remember my professor explaining that greppability is the reason C++ casting operators use a long keyword: static_cast<...> const_cast<...>, etc as you can easily grep for "_cast" or the whole keyword.
- deleted 2y ago[deleted]
- whirlwin 2y agoCode grepping at build time can be useful. Grepping at at runtime, if you can call it that, is also very powerful. If you have a binary, either your company or a third party one, but don't have the source code easily available, I have used the `strings` program from GNU binutils which shows tokens in binary code, e.g. hardcoded URLs, credentials and so on. It can also be useful for analyzing certain things in memory.
- jonathanyc 2y agoRelated—“Too DRY - The Grep Test” by Jamie Wong: http://jamie-wong.com/2013/07/12/grep-test/ http://jamie-wong.com/2013/07/12/grep-test/
- IshKebab 2y agoThis is why I always recommend avoiding kebab-case as much as possible. You'll eventually need to convert it to snake_case and now you have broken grep. (Nobody is going to remember to use a regex every time.)
- creesch 2y agoI fully understand the point the author is making. However, I am not going to sacrifice good JSON and make it flat just so someone can search for it more easily. With the example they give, it is still readable because it is a simple data structure. But with more complex data their flat structure to me does not make it easy to parse and easier to make mistakes as well.
- smartmic 2y agoIt's ofter a matter of having the right tool for the job. In your case, https://github.com/tomnomnom/gron https://github.com/tomnomnom/gron might be useful.
- creesch 2y agoWell, I'd say that in the author's case it might be more useful. ;) I never really had the inclination to grep for data like the author does. I generally work from an IDE anyway, where it is clear that I am working with a value that is part of a JSON object and I can follow it back to the proper structure anyway. In fact, the more I think about it, the more I feel like the article is written for a very specific use case and perspective. Almost to the point where the saying "if all you have is a hammer, everything looks like a nail" is applicable. Where if it doesn't look enough like a nail it should be adjusted to look more like one instead of expanding your toolbox a bit.
- romybrielle1 2y ago[dead]
- shahzaibmushtaq 2y agoThis reminds me of the good practices and guidelines in coding when I was learning to code, which also includes "proper naming" so you can easily find what you are looking for throughout the codebase.
- berkes 2y agoMe too. But that's also what makes me uncomfortable when reading this article. Proper Naming is truly an "art" of balancing trade-offs. It takes domain expertise (Ubiquitous language), understanding of the users of the code (other devs, not end-users), and a lifetime of coding f*ups where naming something wrong turned out painful to balance these. The author gives a nice example of a dynamic table naming. But their refactoring didn't keep the behaviour the same (the else/catch). So it's hard to argue the first is better. And in this case, even without the else/catch, I'd say the latter is better. But there will be cases where greppability is to balanced with readability, testability or refactorability. And in these cases, for me, greppability comes last.
- emblaegh 2y agoPython people should think twice before implementing a `__call__` method if they want to improve greppability.
- VoxPelli 2y agoI advocate for greppability as well – and in Swedish it becomes extra fun – as the equivalent phrase in Swedish becomes "grep-bar" or "grep-barhet" and those are actual words in Swedish – "greppbar" roughly means "understandable", "greppbarhet" roughly means "the possibility to understand"
- mettamage 2y agogreppbarhet Grijpbaarheid I never saw grep as grijp I guess I do now (Dutch btw)
- elygre 2y agoCould I suggest that greppbarhet is more precisely translated as “the ability of being understood”? (Norwegian here. Our languages are similar, but we miss this one.)
- psychoslave 2y agoSo, at the extrem opposite of the esoteric "general regular expression print" that grep stands for with few ever knowing it?
- johncoltrane 2y agos/general/global
- medstrom 2y agoNorwegian still translates grep as "grip"/"grab". I always thought of grepping as reaching in with a hand into the text and grabbing lines. That association is close at hand (insert lame chuckle) for German and English speakers too.
- pbhjpbhj 2y agoIn English that association is going to depend a lot on one's accent; until now I've never associated grep-ing with anything other than using grep! (But, equally, that might just be a me thing.)
- anordal 2y agoSetting a variable by split identifier is surprisingly common in CMake (because functions can't return a value): > set(${VAR}_VERSION ${VERSION}) This is the main reason I don't like CMake.
- nsonha 2y agois there something like an universal "semantic grep" for code? I think rating code based on some (limitation of a) tool, might not be the best way.
- traxys 2y agoI read parts of the Linux kernel source code pretty often, and getting the definition of a function is often pretty involved: - I don't always know the return code type, as the calling code assigned a field whose definition I don't know to find either - I don't know if it's a C function or a preprocessor macro This often results in me searching for the exact function name, and combing through the uses in the drivers. You then need to re-start all that recursively to understand the function you just read. I could use clangd for that, but I don't have the ressources on my laptop to compile a kernel
- dvh 2y agoWhy not simply hold Ctrl and click on the name of the function?
- GeneralMayhem 2y ago> I don't have the resources on my laptop to compile a kernel
- gregjor 2y agoctags?
- trussy 2y agoYou might find this site useful: https://elixir.bootlin.com https://elixir.bootlin.com
- hcfman 2y ago.* is your friend :)
- larsrc 2y agoAs an avid grepper, I disagree with most of these specific recommendations. Use a tool that actual understands references. Don't make the code harder to read for humans just to please grep. As for identifiers, use 'foo.?bar' case-insensitively.
- medstrom 2y agoWhich of the examples are harder to read for humans?
- alkonaut 2y agoThere are of course cases of dynamic data in every language (The table name is an apt example) but usually when I look in code I just expect to be able to follow definitions. If the language doesn't reliably allow me to find "usages of this type" without risking finding another type with the exact same name then I'm already starting up my static type system compiler for the rewrite. There are exceptions of course: when searching git logs, comments etc doesn't help what the language or IDE does. And when searching for an unknown symbol (type, function, variable) you don't know the name of, but you know _should_ look like "DogOrder" or "OrderDog" is a common task too. In this case I'd probably search for " Dog.Order\(" or " Order.Dog\(" if I'm looking for a function. The language trait that enabled it is that method names are Pascal Case and always have an opening ( at the end. But my IDE at least lets me search for members (variables, functions) separate from type names. There should be an index in the IDE though that lets you query this data. E.g. looking for types starting with foo could be done with search t:Foo, instead of having to grep for "(struct|class) Foo" or similar. Tooling is the key.
- berkes 2y agoThe author uses JavaScript and Python as examples. So I presume they have (most?) experience with dynamic languages. In static languages, greppability is hardly as much as a factor. Especially with the availability of LSPs and other such tools nowadays. When I write rust, or Java, I hardly grep, I "go to usages" or "go to definition", "rename symbol" and so on. Similar, but not to that extent, with typescript. But when coding in Javascript, Ruby or Python, no matter how fancy or language-focused an IDE is, I'll be grepping a lot. Decades of Ruby and Rails "black magic" taught me to grep for partial patterns like the author shows, too. Or to just run the code-path entirely (through tests) because the table-definition of the database will change the available methods and behaviour of the code. Yes. I know. An LSP (or linter, or checker) can only do so much when the available code, methods, classes, behaviour can be changed or added at runtime.
- LegionMammal978 2y agoThen again, in languages like Java that go all-in on object orientation and dynamic dispatch, "go to definition" can become a real chore in sufficiently large codebases. I must have wasted hours of my life trying to find which class is the one implementing some interface or abstract method at a certain point. Bonus points if the implementing class changes based on ordinary runtime conditions.
- atoav 2y agoOne very simple way to make code less greppable is to use only single leter variables or other short variables that are very likely to be contained in a ton of other words.
- ljsprague 2y agoIn other words: don't try to be clever?
- kmarc 2y agoSome good recommendations in the article. Greppability is also helpful when you start scripting your editor. Vim has `includeexpr` and co. to implement some "intelligence" when trying to find declarations etc. This enabled me to write a couple line snippet that immediately could resolve Bazel starlark symbols even in "imported" (`load()`) files. At one point I realized I have better code navigation than any of my colleagues using IDEs. This, and tools like ripgrep really help a lot. This is something that VS Code developers also realized when indlcluded ripgrep itself as their "backend" of searching in files.
- vijucat 2y agoOne other thing I'd like to add is greppable comments! In the same vein as TODO and FIXME, I use hashtags in comments to drop hints to future me reading the code. #learning is a universal one: // #learning: transparent color using color.new(color.white, 100). This is GREAT for hiding plot() lines during inapplicable periods (such as when no trade is on) But project-specific hashtags are quite useful, too. // #60within600: bunch API calls to not hit the 60 calls within 10 minutes limit // This memoizes fn call results to prevent #60within600 The hashtagging was inspired long ago by del.icio.us, if you remember that. https://en.wikipedia.org/wiki/Delicious_(website) https://en.wikipedia.org/wiki/Delicious_(website)
- philipwhiuk 2y agoSeems like you're trying to implement a ticketing system in your code to me. If you need to prevent 60 within 600, write a test.
- vijucat 2y agoI'm documenting the (scattered) implementation changes related to the feature / bug-fix, in both code and test cases, using the hashtag. You could just use the JIRA ticket number, too, but it's a bit dry.
- TeMPOraL 2y agoGrep is indeed a critical tool for navigating and understanding an unfamiliar codebase, but greppability should not be a goal unto itself. The article seems to be making that mistake - it's basically advocating improving greppability at the cost of making the codebase even larger, messier, and harder to read: i.e. reinforcing the problem that makes you reach for grep in the first place. It's a false economy. It's asking you to optimize your code for one specific scenario - trying to figure out where an unfamiliar string comes from; but that isn't the most important or most frequent thing people need to do with code anyway. (If it is for you, congratulations, you're the janitor in the codebase. It sucks, but that's what you're being paid for. Maintenance is a means, not an end.) In particular, one of the most important and frequent thing you do with code is read it in order to understand it (locally, at the abstraction level of interest), and the advice from this article compromise it badly - almost as if hoping that, on a greppable enough codebase, you could use grep to avoid reading or thinking entirely. 1. Don't split up identifiers Don't split them up for the sake of splitting, sure. That's not helping anything. But in the example given, there's likely a good reason for it - for example, it codifies the intended coupling between tables. `billing_address` isn't an independent term in this code, nor are the other `_address` table names. There's a naming pattern there, encoded directly in the initial example. The proposed refactor obscures it and triples the amount of code in the process (all of which is low-value noise) and introduces possibility of making errors (typos, copy-paste) of the kind that isn't picked up by compilers (hope you have good tests!). FWIW, the author's refactor may be eventually required - if and when the naming pattern in the original code no longer holds. But not before then. 2. Use the same names for things across the stack Excessive data repackaging is bad, but that tends to be a symptom of having too many layers. A good layer has specific semantics that distinguish it from layers above and below it. This may necessitate renaming some thing, in which case even if such renaming is as trivial as in the example, it should be spelled out explicitly; you can't just return Layer 1 Address object instead of Layer 3 Address object, if the two layers mean something different by "Address"; the triviality of the mapping is incidental and may not hold over time. If it really feels trivial, chances are one of the layers is not necessary in the first place, so go fix that. 3. Flat is better than nested Now that's just screwing with people, especially wrt. nesting namespaces. It's asking to reintroduce the visual noise that the person reading the code will then have to filter out again mentally. The way I see it, if you grep for some log message or unrolled identifier and can't find it, you're supposed to keep grepping for parts of the string, until you hit a match. You then go look, and it's usually apparent that you're dealing with a compound identifier or an interpolated string - congratulations, you just learned something important about that part of the legacy codebase, which is the real job you're supposed to be doing.
- noufalibrahim 2y agoI don't know how to validate this but this seems to be a specific case of "avoiding magic" where there's a lot of dynamically generated variables and things. Having the static text of the program more or less show its intent helps readability and searchability quite a bit. I suppose the other extreme is to have a program generator with an input spec and you being left to read through the generated code without access to the input spec.
- moomin 2y agoIf you really do want your code to be searchable, here’s a couple of practices I’ve adopted: 1) Eliminate spelling mistakes. Eliminate alternative spellings. UK vs US English? Pick a side and stick to it. 2) Eliminate contractions. Or keep a very short list of allowable ones (We permit “info” for instance.) The point of this is to increase the predictability of the names you use. If you’ve got “tradeable” and “tradable” in your code base, search for it is going to be a pain. You can supplement these rules with common coding standards like “We call these things providers.” but just getting the spelling consistent is huge.
- peanut-walrus 2y agoThese are all extremely good suggestions. Especially the flattening bit - yes, it's verbose as hell, but it just makes so much sense whenever you have to deal with the code any time after writing it. Helm charts, please take note, the docs even say that "In most cases, flat should be favored over nested.", yet almost every time I have to deal with a Helm chart, it's a mess of nested structures.
- qwertox 2y agoI use greppable strings explicitly, like requests.get(f'http://a.b.c.d/wol?device={wol_computer}&grep-id=wake-on-lan', timeout=3) This way I find `grep-id` in the server logs as a reminder of what to grep for, then `grep-id=wake-on-lan` in the entire codebase to find the actual source of the call. Or I add comments with a grepable token to the code.
- arendtio 2y agoI am firmly against the suggested changes. I love grepping through code too (often using -A -B -C), but I also like browsing the code, with tools where you can just click on a function and see its definition. However, changing how the code should be written so that grepping becomes easier is optimizing for the wrong target. It is much more important that the code is easily readable and maintainable. In addition, some tools are designed explicitly for grepping through code (from the top of my head ack is an example). If grep doesn't work, one should try a more sophisticated tool instead of using different coding styles.
- gregjor 2y agoNothing the author wrote would necessarily make code harder to read or maintain. Consistent naming of the same thing throughout, not constructing variables or table names dynamically, etc. benefit both readers/maintainers and searching. I understood “grepping” to mean ripgrep (rg) or ack, not just plain grep. I think programmers who use command line tools or vim know about those. VSCode uses rg.
- lucideer 2y agoGreppability is really a proxy metric here - these changes all have other benefits even if you never grep (mostly readability tbh). const getTableName = (addressType: 'shipping' | 'billing') => { return `${addressType}_addresses` } This is a simplified example but in a longer function, readability of the `return` lines would be improved as the reader wouldn't have to reference the union type (which may or may not be defined in the signature). The rewrite is also safer as it errors out if a runtime `addressType` value doesn't match the union type (above code would not throw an error, just return an indeterminate value which would cause undefined behaviour). "Flat is better than nested" also greatly improves readability in both examples: either reading the i18n line, or reading the classname at definition / call will be more readable when the name contains full context of function.
- deleted 2y ago[deleted]
- Mikhail_Edoshin 2y agoConceptually it is akin to having file names that sort well. Grep is a simple tool, not too different from a simple string sort. It is better than no tool, but is it better than a tool that understands the notation? A strong side of grep is that it is universal and is not tied to a particular notation. Yet if you could easily define a specific notation and have a tool to immediately understand it, would you still prefer grep? We tend to organize the code according to the tools we have. E.g. if a tool gives us a list of entities in alphabetic order, we will try to name the entities so that they form “logical” groups. This may pass as a local organizational principle and may be useful but it is always intimately coupled with the underlying tool.
- eterevsky 2y agoThis also applies to dependency injection. While it has significant benefits, it hurts clarity of the code. It becomes more difficult to see where each object is coming from.
- mrkeen 2y agoMagical dependency injection frameworks, that is. Plain old putting-dependencies-in-the-constructor-instead-of-newing-them is great. If you 'wire' it yourself, you see the top-level structure of the project in main, e.g. cache <- createCache "./cache" workQueue <- createWorkQueue parallelism projectFinder <- createProjectFinder basePath gradleBuilder <- createGradleBuilder cache normaliser <- createNormaliser gradleParser <- createGradleParser normaliser relationFinder <- createRelationFinder cache normaliser At a glance I can see what uses normaliser, and what is used by normaliser.
- kgeist 2y agoWe've ditched "magical dependency injection frameworks" for manual injections in constructors and the net result is that development is actually faster. Yes, you write slightly more code, but at the same time, it's much easier to figure out what's going on. Before, if something went wrong, we would get some arcane errors from deep inside the DI framework and had to decypher what it means and what changes we need to apply to the configs. Now, it's just regular code (great with refactorings and greppability, too).
- skrebbel 2y agoThe second point here made me realize that it'd be super useful for a grep tool to have a "super case insensitive" mode which expands a search for, say, "FooBar|first_name" to something like /foo[-_]?bar|first[-_]?name/i, so that any camel/snake/pascal/kebab/etc case will match. In fact, I struggle to come up with situations where that wouldn't be a great default.
- crazygringo 2y agoAdding to that, I'm often bitten trying to search for user strings because they're split across lines to adhere to 80 characters. So if I'm trying to locate the error message "because the disk is full" but it's in the code as: ... + " because the " + "disk is full") then it will fail. So really, combining both our use cases, what would be great is to simply search for a given case-insensitive alphanumeric string in files that skips all non-alphanumeric characters. So if I search for: Foobar2 it would match all of: FooBar2 foo_bar[2] "Foo " + \ ("bar 2") foo.bar.2 And then in the search results, even if you get some accidental hits, you can be happy knowing that you didn't miss anything.
- lathiat 2y agoThese are both of the problems I regularly have. The first one I immediately saw when reading the title of this submissionw as the "super case insensitive" that I often see when working on Go Codebases particularly when using a combination of Go Classes and YAML or JSON. Also happens with command line arguments being converted to variables. But the string split thing you mentioned happens a lot when searching for OpenStack error messages in Python that is often split across lines like you showed. My current solution is to randomly shift what I'm searching for, or try pick the most unique line.
- WizardClickBoy 2y agoThis reminds me of the substitution mode of Tim Pope's amazing vim plugin [abolish](https://github.com/tpope/vim-abolish?tab=readme-ov-file#substitution https://github.com/tpope/vim-abolish?tab=readme-ov-file#subs...) Basically in vim to substitute text you'd usually do something with :substitute (or :s), like: :%s/textToSubstitute/replacementText/g ...and have to add a pattern for each differently-cased version of the text. With the :Subvert command (or :S) you can do all three at once, while maintaining the casing for each replacement. So this: textToSubstitute TextToSubstitute texttosubstitute :%S/textToSubstitute/replacementText/g ...results in: replacementText ReplacementText replacementtext
- irfan786 2y ago[flagged]
- t43562 2y agoastgrep is a very useful tool when grep fails: https://ast-grep.github.io/ https://ast-grep.github.io/ It's not as easy to use as grep but I think one can script it to be nearly so. It has huge power but without learning it all one can do searches that grep finds difficult. e.g. finding all the locations where a method is called and showing the parameters even if they are on multiple lines. You can then use rewrite rules to do CLI code refactoring. I think it also has potential in a build toolchain e.g. to look for patterns you want to discourage as a pre-commit hook. ultragrep - https://github.com/zendesk/ultragrep https://github.com/zendesk/ultragrep - I don't love this quite as much but it does have a way to build indexes so you can do fast greps across a big codebase. It also has a text mode UI if you want it and I find that almost worthwhile. I use ripgrep most of the time but while I like it, there is a limit to how many grep tools I can remember and I should probably cut down to using ultragrep and astgrep. plain gnu grep itself is something one has to know when one is on an unfamiliar machine.
- leetrout 2y agoI encourage my teams to write logs / output with interpolation with the variables at the end for searchability For example: Added %d users Vs: Added users (%d) Then it is much easier to track down where things come from without needing wildcards in the search or to care too much about what might be dynamic in cases where its not obvious.
- davemp 2y agoI’ve basically landed on the following form: ‘Short description. [foo={}, bar={}]’ Which will give you grepability and in theory parsability so you can automatically bisect for a value change or something along those lines.
- jgrahamc 2y agoIn my very first job I wrote a spell checker/corrector for code comments. This was specifically to make greppability possible because some of my colleagues were appalling at spelling and it meant that the incredibly detailed comments we used to write were hard to search for key details.
- assanineass 2y agoSounds a little erotic…
- wrsh07 2y agoNice article. Two notes: First, some of these suggestions will make it harder to introduce bugs when updating the code. That's good! Particularly tricky is when somebody splits up identifiers or function names. These types of things often occur at boundaries (calls between servers or to the db) which can make them tricky to test. Even if all your identifier combining is initially done in a single file, it's easy for someone to see the final shape of the identifier and accidentally hard code it somewhere else. Second, In the spirit of Titus Winters' "software engineering is programming over time", a codebase should be greppable over time. That means that if you rename a function, you might consider saving the old name of the function in a comment.
- hugodan 2y agoTry to keep code reference indirections to a tasteful minimum. If a split is needed that’s one more indirection for whoever will be maintaining it in the future. This weight needs to be on the table. Keeping things referentially transparent helps a lot here.
- eitland 2y agoWorking in spring, which accepts I don't know how many formats from ENV_VARS to yaml, this very much resonates with me, because as a general rule, if one can use a certain option, someone will do it. Also the reason why I try to avoid Gradle when possible: The possibilities are endless. At one place I think I found 21 wildly different Gradle configs out of 24 that I checked. (For anyone that wonders, it was combinations of: - placeholders vs straightforward depency (this is a thing in maven too) - for loops doing things based on lists or maps instead of just calmly declaring them one after another, maybe to save some characters - helper functions so you could declare dependencies like azure(<something>(<version>)) - order of declarations - Kotlin vs Groovy syntax I have probably forgotten a couple more but this is thankfully already a few years ago.)
- r34 2y agoGood point. I would refer to another (similar) metric, which could be called "IDE-search-ability): it extends greppability, by adding some more conventions which work well with your (your company's) IDE.
- indymike 2y agoGreppability adn debugability are two things that I look for in code reviews. If you ask, "How would you debug that?" and the answer stats with, "I'd rewite it to..." Maybe, just maybe you should write it that way.
- 0x69420 2y agosure. the reason i put a line break between return type and function name in c-likes is `grep ^fname`. but i seriously wish greppability wasn't important. the extensive line-orientedness of unix tools really puts a damper on the whole hose-of-bytes concept, and it's no wonder by the time of plan 9, there was a strong desire to do away with it—cf. "structural regular expressions", as deployed in sam(1), which, of all the places to put them, certainly has historical irony, as sam's (decidedly not line-oriented) editing language nonetheless descends from ed, the definitive line editor, and gave us such hits as "stream ed" and "simulate typing `g/regex/p` into ed". just the other week i noticed a change in recommended formatting style in a project i contribute to regularly, and the result was source files got about 20% taller, 20% more of a pain in the ass to edit without some sort of syntax folding. the rationale? diff. making you reach for a syntax-aware editor to compensate for a deficiency in the syntax-awareness of a version control frontend is certainly a choice. the business end of git as seen by most programmers is in fact diff city, sure, but deep down git is a bunch of snapshots. even deltas behave nothing like diffs. pull up the spec for the pack format and look for the word "line". you will not find it. things could be so much better, but for now we live in a world where the headline is true.
- nickjj 2y agoAbsolutely. It's what I like about Rails when it comes to file names too. Having controllers/users_controller.rb as a path might sound wasteful because "you're already in the controllers directory, you don't need _controllers in the path". But when you want to fuzzy find that file, it's really nice to type "users con" and get that file instead of also picking up views, models and other user related files with just a "users" search.
- trilbyglens 2y agoImo this is another big selling point of using tailwinds css. Those log stacks of classes become almost like UUIDs for markup that's discoverable from dev tools.
- ceritium 2y agoI built the command line tool flatito just for the Rails i18n translations keys. I am unsure if I like the author's approach because there are other cons, but it's a good point. * https://github.com/ceritium/flatito https://github.com/ceritium/flatito
- klysm 2y agoA good IDE makes up for this if the syntax of the language doesn’t lend itself to easy greppage. I lean heavily on JetBrains search with their editors
- frabjoused 2y agoThis is why I’ve always fought against BEM in CSS. Tends to drive greppability to zero.
- dwh452 2y agoThis sounds like the advice to prefer the variable name 'ii' over 'i' because you can easily search for it. I loath such advice because it causes the code to become ugly. Similarly, there are 'YODA Conditions' which make code hard to comprehend which solves an insignificant error that is easily caught with tooling. The problem with advice like these is you will encounter deranged developers that become obsessive about such things and make the code base ugly trying to implement dozens of style rules. Code should look good. Making a piece of text look good for other humans to comprehend I consider to be job #1 or #2 for a good developer.
- Aachen 2y ago> prefer the variable name 'ii' over 'i' Vim users don't have this issue, where you can * a variable name you're looking for and that'll enforce word boundaries. PHP developers also don't have this issue: the $ before is a pretty sure indicator you're working with a variable named i and not a random i in a word or comment somewhere Y'all should just use proper tools. No newfangled Rust and Netbeans that the kids love these days, the 80s had it all! (Note this is all in jest :) I do use vim and php but it's obviously not a reason to use a certain language or editor; I just wondered to myself why I don't have this problem and realised there's two reasons.)
- moolcool 2y ago> The problem with advice like these is you will encounter deranged developers that become obsessive about such things and make the code base ugly trying to implement dozens of style rules That's more of a "deranged developer" problem than a problem with the guidelines themselves. E.g. I think his `getTableName` example is quite sensible, but also one which some dogmatic engineers would flag and code-golf down to the one-liner.
- ajuc 2y ago> 'ii' over 'i' You don't need to search for local variables, nobody names global variables "i" - so the "ii" advice is pointless. You often do need to search for places where global stuff is referenced, and while IDEs can help with that - the same things that break grepability often break "find references" in IDE. For example if you dynamically construct function names to call, play with reflections, preproccessor, macros, etc. So it's a good advice to avoid these things. > you will encounter deranged developers that become obsessive about such things and make the code base ugly You can abuse any rule, including > Code should look good. and I'd argue the more general a rule is - the more likely it is to be abused. So I prefer specific rules like "don't construct identifiers dynamically" to general "be good" rules.
- poikroequ 2y agoGrep is nice, but I would much prefer better tools for searching through code. Something that knows how to parse multiple languages and can infer the types of things. Not to mention indexing, for large code bases, grep'ing through possibly millions of lines of code can be awfully slow. IDEs do a decent job but are typically lacking compared to the raw power of grep.
- packetlost 2y agoI mean, I prefer faster symbol, type, etc. based navigation too, but it doesn't work in all scenarios so grep is an extremely handy fallback.
- Timwi 2y agoAll of the apostrophes in this article are wrong. The correct character is ’, but this article uses ‘ (open single quote) throughout.
- riz_ 2y agoThanks, fixed.
- svennidal 2y agoThis approach along with ack, instead of grep, has been a godsend to me.
- pooriar 2y agoI just shared this in the work Slack and everyone resoundingly agreed with the sentiment. Definitely going to pay more attention to this now, thanks for sharing!
- trey-jones 2y agoAs someone who almost exclusively uses grep for finding what I need in codebases that are new to me and old to me, you can make whatever arbitrary rules you want, as long as you're consistent, I'll be pretty happy with it. If syntax is loose in some area (single vs double quotes, parens or braces or none), just do the same thing every time. Whitespace consistency isn't crucial, but it can't hurt (between function name and parens, for example).
- necrotic_comp 2y agoAgreed. So long as the code hits performance and business goals, there doesn't need to be an emphasis put on "newness" or any other sort of vanity metric - make the code obvious, searchable, and understandable so that in a time crunch or during an outage it's easy to search and find the culprit.
- causal 2y agoI'm also thinking long-context LLMs are going to make this advice seem pretty archaic in a few years. They're so good at reading code and extremely useful for asking questions of a code base. That said, I completely agree with the author on not using clever string tricks to compose identifiers. That makes code both harder to search and to read.
- mannycalavera42 2y agoif only there was a language where code is data so that... hold on a sec! #LISP-languages
- guhcampos 2y agoOne situation that comes to mind is configuration of applications on containers using environment variables. It's extremely valuable to be able to just `grep -r PREFIX_` on a codebase and be able to visualize all possible configuration values for that application. This is encouraged by some frameworks like Django, where you are expected to list all the configuration values in a `settings` module, but is not standard for `viper`, `click` and `pydantic-settings`, which try to be too smart and auto-generate the variable names for you. It's one of these cases where "modern" frameworks and applications try to save a minuscule amount of work by automating some task, but end up reducing the maintainability of the code over time.
- Shorel 2y agoI use grep and git grep all the time. This post is very welcome, it sums up my own ideas about grep in a better way.
- jayd16 2y agoI'm a big proponent of visual scripting (where it makes sense) but you really do miss the text-based tooling like grep. One trade-off you can make is using text-based serialization so you're at least able to grep the yaml or JSON or whatever and get to the right file at least. This of course costs you some editor load time. On the flip side you're basically always using an IDE to edit the visual script. In theory symantic search should be possible and built in although reality usually falls short. Someone in a previous HN thread mentioned the idea of a standard graph syntax. Something that game engines and tools could store their graph-based assets in. If there was a standard syntax then standard tools could be made and we could end up seeing something like a graph grep. One one could imagine a visual studio graph editor type app with plug-in support. Even a standard merge tool would be a huge step up for non-text-based code assets. A man can dream!
- wizzwizz4 2y agoThere is a standard graph syntax: Graphviz DOT notation.
- jayd16 2y agoI'm not familiar with this but it seems more geared towards visually representing flow charts but it doesn't have the necessary verbosity for visual scripting. I might be wrong but I think this is only part of the puzzle. Can rules be defined such that, for example, a node port must be pointed to, or that a default value is available? Besides the declaration of a graph, there also needs to be a way to define semantics, ie something akin to a Language Server Protocol. That way tooling can enforce validity while editing a graph.
- wizzwizz4 2y agoDOT is not a schema language, no. It sounds like you're trying to make something new, in which case: I'd forego standardisation concerns for now, and focus on getting something that works, and where future prototyping can be backwards-compatible.
- nottorp 2y agoFunny, I started work on a legacy code base a couple months ago and yes, it has all the problems described in the article and that hinders our understanding of it.
- settsu 2y agoI'm ardently in favor of making code human readable as practically as possible. Personally it follows on my personal Rule #1: Be kind (i.e., in this case, to others and your future self.) However, searchable =/= greppable. > Flat is better than nested Context matters but, generally speaking, I would say that flatter is anti-grep.
- breck 2y agoThere's a new kind of language where the practice is to use whitespace, and only whitespace, as your syntax. Newlines separate blocks and spaces separate words. One of the unexpected extremely powerful things this allows is finding function usage extremely easy in any text editor that supports regex. You just search for ^[functionName] . Since you know that function pretty much will only be used at the beginning of lines. You can thus make edits against the AST with regexes and without parsing the AST at all. It's pretty amazing, and leads to quite faster development, and allows one to tackle bigger and more complex problems.
- medstrom 2y agoExample of such a language?
- welder 2y agoAlso import traceability
- hilux 2y agoDigital marketers have known this for a long time.
- HeavyStorm 2y agoOnly read the title but if it's what I guess it is, then I finally met someone who'll understand that I always declare functions in js using the keyword function.
- tlb 2y agoAn editor feature I'd like is that as I'm typing an identifier, a hover popup shows me how many other instances appear in my codebase. It should be easy to build a map of identifier->count for instant lookups. I generally know if I want 0 (a new unique identifier), 1, a small number, or a large number. For a few pixels, this would prevent a lot of dumb mistakes and ambiguous names.
- jgalt212 2y agoNot to pick on Black, as it happens with all code formatters, but max line length rules kill greppability. > Black defaults to 88 characters per line https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html https://black.readthedocs.io/en/stable/the_black_code_style/...
- linuxdude314 2y agoThe examples are pretty silly, especially the first one. If you know that you need either a shipping or billing address and the user has specified which one they need, just query based on that. There’s no need to introduce a function (getTableName) to detemplate a string or match on a case. Instead just create a function that gets the item you want from the DB and has the table name as input. On your UI make sure when the users specifies billing or shipping address the correct parameter is passed to the API.
- KronisLV 2y agoI work on a project where people decided to refer to translations by doing the equivalent of: :label="$translate(getProductSectionLabel('title'))" where the logic is a bit like: const getProductSectionLabel = (code) => `myapp.sales.sections.products.${code}` and then the actual values are in a nested structure, like: myapp: { sales: { sections: { products: { title: "Products" ... } ... } ... } ... } People seem to have gone for that because writing that first part is simpler within the component, but I couldn't get across that this makes the codebase harder to navigate. Meanwhile, my personal codebases are more like: :label"$translate('myapp-sales-products-title')" and the translation file also has the equivalent of: myapp-sales-products-title: "Products" which is way simpler at the expense of some more duplication (easily mitigated by compressing the translations).
- advael 2y agoFor my purposes, among the most important Even a major refactor is relatively easy if you can find stuff in your codebase. Even a small bugfix can get complicated if there's a ton of ambiguity
- Groxx 2y agoAlong similar lines: I highly recommend making every metric and log in your system spelled out completely somewhere. Don't: base = "abc" something = base + ".some.suffix" Do: something = "abc.some.suffix" I've also had some luck with hard-coded UUIDs at call sites, e.g.: log.Info("something", "callsite", "DECAFBAD-000...") because it makes it absolutely trivial to find a log, and unlike caller-lines (which are great! use them too!) it doesn't change when you refactor code.
- germandiago 2y agoThis is one of the top reasons why I prefer explicit over implicit.
- dang 2y agoYes, and 'greppable' is an underused word/concept in its own right. I've used this as an organizing principle since forever (https://news.ycombinator.com/item?id=1535916 https://news.ycombinator.com/item?id=1535916). It's one of the best ways to factor code that I know of.
- tptacek 2y agoIt's good, but I'm snagging on the examples here; for instance, the insistence on consistent use of camel or snake case. That seems to me to be purely a tooling limitation; a more code-aware grep could just automatically synthesize the covering regexp for both. It feels like there's a sweet spot in between LSP-ism and grep-ism that isn't being captured.
- tabbott 2y agoGreppability is the reason why I feel it's really quite unfortunate that HTML's dataset attribute went with "canonicalizing" to camel case; if you're using any non-camel-case data attribute names in your project, then they immediately become not fully greppable when making use of `dataset`. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement...
- peter_d_sherman 2y agoObservation: The more broad understanding here is that in computer programming there is a difference between a symbol and a referent (https://open.maricopa.edu/com110/chapter/3-1-language-and-meaning/#:~:text=The%20symbol%20is%20the%20word%20that%20represents%20the%20thought%2C%20and%20the%20referent%20is%20the%20object%20or%20idea%20to%20which%20the%20symbol%20refers https://open.maricopa.edu/com110/chapter/3-1-language-and-me....) AKA variable and its value. (Phrased colloquially: "The finger pointing at the Moon is not the Moon.") Information about a given computer program may be found in its symbols (variable names) and its referents (values that variables are set to). The referents may be set in source code (i.e. a string or other constant) and be visable/greppable at design-time -- and/or they may be set at runtime (and be non-greppable!) The article is good; I have nothing against it, it makes an excellent point. But if we look at what I've outlined above as three possible places for information (source symbol, source referent, runtime referent), then we observe that one of these possibilities is not covered by grep -- that is, runtime referent. This leads to the question: "What if grep could be used to search for runtime referents, aka runtime values of different variables?" Well, in traditional compiled languages, we can't do that without significant monkeying with the language... In interpreted and Lisp-like languages, yes, the above is possible -- but without being able to be very specific about the what/how/when of the runtime strings (or more broadly, values) to be grepped, doing the above could generate huge amounts of data and speed degradation, particularly if a value is set and reset multiple times in a long loop! But could it be done? Definitely! Efficiently? Well, that's one of the questions! Deterministically? If random values are in play, probably not. With full code coverage so that values can be known from code paths not taken can be known? That scenario would be challenging to say the least. Point is, it may be an interesting future language feature to consider in various scenarios, for future language designers... "The ability to grep through all runtime referents, aka all runtime values, of all variables, for a given runtime of a program..." Hmmm... now that I think about it, what if a language was created such that you could pass it a special flag, if you passed it this special flag, then variables would maintain their histories in memory (at great expense to performance -- this would be for debugging only!), and then you could grep that memory? Yes, I know... given the pace of technology, there probably is some system or systems that might implement some aspect or aspects of this... rr comes to mind: https://rr-project.org/ https://rr-project.org/ https://news.ycombinator.com/item?id=31617600 https://news.ycombinator.com/item?id=31617600 (Maybe the rr maintainers could be persuaded to implement some interface with grep, either pre or post program run, or maybe implment a timer where the user can grep their program for various strings at various time intervals...) Anyway, good article!
- mkoubaa 2y agoThis is why I try to avoid code reflection in cases where there's an alternative, even when that alternative is less elegant