Archimedes and Input-Output Thinking in Programming

Archimedes and Input-Output Thinking in Programming

Daily short news for you
  • For over a week now, I haven't posted anything, not because I have nothing to write about, but because I'm looking for ways to distribute more valuable content in this rapidly exploding AI era.

    As I shared earlier this year, the number of visitors to my blog is gradually declining. When I looked at the statistics, the number of users in the first six months of 2025 has dropped by 30% compared to the same period last year, and by 15% compared to the last six months of 2024. This indicates a reality that users are gradually leaving. What is the reason for this?

    I think the biggest reason is that user habits have changed. They primarily discover the blog through search engines, with Google being the largest. Almost half of the users return to the blog without going through the search step. This is a positive signal, but it's still not enough to increase the number of new users. Not to mention that now, Google has launched the AI Search Labs feature, which means AI displays summarized content when users search, further reducing the likelihood of users accessing the website. Interestingly, when Search Labs was introduced, English articles have taken over the rankings for the most accessed content.

    My articles are usually very long, sometimes reaching up to 2000 words. Writing such an article takes a lot of time. It's normal for many articles to go unread. I know and accept this because not everyone encounters the issues being discussed. For me, writing is a way to cultivate patience and thoughtfulness. Being able to help someone through my writing is a wonderful thing.

    Therefore, I am thinking of focusing on shorter and medium-length content to be able to write more. Long content will only be used when I want to write in detail or delve deeply into a particular topic. So, I am looking for ways to redesign the blog. Everyone, please stay tuned! 😄

    » Read more
  • CloudFlare has introduced the pay per crawl feature to charge for each time AI "crawls" data from your website. What does that mean 🤔?

    The purpose of SEO is to help search engines see the website. When users search for relevant content, your website appears in the search results. This is almost a win-win situation where Google helps more people discover your site, and in return, Google gets more users.

    Now, the game with AI Agents is different. AI Agents have to actively seek out information sources and conveniently "crawl" your data, then mix it up or do something with it that we can't even know. So this is almost a game that benefits only one side 🤔!?

    CloudFlare's move is to make AI Agents pay for each time they retrieve data from your website. If they don’t pay, then I won’t let them read my data. Something like that. Let’s wait a bit longer and see 🤓.

    » Read more
  • Continuing to update on the lawsuit between the Deno group and Oracle over the name JavaScript: It seems that Deno is at a disadvantage as the court has dismissed the Deno group's complaint. However, in August, they (Oracle) must be held accountable for each reason, acknowledging or denying the allegations presented by the Deno group in the lawsuit.

    JavaScript™ Trademark Update

    » Read more

The Problem

"Give me a lever long enough and a fulcrum on which to place it, and I shall move the world." This is a famous quote by Archimedes. Many evidences indicate that even if he had a fulcrum, it would still take thousands of years to lift the world. Not to mention, where would he find a lever of unimaginable length? But in this article, I'm not discussing the accuracy of the quote. What I find fascinating is his way of thinking: "Give me...I will..."

"Give me x, I will return y" is an input-output thinking in programming. It is a very common problem-solving mindset. Instead of immediately writing code, take the time to evaluate and identify where the approach "give me x, I will return y" can be applied. You may have heard of it in the article Just Code, or Focus on Developing Your Programming Mindset?.

A Simple Example

For ease of imagination, let's take an example of a simple shopping feature. Its logic looks like this:

function createOrder(product, quantity, customer) {
  // find product information
  const productInfo = ProductModel.findOne({
    where: {
      product_id: product,  
    }
  });
  if (!productInfo) {
    throw new Error('Product not found');
  }

  // check quantity in stock
  if (productInfo.quantity < 1) {
    throw new Error('Not enough product in stock');
  }

  // create order
  const order = OrderModel.create({
    customer: customer,  
    product_id: product,  
    quantity: quantity,  
    total_price: productInfo.price * quantity,  
  });

  return order;
}

If you think in terms of input-output, you can make the above code look more concise and reusable. To achieve that, think about separating the logic into smaller pieces, making them Pure functions if possible. If you don't know what a "pure function" is, you can read the article Pure Functions in Javascript. Why Should We Know It as Soon as Possible?.

In the example above, we see three distinct and well-defined logics: finding product information, checking quantity in stock, and creating an order. From there, we can extract them into 3 smaller functions.

Give me a product ID, I will return the product information.

function getProductInfo(product) {
  return ProductModel.findOne({
    where: {
      product_id: product,  
    },  
  });
}

Give me the product information, I will check if it is in stock.

function checkProduct(productInfo) {
  return productInfo.quantity > 0;
}

Give me the necessary information to create an order, I will create and return the order.

function createOrder(productInfo, customer) {
  return OrderModel.create({
    customer: customer,  
    product_id: productInfo.id,  
    total_price: productInfo.price,  
  });
}

With these 3 functions serving clear purposes, you can easily reuse them. Anytime you need to find product information, use getProductInfo. Anytime you need to check quantity in stock, use checkProduct. Someone may ask why checkProduct needs to take productInfo as an argument instead of just the quantity attribute. The answer to this question depends on your experience in the project. By receiving the entire productInfo, it increases the ability to expand in the future. For example, if later on there is additional logic that only allows creating orders for products created after one week.

function checkProduct(productInfo) {
  return productInfo.quantity > 0 && productInfo.created_at < moment().subtract(7, "d").toISOString();
}

Of course, you can continue to break down checkProduct into even smaller functions if deemed necessary.

function checkProduct(productInfo) {
  return checkQuantity(productInfo.quantity) && checkDate(productInfo.created_at);
}

The benefit of writing input-output functions is that they are very easy to create unit tests. Since they have no dependencies (such as using a variable outside the function), you can write test cases to validate their correctness. For example, to test if checkProduct works as expected, simply pass in productInfo with quantity and created_at attributes, and the function will return true/false. With one input corresponds to one output, you don't have to worry about whether the function utilizes variables outside the function or has any side effects.

To sum up, thinking in terms of input-output, along with the ability to create Pure functions and limit side effects, allows us to write reusable and easily testable features.

Until now, Archimedes' quote has not been substantiated whether he could lift the earth given all the necessary conditions. But the functions you write can certainly be easily validated for correctness.

How to Think in Terms of Input-Output?

There are several ways to help you practice thinking in terms of input-output. After reading the suggestions below, the important thing is to practice by applying them in your personal projects or projects you are involved in.

Firstly, take the time to learn about what Pure functions are? What benefits do they bring?

Secondly, before starting anything, take the time to evaluate the feature you are about to implement. I have an article about the 4 steps to hone your skills at Just Code, or Focus on Developing Your Programming Mindset?

While writing code, do it naturally. Then, review if there are any logics that can be separated. If possible, separate them into Pure functions.

Repeat the above steps, and gradually you will develop the input-output thinking. Later on, when preparing to work on a new feature or a feature similar to a previous one, this thinking will come back to help you save time in evaluation and coding.

Conclusion

Thinking in terms of input-output is not new, you may have used it before without noticing. Hopefully, this article helps you realize the importance of this thinking and enables you to write code more efficiently and effectively.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...