this post was submitted on 10 Sep 2024
58 points (98.3% liked)

Programming

17479 readers
221 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
all 14 comments
sorted by: hot top controversial new old
[–] [email protected] 28 points 2 months ago (3 children)

tldr

  1. Keep Commits Small: Ensure commits are small and manageable to facilitate easy reverts and debugging.
  2. Continuous Refactoring: Prioritize frequent, minor refactorings to simplify future changes.
  3. Deploy Continuously: Regularly deploy code to ensure it works in production, as working software is progress.
  4. Trust Frameworks: Avoid over-testing framework capabilities; focus on application-specific logic.
  5. Create Independent Modules: Place functions in new modules if they don’t fit existing ones, preserving code organization.
  6. Write Tests Early: Use tests to design APIs and guide development, but don’t be rigid about TDD.
  7. Avoid Duplication: Prevent repeated code by abstracting similar implementations after copying once.
  8. Embrace Change: Accept and adapt to evolving designs, understanding that change is intrinsic to software development.
  9. Manage Technical Debt: Focus on minimizing immediate blockers and plan for potential future issues.
  10. Prioritize Testability: Ensure that your code and design facilitate easy testing to maintain code quality and coverage.
[–] [email protected] 7 points 2 months ago (1 children)

Manage Technical Debt: Focus on minimizing immediate blockers and plan for potential future issues.

This is a tough one the bigger the project gets. Might be the toughest one.

[–] [email protected] 6 points 2 months ago (2 children)

It's an ideal that's only achievable when you're able to set your own priorities.

Managers and executives generally don't give two shits about yak shaving.

[–] nous 6 points 2 months ago

Just factor it into your estimates and make it a requirement to the work. Don't talk to managers as though it is some optional bit of work that can be done in isolation. If you do frequent refactoring before you start a feature then it does not add a load of time as it saves a bunch of time when adding the feature. And helps keep your code base cleaner over the longer term leading to fewer times you need to do larger refactors.

[–] JackbyDev 2 points 2 months ago

Re: trust frameworks

I often find myself writing scratch work within tests because it's just the easiest way to get stuff up and running. Sometimes I'll leave these as a way to show that my assumptions about a less used feature (by my team) of a framework works the way I believe it does. But it's rare.

[–] [email protected] 1 points 2 months ago
  1. Embrace Change: Accept and adapt to evolving designs, understanding that change is intrinsic to software development.

Sounds like bad management to me.

And sure, start your project flexible and adabtable from the ground up, but that needs planning and time!

[–] nous 18 points 2 months ago

Big refactorings are a bad idea.

IMO what is worst then big refactors are when people refactor and change behavior at the same time. A refactor means you change the code without changing its behavior. When you end up mixing these up it becomes very hard to tell if a behavioral change is intended or a new bug. When you keep the behavior the same then it is easier to spot an accidental change in behavior in what otherwise should be a no-op change.

And if you can, try to keep your refactors to one type of change. I have done and seen many 100 or even 1000s of lines changed in a refactor - but was kept manageable because it was the same basic pattern changing across a whole code base. For instance. Say you want to change logging libraries, or introduce one from simple print statements. It is best to add the new library first, maybe with a few example uses in one PR. Then do a bulk edit of all the instances (maybe per module or section of code for very large code bases) for simply switching log instances to the new library.

If you don't know what an API should look like, write the tests first as it'll force you to think of the "customer" which in this case is you.

I think there is some nuance here that is rarely ever talked about. When I see people trying this for the first time they often think you need to write a full test up front, then get frustrated as that is hard or they are not quite yet sure what they want to do yet. And quite often fall back to just writing the code first as they feel trying to write a test before they have a solid understanding of what they want is a waste.

But - you don't need to start with a complete test. Start as simple as you can. I quite often start with the hello world of tests - create a new test and assert false. Then see if the test fails as expected. That tells me I have my environment setup correctly and is a great place to start. Then if I am unsure exactly what I want to write, I start inside the test and call a function with a name that I think I want. No need for parameters or return types yet, just give the function a name. That will cause the code to fail to compile, so write a stub method/class to get things working again. Then start thinking about how the user will want to call it and refactor the test to add parameters or expect return types, flipping back to the implementation to get the code compiling again.

You can use this to explore how you want the API to look as you are writing the client side and library side at the same time. You can just use the test as a way to see how the caller will call the code. No need to start with asserting behavior yet at all. I will even sometimes just debug print values in the test or implementation or even just write code in the test that calls into a third party library that I am new to to see how it works. With no intention that that test will even be included in the final PR - I just use tests as a staging ground to test ideas. Don't feel like every test you write you need to keep.

Sometimes I skip the testing framework altogether and just test the main binary in a simple situation. I especially do this for simpler binaries that are meant to mostly do one thing and dont really need a full testing framework. But I still do the red/green/refactor loop of TDD. I am just very loose on what I consider a valid "test".

The second time you're introducing duplication (i.e., three copies), don't. You should have enough data points to create a good enough abstraction.

This missed one big caviat. The size of the code being duplicated. If it is only a few lines then don't worry so much. 5 or even 10 copies of a 2 line change is not a big issue and quite often far harder to read and maintain then any attempt at de-duping it. As the amount of code you need to copy/paste grows though then it becomes more advantageous to abstract it with fewer copies.

if you find yourself finding it difficult to mock

I hate mocks. IMO they should be the last resort for testing things. They bake far too many assumptions about the code being mocked out and they do it for every test you write. IMO just test as much real behavior as you can. As long as your tests are fast and repeatable then you dont need to be mocking things out - especially internal behaviors. And when you do need to talk to an external service of some kind then I would start with a fake implementation of the service before a mock. A fake implementation is just a simple, likely in memory, implementation of the given API/interface/endpoints or whatever.

With a mock you bake assumptions about the behavior into every mock you write - which is generally every test you write. If your assumptions are off then you need to find and refactor every test you have that has that assumption. With a fake implementation you just update the fake and you should not need to touch your tests. And you can write a fake once and use it on all your tests (or better yet use a third party one if one is available (for instance I quite often use goflakes3 - a golang in memory implementation for aws s3 service).

[–] [email protected] 14 points 2 months ago (2 children)

Tip #1: if you're gonna post your programming blog to social media, make sure it can handle the traffic...?

[–] sjpwarren 2 points 2 months ago

Works on my machine

[–] [email protected] 1 points 2 months ago

Works fine on my end, could've been a temporary outage.

[–] heikkiket 4 points 2 months ago (2 children)

I 98% agree. Only thing I'm not sure about is if third copy/paste is always a bad thing. There are things like configuration and templates where independency is more important than DRY. Sometimes you do those through code.

[–] [email protected] 2 points 2 months ago

Yeah in reality it's even the fourth or fifth copy-paste that actually warrants generalizing, inevitably I find some tweak I need to make to two of the places to render the original generalization a waste of time

But if you are going to copy and paste make sure you keep it as similar as possible to the other instances to make it easy to refactor if you revisit in future

[–] bugsmith 1 points 2 months ago

Totally agree. Like most "rules", it just needs treating with nuance and context.