Python Function Behavior and Exceptions

Long-winded writeup for a simple solution.

Advertisements

I had an interesting discussion at work today regarding how to handle the behavior of a particular function. In our underlying code, we wanted to be able to call a simple ‘getter’ to retrieve the name of some object. However, sometimes the object in question won’t exist, so the question came up of how to act in this case. Since we cannot access this property itself (it’s all handled in underlying C#/C++ code) we couldn’t just reference it’s name directly, and instead needed this ‘getPropertyName()’ function.

We got stuck on two implementations for this – simply return ‘None’, or raise an exception. We discussed this for at least 30 minutes, arguing the pros and cons of requiring a ‘try:except’ block around the call in question versus retrieving the property and using an ‘if’ statement to see if we got the name. However, neither side seemed to really stand out as the ‘best’ option.

After a while I came up with what the team thought was a clever solution to this problem. One of the members of the team is an only child and so I asked them the following question:

What is your sister’s name?

The response to this question wasn’t just awkward silence. It also wasn’t ‘None’. They instead told me that they don’t have a sister. I knew exactly what to do after receiving this response, and so did everyone else.

This, to myself and the team, seemed quite a lot like raising an exception… (perhaps a SisterDoesNotExistException()?) If I was trying to compile a list of siblings and I got that response, I’d have to change my behavior, and that was very similar to the purpose of the function we were discussing.

We discussed the necessity of additional functions, such as (to continue using the example) ‘doesPersonHaveSister()’ to check first before requesting the name, but we felt it was an extra step that was solved by simply having the exception being raised. I think the signature we came to more closely matched the behavior we wanted. If we were compiling that list of sisters around the room, it feels more natural to simply ask:

What is your sister’s name?

instead of:

Do you have a sister? If so, what is her name?

If someone gives a name, then we’re all set. If someone instead replies that they don’t have a sister, then we’ll take care of that exception case when we reach it. Obviously this isn’t a perfect example, such as if someone had multiple sisters, but as far as our needs it worked great.

Thinking like this really allowed us to step back from the code and come up with a more natural solution that matches what we’re trying to do. I believe programming isn’t just instructions with the computer, it’s a conversation with yourself, the machine, and other developers on the project. Sometimes we find ourselves worrying so much about the code that we lose track of how we already handle this problem on a daily basis.

Mechanical Numpad Project

Finally got around to building my first keyboard!

I wrote up a full guide on this project, which you can find here

I’ve recently gotten into the mechanical keyboard community, and am very interested in designing and building my own keyboards. I’m not going to get into the reasons why someone might want a mechanical keyboard, and the benefits of them, but I am very happy with how my first project turned out.

I recently picked up a mechanical keyboard for work after using one at home for years. I decided to go for Brown switches due to their low noise yet they still have the tactile feedback I’ve grown to expect with my Blues that I use at home.

So, I selected a small, cheap mechanical keyboard that won’t take up all my desk space and also won’t irritate all of my coworkers. So far, I have been quite pleased with it.

Qisan Gaming Keyboard Mechanical Wired Keyboard

You might notice that there is no numpad or function keys! The function keys I can live without (they are actually mapped to the number keys when I press the fn key, so I have access to them) but I learned that I actually use the numpad quite a bit doing certain kinds of work.

I also had just picked up some 1976 Keycaps, since I really enjoyed the look of them.

 1976 Keycaps For MX Switches Mechanical Keyboard

So, I had these keycaps, but I didn’t have a numpad to put them on. You can see where this is going…

IMG_20170906_215221

So, I built my own numpad. It’s made out of lasercut hardwood (Walnut for the faceplate) and uses Brown switches. I did all of the woodworking and finishing myself, and also wired up and programmed the electronics.

The keycaps look great with the dark walnut faceplate, and the wooden look gives everything a really cool feel. I’m very happy with how it turned out. Now, all I need to do is make a hardwood case for the magicforce…

IMG_20170911_103034

As you can see, the metal look doesn’t match quite as well as the wooden look. Perhaps in the future I’ll make a new case for the rest of the keyboard.

I also plan on creating a step-by-step guide on how I designed and built the numpad so that anyone else interested in joining my DIY Mechanical Keyboard adventure can follow along easily. I haven’t been entirely pleased with the resources I found online, so I think having a single resource that goes into full detail on everything will really help people out.

Either way, this definitely isn’t my last keyboard build. I would love to eventually sell these (hah!) but I have a lot of practice and refinement before I can do that. I’ve learned quite a lot about woodworking and CAD design that I hope to use to improve my next project!

Until then, I’ll be quite pleased with my current work and looking ahead to what I can build next!

IMG_20170911_103020

Working on Climbeleon

Originally Posted 2/2/2016

What is this about?

I’ve been working on a game called Climbeleon lately, and figured it would be good to take some notes down for anyone else who is a beginner hobbyist developer.
Climbeleon is an action game where the player has to match color-coded obstacles while climbing to score points. Going too slow or incorrectly matching a color results in a Game Over.
You can find my project write-up for Climbeleon in my projects page.

Why Climbeleon?

Although I’ve worked on a number of projects before, I decided to make Climbeleon my first full project for a number of reasons:

  • Simplicity
    • Climbeleon, as far as modern games go, has a very simple rule set. A less complicated game means I can focus less on design, and more on implementation
  • Iteration on an established genre
    • There are a number of games with a similar premise as Climbeleon; my favorite of them being Escape. By adding features I felt were missing, I know my feature set very well and how it will play out
  • Easy to Prototype
    • The main game play prototype for Climbeleon was completed in less than two days. I have spent several weeks on other projects and do not have anything remotely playable. Climbeleon is a great experience for learning what and what not to do

In addition, it was a game I had been thinking about and I really wanted to explore its possibilities!

A Work in Progress

The following are a few whiteboard designs and some early screenshots from development:

This slideshow requires JavaScript.

So what have you learned?

I’ve made a ton of mistakes during this short process, and I couldn’t be any happier about it! Although I could have told myself several of these things at the beginning with my experience as an Engineer, its good to have confirmation that these issues will creep up on my personal hobbies as well. A few things to keep in mind, whether you’re just getting started or have come across these issues before in the past:

  • Keep It Simple, Stupid!
    • The KISS adage is tried and true no matter where you go, but this mistake will always creep up on you. The excitement of making great progress on something you have passion for is going to leave you with a ton of ideas in your head. Make sure you know what your gameneeds before you start implementing what you want. Having more things than necessary going on is surely going to confuse the players and yourself!
  • Make a plan
    • Day in and day out you will hear about various methodologies on how to work efficiently. Whatever it is that you prefer, make sure that you have some sort of plan, even if it is simple. Not having a plan (make sure you stick with it as well!) will cause you to go off track (see: KISS) and you’ll find yourself undoing work that never actually contributed to the game
  • Track your work
    • A gold star on a calendar, or a notebook full of crossed off items. Whatever you prefer to use to track your progress will help you keep motivated and see both how far you have yet to go, but also show you just how far you’ve gone. Knowing what you are capable of and getting that sense of accomplishment is great. I recommend a service such as Trello
  • Have a customer
    • If you are lucky enough, the only customer you’ll ever need is yourself. Since most of us aren’t lucky, you’ll want to have someone else make you keep yourself on schedule. Find a friend and provide them your deliverables. Pick a date for your ‘release’ and make sure things are ready by then (even if you miss it!) It doesn’t matter as long as you can keep yourself aimed at a target and heading towards your goals
  • Have Fun!
    • And finally, make sure you have fun with what you are doing! I didn’t complete my old projects for a number of reasons (experience, time, poor planning) but most importantly it was because I was not having fun working on them. In the future I will definitely revisit them, but I have developed more skills and knowledge doing something simple that I liked instead of something complicated I didn’t

Of course this list is just the tip of the iceberg. I plan on discussing more in the future on my progress and will definitely share more of what I’ve learned.