5 ms·
> keep the code that changes together close together. Agreed. I do that, but never got around to factoring out a one-liner that expresses it. I also tend to h
by ChrisMarshallNY 5y ago
> keep the code that changes together close together.
Agreed. I do that, but never got around to factoring out a one-liner that expresses it.
I also tend to have fairly deep directory trees that tend to reflect the code hierarchy/structure.
I will also factor out large chunks into standalone subprojects, and reinclude them as packages. This results in very high quality code, and also gives me dozens of libraries of tested, ship-quality, reusable code.
Another thing that I do, is have fairly large source files, that aggregate multiple classes/structs/enums/protocols that relate to each other. It drives me nuts to deal with the typical Java "Each class has its own file -no matter how small" thing.
I practice what might be called "eXtreme documentation. I rely on Jazzy (sort of like Doxygen), and now, DocC, so every element in my code has a headerdoc. I make heavy use of the "// MARK:" macro, as well.
I write about that, here: https://littlegreenviper.com/miscellany/leaving-a-legacy/ https://littlegreenviper.com/miscellany/leaving-a-legacy/ (I need to add a DocC update).
- ryanar 5y agoI followed the same pattern when I had the time to. Instead of building the thing, build it as a separate package and include it as a dep. Packaging code up for open source consumption leads to better docs, tests, and tighter abstractions that are generalized.
- janto 5y agoI think that's the opposite of what the parent says: first build the thing and only then factor it out and reinclude it.
- ChrisMarshallNY 5y agoNot completely. The way that it works for me, is that I start work on a project, and, while building, I notice that some code that I'm working on is: 1) Pretty complex, and fairly insular; and/or 2) Possibly useful, elsewhere. If that's the case, I will then stop work on the main project, and take some time to extract and "genericize" the subproject. I'll usually set it up as a standalone open-source project; complete with tests and documentation. As the commenter stated, I think that this results in some excellent code. I always clean the house before the guests arrive. This may happen before I have completed the coding in the main project, or may happen as the result of a review, after the fact. In some cases, I very clearly need to develop a subproject before starting on the main project, or before certain milestones within that project (for example, SDKs or drivers). In that case, the timelines are completely separate. If you look at my GH repos, you'll see a whole bunch of these projects, including some rather strange ones, like an XML duration parser[0]. These are the types of projects that I extract. In some cases, I end up not using the extracted project in my main project (happens to some of my UI widgets). In that case, even though I am not using it, I still have an excellent project for the future. Here's an example[1]. I have ended up not using the spinner in my own work, as it was too obtrusive a widget, but it's nice to have it available for future projects. [0] https://github.com/RiftValleySoftware/RVS_ParseXMLDuration https://github.com/RiftValleySoftware/RVS_ParseXMLDuration [1] https://github.com/RiftValleySoftware/RVS_Spinner https://github.com/RiftValleySoftware/RVS_Spinner
- camtarn 5y ago> It drives me nuts to deal with the typical Java "Each class has its own file -no matter how small" thing. Couple this with extensive use of inheritance and design patterns, and you have a recipe for awfulness. One of my previous teams had an implementation where rendering a piece of HTML would involve digging through about twenty different source files, each with maybe 3-4 lines of actual code other than the class definition boilerplate. One top-level line would send you down the inheritance hierarchy of ClassThatGetsData/ClassThatGetsDataPlusThisOneOtherThing/...PlusThisOtherOtherThing/... etc etc, then the same thing for ClassThatProcessesData/... and then ClassThatRendersData/... Adding a single bit of data to rendered HTML (e.g. a tiny star for 'this product is well-reviewed') would involve altering every single source file in the multiple trees, and maybe adding some new specialisations to make the trees even deeper. At the time, fresh out of Uni with a head full of design patterns, I thought this was just how enterprise code was meant to be structured!
- radicalbyte 5y agoIt takes a long time for some people to understand that complexity is the enemy; and for patterns in particular it takes some people a long time to learn when to apply them. Same thing for algorithms: BloomFilters are the classic example. You can tell when it has hit the font page of HackerNews recently because suddenly everyone starts looking for problems that it can solve (poorly). Just don't get me started on blockchain... :-)
- ravenstine 5y ago> At the time, fresh out of Uni with a head full of design patterns, I thought this was just how enterprise code was meant to be structured! Ugh... and that's the flaw in teaching design patterns. Don't worry, that happened to me too, though I didn't go to a university. Design patterns are an advanced tool. If you're learning to code, design patterns are mostly non-applicable to the kinds of problems that newbs are solving. Design patterns are meant for specific problems, but somehow we end up believing they're the end-all-be-all and that we must be using some design pattern in our work. If you have design patterns pounded into your head, and you believe the only design patterns that exist are the one's someone has already named, then not using a particular design pattern can seem like chaos even when it's not. I wish we'd stop teaching that shit. If you've been coding long enough and faced complex enough problems, you'll either come to embrace some design patterns or you won't. Otherwise they'll likely just be misapplied. OOP fits this view of mine as well. In general, I don't think most programmers need to be aware off OOP principles because they will almost certainly misuse them. And to what end? Several single purpose classes and "tiny functions" scattered across multiple files that others are now forced to jump between.
- whoisthemachine 5y ago> It drives me nuts to deal with the typical Java "Each class has its own file -no matter how small" thing. There is value in keeping some source together, but I think it's more judgment based than you imply, it can also drive me nuts when I have to dig through a dozen 2k+ line files to find the thing I need to change, and it also reduces my confidence that I can change that thing without introducing unwanted side effects (unit tests help increase that confidence regardless of code structure).
- ChrisMarshallNY 5y agoFair point. As someone that regularly digs through 200-1.5K files (2K is a bit much, for me), I can report that good documentation makes a huge difference.
- whoisthemachine 5y agoGood documentation is definitely helpful, but expensive and challenging to maintain (and keep correct). In my personal experience, documentation is out of date or invalid nearly the minute the ink dries... much better (again, in my experience) to have good, descriptive unit tests that fail when the code changes.
- ChrisMarshallNY 5y agoUnit tests are not always a good match with the application: https://littlegreenviper.com/miscellany/testing-harness-vs-unit/ https://littlegreenviper.com/miscellany/testing-harness-vs-u... I tend to use a lot of test harnesses (which, IMNSHO, are much better application exemplars than most unit tests). I also use a lot of headerdoc stuff. In Xcode, it allows your own code to show up in the QuickHelp navigator tab, and you can generate really good SDK docs. It also stays fresh. Easy to maintain. Using "breakers" is a huge aspect of my code. Makes it much easier to scan, and liberal use of // MARK: is good.
- whoisthemachine 5y ago
- radicalbyte 5y ago> Another thing that I do, is have fairly large source files, that aggregate multiple classes/structs/enums/protocols that relate to each other. It drives me nuts to deal with the typical Java "Each class has its own file -no matter how small" thing. This can go both ways - I've seen plenty of projects where you have to dig through 3k lines of unrelated spaghetti code just to find the bit you need. That turns into a special piece of hell when you have multiple team members working on it. The original motivation for having more small files was driven by these "god objects" and the limitations of version control. CVS, Visual Source Safe and to a lesser extent Subversion were much easier to use together with smaller files. If you're using modern version control (git) then that reason has gone. My personal preference is to split horizontal concerns into their own modules and depend on them; they become the core / support libraries for my team/organisation. For services I try to keep the domain-specific parts close. Tests go in a different module which mirrors the structure. When units start to get big (or become cross-cutting) they get factored into smaller units or other modules. In the end it's all a balance and unless you're under outside constraints (cough SonarCube box-ticking busywork idiocy cough) then you can generally keep it sane.
- corpMaverick 5y agoThis sounds a lot better than the Single Responsibility Principle that is often quoted and IMHO often misunderstood.