Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 18, 2022 08:47 am GMT

Code development guide: working with small chunks

Final work with the code can be a real pain in the neck. It is often a long and tedious process of going through and verifying all the changes which can bring you to a moment when frustration is directly proportional to the time spent on writing the code. Is there a way to tidy up the work and avoid those tears under the shower at the thought of another implementation? Yes, there is. You can work with small chunks.

Whats the prob, dev?

Imagine waking up after tens of minutes (or even hours!) of coding trance and seeing 50 changes in your editor. Changes were made in totally different places of the application.

Image description

You look at this code-battlefield and ask yourself *the same question the Ant-Man did. *What the hell happened here?... To make matters worse, not everything is working and you have a lot more to do because you poked your thumb in too many pies. Part of the changes has to be verified again and it will pull further changes. On top of that - you havent corrected even one test.

You know that means one thing - a need to go through and verify all the changes, remove the unwanted code, and fix the tests. Well, you could probably say that youre starting iteration number 2. The problem is, you are tired and you lean towards leaving work for tomorrow. After all, you did a great job today. Isnt it enough? ;)

Eventually, that approach drives me to the moment I dont feel like working at all. Its like complaining about other peoples work and making a much worse mess than the others. Frustrating, isn't it?

Image description

Thats why I try to organize my process of creating software by the little approach. Small, but thought through and effective steps.

Let me guide you through all the stages of the work and point out its important elements.

Lets start with a good plan

Think over carefully how to approach the problem to finish the task as a whole. Focus only on your goal and only those changes that will lead you to achieving it.

Whats important - I rarely pay attention to the tasks Ill be working on next week or in two weeks' time. I focus on it when the time is right. Not once and not twice I had prepared implementation for the future changes and finally, there was no need of doing all that work because requirements changed in the meantime. All the work that was done turned out to be a waste of time.

Its hard because youre often overwhelmed with tasks. You hear about SOLID, DRY, and the rest of the acronyms that make the whole thing messy and abstract. You end up wasting your time instead of just working on a specific piece of code.

This is the moment when you need to start making decisions. For instance, you need to decide if youre implementing the strategy pattern or not. But on top of all that is if in the future you will be able to easily change/ replace the elements you are currently working on. Without deleting half of your code. I focus on coupling the changes with the rest of the code.

I tend to point out the work phases on a sticky note. After completing each task I can cross it out. But the real moment of little glory comes when all the tasks are completed and the crushed paper lands in the bin.

Image descriptionOf course, you can use the virtual sticky notes and have the satisfaction coming from clicking the delete button but frankly, its just not the same ;)

The plan isnt a carbon copy with the changes Im implementing. It is because you can subdivide them into even smaller steps.

It is my high-level work plan and not the plan for the particular changes.

One change at a time

This is where it gets interesting. After preparing the plan its time for a serious fight - decomposing the work into smaller pieces.

Examination of borders and drawing a line where I start and finish the work is just a heuristic. In that matter, I rely on two factors.

A type of change is the first factor. Ive defined a catalog of 5 different potential changes:

  1. Change in the file and directory structure - changing names and moving them
  2. Refactorization - preparing the ground in the accordance with Uncle Bobs 'Clean Code' principle
  3. Implementation - the most atomic and independent part of the function youre working on
  4. Fixing an error
  5. Deleting an unnecessary, unused part of the code

I always try to make only one change at a time. The exception to this rule is when the changes are mutually exclusive, for example - changing the file name that fixes the error.

Unfortunately, there is a trap here because its impossible to describe decomposition in just five easy points.

Second factor. I verify if the change meets the assumptions below:

  • To be atomic
  • To be as independent of the future changes as possible. What I mean - it doesnt require something that doesnt exist yet
  • To be dependent on things I added earlier. What I mean - it can use previously added functions
  • To change one specific function
  • To apply only one specific file (ideal situation)
  • To easily describe the changes in just a few words (it makes creating commit messages a lot easier)
  • To start from the deepest changes, the ones at the end of the calls chain

Let me show you an example:

  1. I add a function to calculate the X. Its not used anywhere yet (new MathCalculations function)
  2. I add a new endpoint that Ill be taking actions in (only Controller action and route)
  3. I add the validation of data coming to an endpoint
  4. I add business logic (e.g. in UseCase)
  5. I connect triggering logic with the endpoint

Theoretically and practically I am able to merge my changes with the main development branch. Nothing should happen because various elements are independent of the next steps I plan to take.

This is, of course, just an example and we may find that to point 4 we would need to add a new entity, repository, or generate a new table in the database. I would think about these as separate changes.

Its important to remember that theoretically, one single change can require dividing it into many smaller pieces and each one of them follows exactly the same process. I was thinking about 3 changes and I ended up making 7 :)

Decomposition is the key part here. Either you have it or the process itself wont get you anywhere.

Earning the ability to do that wont always mean it is going to be easy (but also no one promised it will be ;) ). You often need to know the system youre modifying, its architecture, code structure, and the main principles it follows. Without them, working little by little is a lot more difficult. Whats important is that each new task is knowing the code and how the software was built. This is what makes us move around much quicker and with higher confidence.

In this step, Im not afraid to use comments // @ TODO. But only to make my work in the next step easier and finally to replace it with an appropriate implementation.

Everything needs to work

Everything needs to work after the changes you made. Every added, deleted line or modification cant have any side effects, e.g. damaged automated tests, defective SCA, or malfunctioning software. Everything needs to be compiled correctly.
Whats the next step? To check if everything works. I verify not only the app but also everything needed to implement the next change in the environment.

I tell more about the changes testing approach in my 'Programmer, Test!' article. Youll find a list of points worth remembering in the process of changes verification.

Write it down!

Image description

Preparing the storytelling about changes in the right words is the final step. Remember, you need to explain what you have done and why.

  1. I add files to the Staging Area (git add)
  2. I make the Git Commit and the created history I put into the Commit Message

This is where I can make the next step. Part of the work I consider done has been secured and I can safely take the next step in this case

What Have I achieved?

First of all, using only one tool (git) I can track/monitor the progress of my work. Even if I had a difficult weekend due to my friends bachelor party I could still easily remember what I had done on Friday. And after getting rid of the hangover I am able to quickly get back to work on Monday morning ;)

And whats important - I can easily undo my experiments. All I need to do is to get back to a specific commit.

In my opinion, its important to take care of proper history creation used in Commit Message. You get this feeling of sweet harmony in your work. You know what you changed and most importantly why you had done it.

But on the other hand, if you dont care about this you can use Git Squash to simply merge a few commits in one. That way you will describe the final result, not how you achieved it.

Obviously, as always, the right approach depends on the project youre working on or simply your preferences.

Do I push every commit created?

It depends. Sometimes you co-work with someone on the same branch and your partner is waiting for your changes. The other time you will just upload them from time to time.

Over time you will get this on your list of habits and you will realize that the work is getting done faster. Next thing you know you have a bunch of tasks marked with the green done tick. In the end - practice makes perfect, or rather practice makes practitioner ;)

In the end, I would like to draw your attention to 'Commit Often, Perfect Later, Publish Once: Git Best Practices'. Actually, I think this is where my adventure with the little by little work and Git approach had begun.


Original Link: https://dev.to/emphie/code-development-guide-working-with-small-chunks-28e4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To