For years I have felt sad without knowing the reason. Some nights, I'd feel very bad. A feeling of missing something essential, though I wouldn't be able to pinpoint a thing that I miss. I'd sometimes discover something as the cause for my worries. Once such a discovery has happened, I can then consciously think about that and worry deeply. It has never helped much.
Only a few weeks ago I found out that I don't know the reason for my worries because there's no real reason. Because I was never worried. I was only feeling tired and/or sleepy. Just letting myself relax and rest would bring my happiness back. Mind is a wretched thing that rejoices in its own tears!
18 Jul 2011
15 Jul 2011
Mistakes
I can correct my brother's spelling/grammar/factual mistakes in public and he's cool with that (although he doesn't always correct me in public). When I was a kid, I was even told that I am allowed to correct my teachers if/when they're wrong.
Having grown up with people like that, I took the liberty to correct people when I thought they were wrong. Not many people took it well, especially when they were not close friends with me. I learned from my experiences and now I don't always say it when I find something wrong. I mostly assume that they don't care if they are right or wrong; they just want to say what they think/feel.
But I really wish everyone were like my brother. I also wish everyone told me when they thought I was wrong.
Having grown up with people like that, I took the liberty to correct people when I thought they were wrong. Not many people took it well, especially when they were not close friends with me. I learned from my experiences and now I don't always say it when I find something wrong. I mostly assume that they don't care if they are right or wrong; they just want to say what they think/feel.
But I really wish everyone were like my brother. I also wish everyone told me when they thought I was wrong.
Conditionals and code complexity
Let's say we have a function that takes the name of a city and returns the city's current weather. More details about the implementation:
The problem with this code is that it uses way too much branching. An if-else is essentially one statement. What I mean by that is, when you change something in the middle of an if-else, you should keep the entire if-else in mind. The condition
Now, the same code written in a way I like:
If you have been writing code for a while, you will know that what starts like the second version would eventually end up like the first version over time: by changes that happen one line at a time. As you make code changes, check how deep your decision trees get; if it's anything more than two, it's time to restructure the code to simplify it.
- The user might misspell the city name. The function will use a spell checker to correct any spelling errors we may find.
- Even with spell correction, we may not find the city. We will throw an exception if we can't find any city.
- There can be multiple cities with the same name. For example there is a Hyderabad in India, and one in Pakistan. In such cases, we will arbitrarily pick one. If we get the wrong city, user has to refine the query.
- We may not be able to get weather data. If the weather service we are relying on fails, we return
null. (You would probably throw an exception here as well, but this is just a fabricated example.) - You don't always want real weather data. Most of the time, you are working on different parts of the app; you don't care if the weather you get is accurate or not. You want to use fake weather data if you are running the app for development/testing.
Weather getWeatherFor(String cityName, boolean mayCorrect) throws CityNotFoundException {
if (AppConfig.useFakeWeather()) {
return AppConfig.getFakeWeather();
} else {
try {
List<Location> locations = locationService.findCity(cityName);
if (locations.size() > 0) {
Location locn = locations.get(0);
return weatherService.getCurrentWeather(locn);
} else {
if (mayCorrect) {
for (String name : spellCheckService.getCorrectionsFor(cityName)) {
Weather weather = getWeatherFor(name, false);
if (weather != null) {
return weather;
}
}
throw new CityNotFoundException(cityName);
} else {
throw new CityNotFoundException(cityName);
}
}
} catch (WeatherException e) {
return null;
}
}
}Sure, you can read this code and understand what's going on. But how easy is it to find a bug in this? And how easy it would be to fix that bug?The problem with this code is that it uses way too much branching. An if-else is essentially one statement. What I mean by that is, when you change something in the middle of an if-else, you should keep the entire if-else in mind. The condition
(weather != null), for example, is 3 levels down a decision tree. Before you can touch that part of this function, you should have worked out all 3 paths of the decision tree in your mind; otherwise your change might introduce a bug.Now, the same code written in a way I like:
In this version, the decision tree is only one level deep at any given point. (Weather getWeatherFor(String cityName, boolean mayCorrect) throws CityNotFoundException { if (appConfig.useFakeWeather()) { return appConfig.getFakeWeather(); } List<String> candidates = new ArrayList<String>(); if (mayCorrect) { candidates = spellCheckService.getCorrectionsFor(cityName); } // Actual input must be the first search we do; insert it // as the first item in the list. candidates.add(0, cityName); Location locn = findCity(candidates); try { return weatherService.getCurrentWeather(locn); } catch (WeatherException e) { return null; } } Location findCity(List<String> nameCandidates)throws CityNotFoundException{ for (String candidate : nameCandidates) { List<Location> locations = locationService.findCity(cityName); if (locations.size() > 0) { return locations.get(0); } } return new CityNotFoundException(nameCandidates.get(0)); }
for and try are not necessarily "conditionals", but even if you include them, nesting is only two levels deep. Which of these two versions would you change confidently?If you have been writing code for a while, you will know that what starts like the second version would eventually end up like the first version over time: by changes that happen one line at a time. As you make code changes, check how deep your decision trees get; if it's anything more than two, it's time to restructure the code to simplify it.
12 Jul 2011
Parking cars
I just realised today that my brain has started trusting my subconscious driving skills. This is like leaving phase 1 and entering phase 2. I don't have to focus to stay on one lane, to brake before hitting the car before me, and so on.
Because I don't think much about the fundamentals anymore, now my brain can focus on other things. Now I am learning to park. Parking straight, I found in the first few days (of this Mountain View trip) that I can do much easier than before. Now I am learning to park in reverse.
Well, I am not the kind that would try those things just like that. I am too scared to experiment with cars or bikes :) One day I missed the only parking space available and went past it. Instead of going back all the way and returning to the same place again to park, I tried to park in reverse. I was surprised how easy it was, and I called my brother Saravanan to ask if I was doing something wrong. Apparently parking in revers is easy. He gave me a tip as well: instead of using the rear-view mirrors or looking through the windows, looking through the rear wind shield gives you a better idea of where the car is going.
So I tried that today. First time I parked the car fairly decently; second time was terrible. But the point is I am now consciously experimenting. That's a progress. I'm happy :)
Because I don't think much about the fundamentals anymore, now my brain can focus on other things. Now I am learning to park. Parking straight, I found in the first few days (of this Mountain View trip) that I can do much easier than before. Now I am learning to park in reverse.
Well, I am not the kind that would try those things just like that. I am too scared to experiment with cars or bikes :) One day I missed the only parking space available and went past it. Instead of going back all the way and returning to the same place again to park, I tried to park in reverse. I was surprised how easy it was, and I called my brother Saravanan to ask if I was doing something wrong. Apparently parking in revers is easy. He gave me a tip as well: instead of using the rear-view mirrors or looking through the windows, looking through the rear wind shield gives you a better idea of where the car is going.
So I tried that today. First time I parked the car fairly decently; second time was terrible. But the point is I am now consciously experimenting. That's a progress. I'm happy :)
10 Jul 2011
Subscribe to:
Posts (Atom)


