1. 8

Hi Lobsters, I thought this might be an interesting poll:

In the language/environment/codebase that you’re working on at the moment, how easy is it to find all places where a variable might be used? If an exact answer is impossible, how easy is it to find the most likely set?

Does the language itself make it easy - ie, required type labels you can just grep for - or do you get there with IDEs or other tooling?

I’m also curious if this specific task never occurred to you, like if it’s a question you wouldn’t even need to ask in your environment.

Question courtesy of trying to reacquaint myself with some Python code I used to know…

  1.  

  2. 5

    I use Visual Studio for C# development, finding all references of a variable is as simple as placing the cursor on the variable and pressing shift+f12. The language itself does not facilitate easy grepping, though Omnisharp does give similar functionality to other editors (including vim).

    1. 3

      The refactoring and find-all-references is what makes C# development for me. It’s something that becomes so second-nature after a while that it hurts when I have to use something else.

      1. 2

        Same here, but I do wish I could easily see all places where an identifier is on the left hand side of an assignment.

      2. 5

        Lately I’ve been writing mostly in Swift using XCode (joined a project that’s in that language/environment), and it’s not easy, because this functionality is simply not supported. One stupid but effective hack: rename the variable to a dummy name and then recompile, which’ll give you an error everywhere the previous variable name was used. Don’t forget to rename back! :-)

        As an aside, this is my first foray into development in the Apple ecosystem in many many years, and I’m not super impressed (I’ve owned Macbooks, but only developed cross-platform Unix software on them). Traditionally, Apple had a reputation for at least two good things in their development ecosystem: 1) a coherent, consistent worldview and Appleish way of doing things, and 2) good, comprehensive technical documentation that gave not only bare information like function parameters, but also explained the design and the right way of doing things. So far I’m not finding this to be the case with their contemporary tools. XCode is not a good IDE, and the developer documentation for Swift is quite poor (especially the platform APIs, the core language documentation is ok).

        1. 3

          Interesting. I used to spend a lot of my “free time” in XCode, but stopped long before Swift came around.

          I’m not too surprised that the usability of Swift is lagging a bit, from my experience working with ObjC and Cocoa frameworks back around 2002-05, the documentation was pretty poor for a lot of their frameworks at first - the reputation you cite was won over years of effort on their part.

        2. 4

          I’m working on a WordPress theme and a couple of plugins for a university website, using SiteOrigin Page builder, Timber, and ACF.

          I have no fucking clue where half these variables are being used at the moment.

          Edit: To extrapolate, I would use xdebug to actually step through the build up / tear down of the request to see what’s actually happening with my data, but for now I’m content with littering print_r()’s everywhere.

          1. 4

            I use IntelliJ IDEA with Java and it’s trivial: Cmd+F7 and I can choose the scope for it to search (even through libraries, if I’m looking for types). It’s not doing text/string searches, its got a fully parsed AST. This has been available for many years (15?), so it’s hard for me to switch to languages/environments where I can’t do this. The Golang plugin for IDEA (and Go Guru, et al) also make this super-easy and precise.

            1. 4

              In Smalltalk… It’s a menu pick or a single line of code, as the browser is just an object in the system like any other.

              1. 3

                Go has tooling to do this: see go oracle. Most editors have decent integration for this.

                1. 1

                  My only gripe with the oracle is that you can’t ask it to ignore files that don’t build. So make sure the bit you’re working on is stubbed out, first.

                2. 2

                  In all honesty? On the extraordinarily rare occasion that I need to do this in java, I just declare the class or member as private and recompile. Boom, instant list of references.

                  1. 1

                    The far more interesting question, for me, is, why is this information ever needed? A well-designed system should expose data via a highly contractual method call, not a type member, no?

                  2. 2

                    I’m working in a large-ish Ruby on Rails 3 codebase at the moment, and finding where something is used (or comes from, for that matter) can sometimes take on the order of minutes, but is usually on the order of a dozen seconds, depending on how heavily-used that something is and whether it’s uniquely named or not.

                    The only tools that can help me are ag (i.e. thesilversearcher) and a working knowledge of the codebase. This gives me things that at least contain the name of the variable/method, and hopefully they named it something that doesn’t collide with something else too badly. From there it’s mostly an exercise in praying that nobody did anything weird (meta-programming etc.) that will come back to bite you later.

                    Ruby does absolutely nothing to help you figure out what’s going on. Once you write a method, it’s essentially forever “public” and nobody can help you figure out where, or what for, it’s being used for.

                    As an aside, working in this codebase has forever soured me on using a dynamic language for anything that has even a faint chance of growing beyond a couple thousand lines. It’s a constant fight against the what the language allows you to do wielded by people who often lack the perspective to know whether they should do it. Refactoring is an enormous task because you never know what you’ll need to change. There’s just too much rope to hang yourself by.

                    1. 1

                      I’ve mostly been writting in php and haskell recently.

                      While php is much harder to grok with grep, sometimes you do declare the type of certain objects before using them (in the function signature) and you can find when you instantiate a class. Haskell makes it pretty easy, I often grep libraries before using one of their types so I can learn it better, and finding it in my code is a breeze, ide tooling can be a little sparce though, I still dont have it working perfectly in vim, while I’ve been able to do some pretty cool completion stuff with php.