7 ms·
> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian. I hate this so much. It means I can't grep for a constant.
by 4ad 4y ago
> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian.
I hate this so much. It means I can't grep for a constant.
- overengineer 4y ago1_?0_?00_?0_?500 yes it sucks
- elygre 4y agoThat’s not enough — the underscores can appear anywhere, so you need to cater for 500000_0 and 5_0_0_0_0 etc. basically an optional underscore between each digit.
- amalgamated_inc 4y agojust grep for `[0-9_]+`
- masklinn 4y agoI would assume they're looking for a specific constant value.
- gpderetta 4y agogrep '[0-9_]+'|sed 's/_//g'|grep <my-constant>
- amalgamated_inc 4y agoThat's kind of a handful to type every time :) I'd just do for the first one and then scan for the number I'm interested in. If you have more than a page full of constants..
- nicoburns 4y agoYou can make it an alias. No need to type it every time!
- turtledragonfly 4y agoWell you can, you just need a more complicated regex. Same for '1e6' and such.
- maxbond 4y agoIf you have constants repeated throughout the codebase, you can pull them into a constants file, and then you can go to that file, navigate to the definition of interest, and use your IDE of choice to find usages. You'll also be able to give these constants semantically significant names, and comment next to them providing derivations or citations. And of course, if it's a mistaken or outdated value, you can change it one place and apply it everywhere. Consider that, if you were debugging a problem with this constant, and the problem was caused by someone having made a typo in one of it's usages (eg having typed 1000500 instead of 10000500, a mistake that's more difficult to make of you have better ways to format numbers [did you have to look back and forth to find the mistake? I did]) - your regex would fail to find it, even if there were no ambiguity about the format it was written in.
- ludston 4y agoDepends on the size of your codebase and how many people work on it. Once you have 20 years of code written full time by 200 developers in your monolith, finding the constants file out of 400 different subsystem's constant files for a given subsystem that you've never seen before can become a legitimate and challenging pain.
- maxbond 4y agoTotally true. I don't know if IDEs commonly have a feature like, "find this value, regardless of how it's expressed" (even better yet, fuzzily, to catch typos), but I think that's the proper general solution. It's a good idea to consider a constants file earlyish, while everything still fits in your head. I'd say in the case of such a sprawling system, make a constants module, and it can have different files for different topics. But keep them all together. Code style is an engineering tool you can use to prevent problems. But I do understand this is cold comfort for those working on systems where the decision around this were made 15 years ago, and there's no possibility of refactoring the constants. That's quite annoying.
- CuriouslyC 4y agoThen you have a problem with having to update both the constants module and the service that depends on it to change the service, and you need to handle packaging and distributing the module. Maybe if you have a monorepo where those issues are moot...
- Smaug123 4y agoOut of interest, how often do you do this, and what are the semantics of the numbers you're grepping for? I literally can't remember a time I've ever tried to grep for a number.
- duped 4y agoI've done it for error codes but that was awfully cursed
- jbverschoor 4y agoBut you need to search for both hex and dec codes for that
- duped 4y agoYup which is still a one liner in grep
- jbverschoor 4y agoYou can grep the snake case too in one grep You can do almost anything in a grep
- 4ad 4y agoWhen I was doing bare metal programming I was doing this all then time, depending on what I was doing maybe even tens of times an hour. And it's not just source code either, these days even debugging tools print values in this way making it a total PITA to reverse engineer things because you can't easily match values coming in from different tools, or from the tool and source code, etc.
- AnimalMuppet 4y agoYou already can't, because someone can write the constant in hex, or even (shudder) octal.
- adrianmonk 4y agoOr like this: const int WAIT_TIME_MICROSECONDS = 42 * 1000 * 1000; // 42 seconds
- tmtvl 4y agoOr: 0x2A * 10e6; The solution is clearly to have language tooling with a find-constant tool which you give an expression and it parses all declarations in the source code looking for one which evaluates at compile time to the provided expression.
- dan-robertson 4y agoThe funny way I’ve seen this go wrong in practice is a typo like: // we don’t want more than 100m because … quota = 1000_000_000 Where people assume the underscores are in the expected place.
- dunham 4y agoThis is why I like the underscores, it's easier for me to see the typo in: quota = 1000_000_000 than it is in: quota = 1000000000 And also when I'm typing the number, it's easier for me to be sure I got it right when I can count the zeros in groups of three. It's rare that I've needed this, but I've used it in Java a few times in my career.
- nicoburns 4y agoWhen do you find yourself grepping for a constant (as opposed to it's name)?
- 4ad 4y agoI explained down in this thread.