DEV Community

Cover image for Why Losing Weight is Only Possible with Programming
Ivan
Ivan

Posted on

Why Losing Weight is Only Possible with Programming

A few years ago, I read an article that detailed numerous calculations, formulas, and explanations about why it is so difficult for us developers to lose weight. I don't understand why there's so much material and discussion about weight loss when there's only one way in our universe to shed those extra pounds (maybe different laws of physics exist in another universe): consume fewer calories than you expend. There is no other way to lose weight. Nature hasn't invented one, and it hasn't emerged in the evolutionary process. So, where does programming come into play?

This article is divided into two sections: the technical part, about a Java application written for AWS, and a discussion on weight loss, motivation, etc. If you're only interested in the technical part, you can skip to "Technical Part", otherwise, I invite you to read on.

Rationale

I don't believe that losing weight is a hard and complicated process. Losing excess weight is a long process that ideally (for me) evolves into a lifelong journey that never ends. The truth is, the process shifts to another category where you're not trying to lose weight but rather you're becoming an athlete for your own health and longevity. So, does this mean you'll never achieve your desired result? That's exactly right. Once you set a goal like: "I need to lose weight," you’re doomed to fail. The goal should be rephrased: "I want to live a healthy and fulfilling life." The goal of losing weight will never lead to satisfaction because your reflection in the mirror will never look the way you want, and it will never be like photos of fitness bloggers on Instagram, simply because you're probably not a fitness blogger.
When you set the goal to lose weight, questions immediately arise: how quickly will you lose weight and how? Most likely you're aiming for as quickly as possible, right? This means either starting to drink some health-wrecking fat burners or going on a strict diet and eventually gaining back more than you lost, repeating this cycle over and over. How many times have I seen this cycle at the gym: people start a new life, only to then return to their old one, gaining weight faster and falling into an even deeper pit. Let's even assume you've lost the desired weight. Do you look better? Probably not, because losing water, fat, and muscle doesn't mean looking better; for most people, it's the opposite effect. And also, look better for whom? For yourself, for Instagram photos, or for your partner? In my 11 years at various gyms, I have never met anyone satisfied with the result of such a goal in the long run. NEVER! I could go on and on with examples. For myself, I decided once and for all: “losing weight” is not the goal and never should be. I dare to suggest you agree with me, at least for the duration of this article.

Goal

Our goal is to be an athlete for the rest of our lives and to improve our quality of life. Why should the goal be lifelong? Because I am 100 percent sure that the overwhelming majority of people on earth share one thing in common: we want to live happily. What this means is up to you, and I'm not talking about destructive or unattainable desires, maniacs or psychopaths, but about most people. So, if we want to live happily, why not set a goal that we can maintain throughout our lives, not just from ages 23 to 31? Maintaining such a goal is impossible without daily consistency, so we’ll now shift our discussion to focus solely on food. Not on workouts, being more active, running, getting in your steps, etc., but on what we cook and eat every day. Proper nutrition is probably 80% of success in achieving the goal, if not more. If you can control what you eat, you can control your weight, your well-being to some extent, improve your sleep, and ultimately your life.
I know it sounds crazy, but just consider how much of your life is spent eating, and you quickly realize the immense impact of your diet over the years. I would also add sleep to this, but that's a whole different conversation, so I'll just leave this link to a great book on that topic.

A Simple Strategy for Success

I'll now describe a strategy that, in my opinion, is foolproof: changes should happen very slowly and start with the simplest steps. Currently, I know my calorie intake, which is 2300 kcal per day, and I count every intake. If I want to lose weight, I gradually reduce calories; if I want to gain weight, I increase protein and carbs. But it's challenging and time-consuming for many people to start counting if they've never done it before, and most likely, they won't last even a couple of months. Let's simplify the strategy. Let your first step be to start recording what you eat, maybe using a note on your phone, and continue for a few weeks. Then, start analyzing: how can I change my diet? For example, if you usually eat two pieces of candy or drink a soda on your way home from work, you can cut that out, and so on, step by step. Remove some things entirely, replace others with protein and fiber. Does this still seem too complicated? No problem, simplify your first step even more, so that you can make it a lifelong habit. For example: I will drink 2-3 liters of water every day. I have a 2-liter bottle, and by the end of the day, it should be empty. I think you get the pattern: it’s doing something that you can do for a week, two, a month, and so on, without stopping and never quitting. And add such changes as you get used to the previous ones.

Technical Part

Now, let's transition to programming and how it's related. As mentioned earlier, I count calories, which means my wife and I weigh the raw food we eat. We don't always have time to cook every day, so we cook for a week. The challenge is, for example, we cook 500 grams of dry rice, but how much cooked rice is that? It varies every time due to many factors, but the main point is: you need to know the raw weight of the food on your plate if you're counting calories. Do you see the potential for automation? Every time we take cooked rice (or any other food), we need to calculate it using the same formula:

(X / N) * Y where:
X is the raw weight you want to eat,
N is the total raw weight of the all the food item you cooked,
Y is the total cooked weight of all the food item you cooked.

Automation Potential

We realized that this could be more convenient, which led to the idea of creating an app that remembers and calculates this for us. The idea is simple: we record the food, raw weight, cooked weight, and when eating, we choose the dish and the desired raw weight, and the app gives you the corresponding cooked weight. For example, if you cooked 500 grams of dry rice, resulting in 1210 grams of cooked rice, to eat 40 grams raw, you need to take 96 grams cooked.

Saving prepared food

Eating process

Eating process

Implementation

For the implementation, I didn't want a separate app for such a minor function, so I chose something I use constantly: a Telegram Bot. I wanted to learn AWS Lambda, so that was an easy choice. For dependency injection, I used Dagger. For database connection, I used standard tools from java.sql, but in retrospect, I should have spent more time finding a lightweight ORM solution. Writing a lot of auxiliary code took a lot of time and made the project more complex than necessary.

Core Functions

I needed to achieve two things: remembering the prepared food and calculating the equivalent raw weight in cooked form. The bot has two buttons: "Cooking" and "Eating". Pressing one of these sends a message to the Telegram server, which then sends it to the webhook API Gateway, triggering a Lambda function. This request goes to the handleRequest method, where the message and additional information are processed into an update object. With each new user message, we need to know the user's current step to send the correct response and perform the corresponding action. The exceptions are commands like /start, /help, and /cancel. /start and /help are standard commands suggested by Telegram for a better user experience, displaying messages to the user without database interaction. The /cancel command resets the user to the beginning, canceling all previous actions. Unfortunately, I didn't implement a rollback for deleted foods; it just returns the user to the main menu. The /delete command also initiates a chain of actions, saving the user's step in the database.

Project Structure

Package com.nutritrack.bot.service.step contains the logic for all user steps.
Each class implementing StepService represents a user step: SteplessServiceImpl, RawFoodWeighingServiceImpl, etc.
UserStep class presents all existing user steps.
At the end of each service, the user's step is updated and saved in the database, ensuring the correct service is called with the correct message to it’s step.

Conclusion

This seemingly simple project involved significant thought and learning. The code is available on GitHub, and you can try the bot to test response time and see how it's implemented. If you want to join and contribute, I'd be thrilled.

Final Thoughts

Why did I create this project and write this article? First, for convenience and saving time in my family's daily life. Second
, for motivation: I created a project that's not perfect, but it helps me stay on track with my goals every day. Isn't that a worthy reason for the time spent? Additionally, there are many positive side-effects, such as learning new technologies (Dagger and AWS Lambda), finishing a pet project that’s on GitHub rather than forgotten on a external hard drive. If this work inspires or helps even one person, it was worth it! If you're lacking motivation to start something big, begin with something small. Your own project, article, or contribution to someone else's application could lead to positive changes, so why not to try it? Thanks for your attention!

P.S.
As Master Yoda said: "Fewer tests you write in applications, more calories you burn during debugging. May the Force be with you."

Book: Matthew Walker; Why We Sleep

Connect with me: LinkedIn

Project on GitHub

Telegram Bot

Top comments (0)