14th: Reading other people’s code will always be a bad user experience

First, my bold statement is this: reading other people’s code will always be a bad user experience.

The reason is simple: code readability is mostly subjective.

In year 2000, I had to set up Mailman for the company I was working and maintained the application for a few months. I remember when I first read its source code, I was so amazed how beautiful and elegant the code were. Today, I just tried reading some of them again, I was like, “that is not easy to read at all”.

This week’s idea is: I am making a list of standards when reading other people’s code.

(1) The reason of reading

I was exhausted when I tried to read the source code of Mailman again.  More importantly, I no longer have any need to understand it and I also do not remember how to write Python.

The motivation of reading the source code is probably the first important element. If I don’t have to run it, test it, and even maintain it, it’s unlikely I will ever like it.

The take-way message is:

Do not judge other’s code when you don’t have enough context unless you are going to take responsibility with it. 

(2) The stage of the project

I will give an example of this point. The example is written in Scala. Let’s say I have an entity like this:

case class Entity(field1: String, field2: Int)

I would like to validate the fields, so I can start with this simple class:

class Validation1 extends Logger {
  def validate(e: Entity): Option[Entity] = {
    if (e.field1 == "hello") {
      logger.info("I say hello")
      None
    }
    else if (e.field2 == 10) {
      logger.info("10 is a bad number")
      None
    }
    else
      Some(e)
  }
}

I hope it’s easy enough to understand. The problem of this version is: I have to do if-else statements and when I have more validation logic, this function will become very ugly.

Therefore, I can refactor it to the following version:


class Validation2 extends Logger {
  def validate(e: Entity): Option[Entity] = {
    for {
      _ <- noHelloRule(e)
      result <- noTenRule(e)
    } yield result
  }

  private[this] def noHelloRule(e: Entity): Option[Entity] = {
    if (e.field1 == "hello") {
      logger.info("I don't say hello")
      None
    }
    else
      Some(e)
  }

  private[this] def noTenRule(e: Entity): Option[Entity] = {
    if (e.field2 == 10) {
      logger.info("10 is a bad number")
      None
    }
    else
      Some(e)
  }
}

What I am doing now is: each rule becomes a function so I can use a for-comprehension to connect them together.

However, the problem of this version is: the class has 2 private functions and all of them have to be invoked inside the main validate function. If I need to add more validation logic, I will add one private method, and then update my main function. It’s annoying.

Hence, I can refactor one more time.

trait Rule[A, Result] {
  def validate(e: A): Result
}

trait EntityRule extends Rule[Entity, Boolean]

case class NoHelloEntityRule() extends EntityRule with Logger {
  override def validate(e: Entity): Boolean = {
    if (e.field1 == "hello") {
      logger.info("I don't say hello")
      false
    }
    else
      true
  }
}

case class NoTenEntityRule() extends EntityRule with Logger {
  override def validate(e: Entity): Boolean = {
    if (e.field2 == 10) {
      logger.info("10 is a bad number")
      false
    }
    else
      true
  }
}

class Validation3 {

  private[this] val rules: List[EntityRule] = List(NoHelloEntityRule(), NoTenEntityRule())

  def validate(e: Entity): Option[Entity] = {
    if (rules.forall(_.validate(e)))
      Some(e)
    else
      None
  }
}

In this version, each validation logic becomes a single class with a single purpose because it represents only one rule.  The Validation3 class has a local variable holds all the rules. I am hard-coding the instance of the concrete rules, but later I can use different approach to inject rules dynamically. For example, I can use config file or attribute decorating a rule class, and then use reflection or meta to discover them.

Now, the question to ask is: why the heck I would do the third option at the beginning of the project, or with only a few validation rule and each of them is just a simple if-else?

Wait, some smart people might interrupt me. The third option is not Scala, or not pure functional at all. They are probably right. However, the take-away message is this:

It’s better to avoid over-engineering at the beginning. Instead, focusing on how to get the requirements right and readable, as well as validating the implementation with stakeholders, as opposed to overly generalize or optimize the implementation in the early stage. 

(3) the context of the product and the company 

As I mentioned earlier, code readability is highly subjective. However, my personal belief is: all programming languages are the same fundamentally. Proper choice of algorithm and data structure, separation of concerns, and SOLID (or following some similar principles)  are more important than particular language syntax.

This should be particularly true when building commercial software. Writing pure scientific applications could be a different story. The reason is: after the product is launched, 6 months later, people might not remember what the code is about. Because of all kinds of staff turnover, for example, original dev team has been switched to a different department, or external team has finished contract, some new people will end up maintaining the code-base they have no idea of. Those poor guys could be as dumb as me, or they could be any level, but they need to be fast enough to debug and enhance the application.

Here is a bottom-line, especially for people do not believe it: business software needs to be maintainable and enhance-able by any software developer, as long as they know how to compile, code, and test. Therefore, the take-away message here is:

Depending on the type of the project and the company you are working for, simplicity is always the key. We are never writing code for ourselves. We are writing code for, one day, another person has to add a new feature to it or debug why it fails. Always KISS (Keep it simple and stupid), but flexible (so other people can easily change).


In conclusion, I believe we should refer to at least above 3 points before judging people’s code. However, as a software engineer, I know no matter what I say, people will always have this expression when they read other people’s code.

 

 

13th: Hunter’s Feeder for cats and dogs

I have a Siberian cat. She is beautiful, but like most other cats, she is a pain sometimes. For example, she refuses to eat wet food, unless I hold the wet food in my hands. I also have to try many types of dry food to finally settle down one brand.

I use this auto feeder. It dispenses specific amount of food at a specific time. The timer is defective, always moves slower. Other than that, it works well. In terms of dispensing,  it basically drops the food in a bowl.

PetSafe Healthy Pet Simply Feed Automatic Pet Feeder

Unfortunately, my cat is not really into this feeding style. As I mentioned, she loves to eat when the food is in my hand. But I am not really a big fan of that. She will still eat from the bowl, but usually she will meow a while, see I am not going to feed her with my hand, and then finally she will go eat them. She seems to be more happy when she can eat in her favorite style.

There is, however, another way she loves to eat her food. It’s easier to show what it is than describing it:

Food hunting

Basically, I glide a dry food on the floor, and she would chase it and stop the food, then eat it. It may be difficult to see the food in the GIF.

My cat loves it.

I think deep inside, she still has the hunting instinct. Siberian cats are said to be dog-like and love chasing small animals.

I hate to let her go outside, because of her long fur, which attracts a lot of mud and leaves, very difficult to clean. I guess that is the main reason of me feeling guilty and letting her to eat from my hands so many times.

But you see, I have to stop doing that. Then I have to do something with the other option. Chasing food down reminds me of this:

iFetch Interactive Ball Launchers for Dogs
iFetch Interactive Ball Launchers for Dogs

This is a little machine that launches a ball for dogs to chase.

Yes, we need a similar machine to let cats or dogs to chase food.

This machine, I name it:

Hunter’s Feeder

I am going to learn how to build something like this, but here is a list of features I am thinking:

  • shooting food in at least 2 different ranges, i.e. short distance and long distance.
  • alarm mode: rings to prepare dispensing food so the pet can learn the time is ready.
  • user setting: user can select when and the amount of food to launch.
  • rotating shooting head: the launch angle can be 360 degree.
  • visual detect: the machine will detect if there is any obstacle ahead before launching and automatically select a direction to launch.
  • self-learning: it can learn how to train a pet to start chasing the food further or to a different direction, always keeps the pet interested. Say if a smart cat realizes the machine always just launch food to one direction, she just stays there and waits, instead of coming closer to the machine and then chasing. The machine needs to have algorithm to avoid this behavior.
I don’t have to do it again

OMG, I love this idea.

Now I really need to learn some mechanical engineering.

12th: Another way to buy used cars and more

Here is this week’s idea: why are all websites sells used cars the same?

They all have something like this:

from https://www.autotrader.com

They all have this kind of control for the users to filter what they want. It is a reasonable assumption. This kind of control is used by the potential buyers. However, has anyone thought of: this kind of website is actually initialized for the sellers.

By that, I mean: a seller will have to first go to that website, enter the selling vehicle’s information, as detailed as possible. Of course, it is also possible that a web crawler can do this job. It pulls the information from all sorts of website and extract those data from the description.

Therefore, a website that sells used cars must first have used cars to sell, and then when potential buyers come, they will be able to search.


E-commerce Websites are the Same

In fact, not only used car websites, all other E-commerce websites are the same. They first have things to sell, and then they attract buyers to find them. They all start from the seller’s perspective.


Another Perspective

What if a website starts from buyers’ perspective?

Let’s use cars as an example again. Here is a potential user flow:

  • A user enters some basic criteria on what used car he/she wants to buy: the type of car, mileage, budget, etc.
  • Sellers who think they have the possible used cars this particular user likes will submit an application, almost like submitting a job application, including what his or her car is and why the buyer should pick his/hers.

You may say, isn’t it the “WANT” option on craigslist?

True.

But there are more.

  • The website then uses algorithm to pick a few most likely to be sold cars (the number is user configurable) and then presents to the buyer.
  • The buyer can choose to pick one from the list and contact the seller directly. They are free to complete the transaction with each other offline.

You may ask: hey, where is the revenue stream?

Here it is.

  • The buyer can opt in a service that assists you to complete the transaction. The assistance could include
    • legal assistance: help finish all the paper work
    • mechanic check assistance: help inspect the car and provide accurate estimate and negotiation
  • The seller can also opt in a service that pre-inspect the selling car, which will show a proof that the car has been inspected (on what date). This can increase the likelihood that the car gets picked by a potential buyer.

I think it will work.

Unlike the bidding from eBay, which must have the sellers first. This idea serves the buyers first. The sellers must “sell”. Every seller is like a salesman on this website.


Extend Further

Let’s go further from here.

What if every E-commerce is like that?

Here is a use case.

I want to buy a book case. Traditionally, I will go to Google to find what website sells book cases. I examine all different websites for all the book cases, and read the reviews of the book case, and then read the review of the website (unless it’s a big trust-able seller).

That is totally waste of my time.

What if I enter briefly what I want in the book case, say, I want 4 levels, wood, with a stair. It should be A meter wide and B meter tall.

Now, all the sellers to submit an application of their book cases. Sure, you can argue this will only work if the sellers (human) are actively monitoring the request. But think about the possibility that we can build a robots pulling the information from all sorts of website and then intelligently select the most possible items I will be interested.

I will be given, say 10 different book cases. I am free to click the item and go to that website to complete the purchase. However, I can opt in a service that uses machine learning to tell me if those items are priced accurately. This will be useful, especially if some of those items are second-handed.


Conclusion

The idea is, a website serves from a buyer’s perspective, reduces the noise the buyer could receive from traditional E-commerce approach, and offer intelligent service to facilitate the buyers to find the things they most likely fall in love with.

I seriously think it might work.

 

11th: Street Food and Thoughts on Homeless Crisis

Street Food

I watched a few episodes of Street Food on Netflix.

Very interesting to watch. I recommend it.

I grew up with eating street food. After doing dumb things (like doing karaoke, playing Mahjong or pokers with friends), we went to have late night feast. Street food vendors usually opened late, could go all the way to next morning. Even after midnight, I remember still seeing street filled with people dining.

It becomes rare these days now, because government starts to have regulation. Some streets still have it, but not the same as before. It was a very special age. The fragrance, the people, the sounds, and the lights, all of those complete a very unique dining experience, which cannot achieved from eating in a restaurant.

Just like the trailer says:

“If you want to taste real food, it has to be street food”


Seattle’s Homeless Crisis

On a total different topic to street food, Seattle, is infamous for its homeless crisis. Because of some special policy of this city, it’s said some other cities bought one-way bus fare for their homeless people to come to Seattle.

Fair enough, I am not against homeless, everyone has a bad day. I truly believe parts of homeless population end up in the situation due to a serious of unfortunate events, and they actually want to change, like having a home again.

This city government has been discussing again and again on plan for homeless. Some city councils just want to tax more to the rich people to solve some problems those city councils have no idea how to solve… every time I hear that again on the radio, this is my reaction.


The Connection

People still need to eat.

People have memory.

People also have skills.

For some people, they remember the food they have had; maybe it’s a food they love, or may it’s a food they remember from someone else.

Like I said earlier, I believe some of the homeless people really want to change the situation, and they probably are working hard on it.

Here is my idea: can this government allocate some funds that provide training and necessary resources to those people who want to change? For example, train they how to cook with a food stand, provide some tools and resources for them to prepare, and then lower the bar for them to run business.

In some areas, this city could open an area for night market. It is opened to public. Have some police watching as well, maybe at the beginning. It does not have to be limited to street food, it could be anything else. Maybe some homeless people love to make arts, or play music, or crafting. So the night market, by homeless people, sponsored by government, is not only a way for people who want a home again, but can also become a celebration of folk culture nightly and connect people together.

Of course this idea is very naive. From the bottom, the government is investing on people who want to work and contribute; instead of charging more from the top. Yes, the world is unfair: a small group of people appear to own large portion of the fortune in this world. But some of those people really worked their ass off to reach that point. They just happened to have the right idea at the right time, with the right vision and enough courage. We should not lay here and ask them to give us money, simply because they have more than enough. How is that fair?

The same thing is applied affirmation action bill. It is basically a legalized racism. Because Asians are over populated in many schools and work places, they want a law to enforce admission in school or hiring in work places must have even ratio of races. It implies they don’t even want to give a chance to people who are willing to work hard, or who have better qualification.

That, is why this city government does NOT want to invest in homeless people who want to change the circumstances.