// Richard Hart / Hates_

Archive
computing

The Code Shall Set You Free

computing, programming

I was recently asked why I don’t comment my code. It’s a fair enough question. There was a time when commenting your code was the done thing. I was once a great believer in commenting code as much as possible and would bash those that didn’t, but now I vary rarely comment my code at all. In my current project of over 1,500 LOC, there are only a handful of comments. Many people will argue this is irresponsible. Well how is anyone supposed to pick up and understand my code, if it’s not commented?

The code should comment itself.

That just sounds silly. It’s like saying a car should drive itself. But it can be done. A lot of this change of heart about comments has come from my commitment to becoming a better developer and spending countless hours reading about the practice of great development, which is something I’ve written about in the past. I’m a big believer that most of the time if you need to comment a piece of code, then it’s either bad code or too complicated. Of course that’s not true 100% of the time, but for the other 99% it really is. There are cases where things need to be explained and especially warned of, but a lot of the time, commenting is just an easy escape from having to do “proper” coding. It took me years and years to get a basic understanding of proper OO and I’ve still got a long way to go to reaching Journeyman levels of understanding, but I would always create large objects with huge and complex methods, when really what I needed were more concise classes with more responsibility for what they should be able to do. It’s not object orientated when you’re focus is on the method and not the class.

A simple example of commented code:

  # Feed the fish.
  def check_food(food)
     # Check if Fish has eaten more then 5 hours ago.
     if self.last_meal_time < 5.hours.ago
       return "Not hungry"
     # Check if the Fish eats this food.
     elsif !self.edible_foods.contains?(food)
       return "Can't eat that!"
     # Make sure the fish isn't being underfed.
     elsif food.amount <  fish_feeding_amount(self.type)
       return "Not enough food."
     # Make sure the fish isn't being over fed.
     elsif food.amount > fish_feeding_amount(self.type)
       return "Too much food."
     end
     # Eat food.
     self.eat(food)
  end

Most comments can be done away with. If we split out functionality into more concise bits, then just reading the code should explain what’s happening better:

  def feed(food)
    return "Not Hungry" if self.recently_eaten?
    return "Can't eat that!" if self.does_not_eat?(food)
    return "Not Enough food" if self.not_enough_food?(food)
    return "Too much food" if !self.too_much_food?(food)
    self.eat(food)
  end

This isn’t a great example as once again the validation of whether the food can/will be eaten should move into it’s own method or class ever. If we’re follow Uncle Bob’s SOLID principles, the above examples breaks the OCP (Open Closed Principle) where entities should be open to extension and closed to modification. If more validation rules were needed, then that would require the code to be modified.

Read More

Gist

computing, programming

[gist id="172116"]

Great find by andhapp.

Read More

Getting Productive

computing, life

I first picked up the Productive Programmer a few months back and after flicking through it I initially thought a lot of it wasn’t relevant and didn’t bother reading it. As I had been playing around with Vim (again!) and thinking about the whole idea of being more productive, I felt compelled to pick it up once more and actually read it. After learning a few new OSX tricks within the first few pages, I was hooked. Admittedly I skipped over any Microsoft related content, but overall the book is full of real productivity gems, and ever since I’ve been on a quest to increase my day to day effectiveness when it comes to using my computer.

My first port of call was sitting down and learning to use LaunchBar properly. I still have a long way to go, but more and more I’m using it to find and open files, as well as small things like quickly playing music and using the extremely handy clipboard history. I’ve stripped my dock of all apps except those that are running. The reason being that there is no need for me to use it as a launcher when I can use LaunchBar to start any app I need, without even having to use the mouse. I’d totally hide it from my screen, but somehow that feels “anti-Mac”.

Secondly I customised my terminal to be more “friendly” and learned some advanced command line techniques courtesy of the Peepcode screencast on the subject. Now I have a load of aliases as well as custom functions which culminate commands I frequently run in conjunction with each other.

I also spent some time learning more general shortcuts as well as trying out some other apps to help in my quest for computing Zen. One is Desktopple, which hides apps which have been in-active for a certain period of time and another is TextExpander, which allows you to create small snippet shortcuts, for example, typing r@ now automatically expands to become richard[at]ur-ban.com. Very nifty.

To try and become more effective overall in my life I’m really trying to knuckle down and keep a track of everything I need to do using Things. As with any todo app, you get out, what you put in. If you don’t really make an effort to use it and dump stuff into it, you’ll never really get anything out and never get anything done. I would have prefered to continue using The Hit List as I have a registration for it, but it would seem the iPhone app is still nowhere in sight. So for now, Things is what it’s going to have to be. I’m also now making more use of Evernote. I regularly email notes to myself and had totally overlooked the fact that I could just email them straight to my Evernote account. So now, any thoughts or ideas I have appear straight in my account thanks to the power of Email. I’ve also installed The Habit Factor on my iPhone to keep a track of my goals. It’s a simple app which lets you set a number of goals and habits, which you can then tick off each day, hopefully leading you to form good habits over time.

Read More

If I was Microsoft

annoyances, business, computing, mac, microsoft

Microsoft recently started their new ad campaign where seemingly normal people try to buy a new computer. They’re given a budget and if they find one that fills their requirements, they can keep it.

The first advert follows Lauren with a budget of $1000. She wants a laptop that’s fast, has a comfortable keyboard and a 17” screen. On her quest for a laptop she visits the Apple store and comes out saying they are over her budget and that she’s just not cool enough to be a Mac person. She has no trouble finding a suitable laptop in regular electronics store and ends up purchasing a HP Pavilion. The second advert follows Giampaolo with a slightly bigger budget of $1500. He wants a laptop that’s portable, has good battery life and power. Once again there is a poke at Apple. This time they are more about aesthetics rather then power and that he doesn’t want to be paying for a brand. And once again he ends up with a HP Pavilion (Oh, HP, that’ll be the brand you just paid for then).

My problem with the adverts isn’t the poke at Apple, but rather they are promoting a non-message. If I’m going out to buy a new computer and I don’t want a Mac or want a Mac but don’t want to save for it, I have no choice but to buy a PC. I can’t see them swaying the people who have made the choice to switch and have the money ready to buy a Mac. So who are these adverts aimed at? There’s no competition in the sub $1500-$2000 range, so is it even worth doing at all. Honda make ads to stand out amongst Toyota, Ford etc. They don’t make adverts in the hope that they’ll stop people that are thinking of splashing out on an expensive sports car, and sway them to settle for a Civic instead. This and the previous ”I’m a PC” series of adverts just end up legitimising Apple’s presence. Apple is a tiny dog biting at the heels of a giant, yet Microsoft feel like they need to gear their entire consumer TV advertising campaign to take them on. The campaign may well be just insurance for Microsoft. How many heads would roll if they decided to ignore Apple completely, or not even bother advertising to consumers, and then their market share tanked. Perhaps it’s better to appear to do something, then to not do anything at all. On the other hand though, could all that money be better spent doing something else.

All of which leads me onto what I would do if I was Microsoft. Simply, I would advertise Windows 7 and IE8. I’d give people hope once again in Microsoft products (Hopefully the products would match the hype). But I’d ram it down people’s throats day and night. “Forget what you thought about Vista, Windows 7 is coming”. “Experience the web like you’ve never done before. IE8 has arrived”. That sort of stuff.

Perhaps that’s what I find so alluring about the “Apple way”. When you see them in use, you feel like you’re being taken to a time and place beyond the normal preconceptions of how technology works. Microsoft used to have that. Microsoft used to ask “Where do you want to go today?”, with the promise of taking you there, no matter how far fetched your dreams were. Now they just state “Your potential. Our passion.”, which leaves you with the sinking feeling that really over the years a massive void has grown between us and them.

Read More

Another New Facebook

computing, internet

I said it before and I’ll say it again. Don’t like the new Facebook? Tough shit. Suck it up.

Read More
Real Time Web Analytics