Jason in a Nutshell

Jason in a Nutshell

Jason Baker  //  Just another random geek. Visit my homepage at http://jason-baker.com

Follow me on twitter: http://twitter.com/jasonsupdates

Jul 17 / 6:52pm

Python metaclasses in depth

Metaclasses have a reputation for being among the blackest of Python’s black magic. As it turns out, they’re actually pretty simple, but they require you to understand some concepts. There are two ways to learn metaclasses in Python:

  1. Skip the details and get straight to a useful implementation of a metaclass.
  2. Understand the core concepts behind metaclasses.

Number 1 is useful, but will only get you so far. If you really want to understand metaclasses, you need to take approach number 2.

With that in mind, I’m going to start with the basics of how classes are constructed in Python. Let’s consider the following class:

class SomeClass(object):
     x = 1
     y = 2

If you’re ready to learn about metaclasses, the above statement shouldn’t require much thought to understand. But let’s stop to think about it a bit anyway. In Python, everything is an object. Therefore, classes are also objects. But if SomeClass is an object, it must be an instance of some class. Let’s find out what that class is:

>>> type(SomeClass)
<type 'type'>

So apparently, it’s an instance of type. We just saw the most common usage of type: to query an object’s type. But have you ever read the help on that function?

>>> help(type)

Help on class type in module __builtin__:

class type(object)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type
 ...

So it turns out that type is not only a builtin function, it’s also a class! How do we instantiate an instance of type? You’ve already seen the most obvious way: using a class statement. But did you know that you can create a class using type?

SomeClass = type('SomeClass', (object,), {'x' : 1, 'y' : 2})

For all intents and purposes, the above statement is equivalent to the prior definition of SomeClass. But this way is ugly and isn’t used very commonly. That said, we’ve demonstrated something: type isn’t just any class. It’s the class of classes. It’s a metaclass.

Let’s go a step further though. How does the compiler generate the dictionary that’s the third argument to type? As it turns out, classes have something in common with functions: they have local namespaces. You might have seen the locals function used like this:

def some_func():
    x = 1
    y = 2
    print locals()

If you execute this, the output is this:

>>> some_func()
{'y': 2, 'x': 1}

If classes have their own namespaces, then they must also be able to use the locals function as well:

>>> class SomeClass(object):
...     x = 1
...     y = 2
...     print locals()
...
{'y': 2, 'x': 1, '__module__': '__main__'}

The only difference here is that the namespace is generated at import time and passed into the type function.

So this is all interesting, but we haven’t really seen anything terribly useful yet. Let’s go further. We’ve seen that type is the class of classes. But if type is a class, then we must be able to subclass it. There are a number of reasons you might want to do this. For instance, you may sometimes want to attach a property to a class rather than to an instance. Let’s do just that:

class SomeMetaClass(type):
    @property
    def z(self):
        print 'In class property z'
        return self.x + self.y

>>> SomeClass = SomeMetaClass('SomeClass', (object,), {'x' : 1, 'y' : 2})
>>> SomeClass.z
In class property z
3

But we don’t want to use this same ugly notation for creating SomeClass. Python provides syntactic sugar for this. We can instead define SomeClass like this:

class SomeClass(object):
    __metaclass__ = SomeMetaClass
    x = 1
    y = 2

A more common use of metaclasses is to create a class constructor. Let’s attach z to the class directly rather than defining a property:

>>> class SomeMetaClass(type):
...         def __init__(self, name, bases, dict):
...             self.z = self.x + self.y
...
>>> class SomeClass(object):
...         __metaclass__ = SomeMetaClass
...         x = 1
...         y = 2
...
>>> SomeClass.z
3

As we can see, we’ve defined a constructor for SomeClass. Now let’s go a bit further. What if we want to change the base class of SomeClass? That can be done, but we have to use a __new__ method. I’m going to presume that you know a bit about __new__ methods. If you don’t, you might want to read up on them.

>>> class SomeMetaClass(type):
...     def __new__(cls, name, bases, cls_dict):
...         new_cls = type.__new__(cls, name, (dict,), cls_dict)
...         return new_cls
...
>>> class SomeClass(object):
...     __metaclass__ = SomeMetaClass
...     x = 1
...     y = 2
...
>>> x = SomeClass()
>>> x['foo'] = 'bar'
>>> x
{'foo': 'bar'}

That should hopefully give you an idea of what metaclasses are and how to use them. If you’re even more lost than you were before, don’t worry. This is just one of those things that requires an “aha!” moment. You might also want to check out Michael Foord’s great Meta-classes Made Easy for a different perspective.

Filed under  //  metaclass   object oriented programming   programming   python  

Comments (1)

Jul 15 / 8:17am

How Celery, Carrot, and your messaging stack work

If you’re just starting with Celery right now, you’re probably a bit confused. That’s not because celery is doing anything wrong. In fact, celery does a very good job of abstracting out the lower-level stuff so you can focus just on writing tasks. You don’t need to know very much about how any of the messaging systems you’re using will work. However, to truly understand celery, you need to know a bit about how it uses messaging and where it fits in your technology stack. This is my attempt to teach you the things you need to know about the subject to be able to make everything work.

Messaging

At the very bottom of celery’s technology stack is your messaging system, or Message Oriented Middleware in enterprise-speak. As of this writing, there are a couple of standards out there in this market:

  • AMQP – A binary protocol that focuses on performance and features.
  • STOMP – A text-based format that focuses on simplicity and ease of use.

Of course, there are a lot more players out there than just this. But these are the two protocols that are the most important to celery.

Now, a protocol is totally useless without software that actually implements it. In the case of AMQP, the most popular implementation seems to be RabbitMQ. The popular implementation of STOMP seems to be Apache ActiveMQ.

Carrot

A good analogy that I think most people can wrap their heads around is the SQL database. STOMP and AMQP are like SQL, while RabbitMQ and ActiveMQ are like Oracle and SQL Server. Any one who has had to write software that works with more than one type of database knows how challenging this can be. Sure, it’s easy to issue SQL commands directly when you just support one type of database, but what happens when you need to support another? One possible solution is to use an ORM. By abstracting out the lower-level stuff, you make your code more portable.

The first thing most ORMs do is provide an abstraction to write SQL queries. For instance, if I want to write a LIMIT query for SQL Server, I would do something like this:

SELECT TOP (10) x FROM some_table

Oracle’s query would look something like this:

SELECT x FROM some_table WHERE row_num < 10

These are different queries, but they are both doing the same basic thing. That’s why SQLAlchemy allows you to write the query like this:

select([some_table.x], limit=10)

This is the functionality that carrot provides. Although most messaging systems are fundamentally different in a lot of ways, there are certain operations that every platform has some version of. For example, sending a message in STOMP would look like this:

SEND
destination:/queue/a

hello
^@

AMQP’s version is binary, but would look something like this in text format:

basic.publish "hello" some_exchange a

Since we don’t want to worry too much about these protocols at a low level, carrot creates a Publisher class with a “send” message.

Celery

Carrot makes it so that we can forget about a lot of the lower-level stuff, but it doesn’t save us from the fact that we’re still working with a messaging protocol (albeit a higher-level one). Going back to the ORM analogy, we can see the same thing happening: we need a layer of abstraction to make dealing with different implementations of SQL easier, but we don’t want to write SQL. We want to write Python (or whatever your language of choice is).

Thus, ORMs will add another layer of abstraction. Wouldn’t it be nice if we could just treat a database row as a Python object? Or, in the case of task execution, wouldn’t it be nice if we could just treat a task as a Python function? This is where celery comes in. See, we could run tasks like this:

  1. Process A wants to run task “foo.bar”
  2. Process A puts a message in queue saying “run foo.bar”
  3. Process B sees this message and starts on it
  4. When done, Process B replies to Process A with the status.
  5. Process A acknowledges this message and uses the return result.

Rather than having to code all the details of the messaging process, celery allows us to just create a Python function “foo.bar” that will do the above for us. Thus, we can execute tasks asynchronously without requiring that people reading our code know everything about our messaging backend.

Hopefully, this gives you a high-level overview of how celery is working behind the scenes. There are a lot of details that I’ve left out, but hopefully this provides you with enough knowledge that you can figure the rest out.

Filed under  //  amqp   carrot   celery   messaging   programming   python   rabbitmq  

Comments (0)

Jan 6 / 6:34pm

How to have a side project

If there's one part of a programmer's professional development that is harder than it seems, it's starting a side project. I, for instance, love to program. The funny thing is, that programming is the easiest part of programming (those of you who are true programmers will know what I mean). The hardest part is starting to program and finishing your work.

That said, I do have a few bits of advice for programmers who want to start a side project. If you follow my advice, you probably won't write the next big thing, nor will you have a side project that will stand out particularly well on your resume. But you'll probably write something that will make you a better programmer, and that's the point, isn't it?

Here's what I've learned about starting a side project:

#1 - Have a good time

This is the first point on here for a reason. And I should hope it's self explanatory. However, note that there are good reasons for making tradeoffs here. I'll note them below, but there are some things that shouldn't be tradeoffs. For instance, don't make tradeoffs based on how it will look on your resume or what the latest and greatest technology is (not that either of these can't be fun).

#2 - Learn something

I've known some programmers who live by the mantra "I'll learn it later". Here's the problem: later never comes. You're just going to have to accept the fact that you as a professional need to have some responsibility for your own development. Fortunately, a side project is just the way to take on that learning.

#3 - Make it timeless

Here's where you may have to make tradeoffs in terms of fun. The most fun things are usually a hassle to maintain. For instance, suppose you write a new twitter client. I can guarantee you that at some point in time, the twitter people will change their API in such a way that will break your code. Granted, this isn't a big deal if you put a lot of time into it. But that's the problem: no matter what you do, there's going to come a point in time in your life that you have something else to spend your time on or otherwise just won't want to deal with your side project. That's normal. What you want is something that you can come back to after that time is up.

This is part of the reason why I chose pysistence for my project. Aside from keeping up with language changes, it's doubtful that functional data structures are ever going to change such that my code will break.

#4 - It doesn't have to be code

I'd probably say that the blog you're reading right now is my most rewarding side project. Don't underestimate the value of doing things that aren't coding. It's fashionable to talk about how bad it is to be "all talk and no game", but the fact of the matter is that reading and writing about programming helps you to solidify some of the ideas that may be forming in your head.

#5 - Make it something you'll use

This is the last point, but that doesn't mean it's not important. If you write something fun, you'll abandon it as soon as it stops being fun. If you write something useful, you'll have a reason to keep coming back!

Filed under  //  programming   side projects  

Comments (2)

Nov 29 / 9:07am

My five rules for writing good code

I just came across a blog post that outlines 5 rules for writing good code.  I agree with them for the most part.  But this subject is extremely subjective and will vary from person to person.  Therefore, I'd like to write up my own rules for writing good code.

Keep it simple

This is the YAGNI rule.

There are often times when we want to try to solve problems we don't have.  You must resist this urge.  It's far easier to make simple code more complex than the other way around.  This is usually more of a challenge than it looks.  It's a sign of good code that you constantly find yourself saying "any idiot could have put this together".  Reality is that idiots only write simple code when the problem is easy or when they get lucky.

...but not simplistic

You can call this the SYDNI rule.

Albert Einstein said it best when he said "Everything should be made as simple as possible, but not simpler."  I've seen too many "simple" hacks that ended up causing more of a maintenance problem than it would have been just to write something more complex.  As good a thing as simplicity is, you need to be realistic.  Don't try to make a simple solution match a complex problem.

Abstraction is your friend

So what do you do in those situations where you need to use a complex solution?  Do you give up all hope and just write some horrible piece of crap?  No, you find a way to make that complexity easier to manage.  This is where abstraction comes in handy.  

For instance, Lisp introduced the concept that "it's all data". This makes it easier to understand things.  If you want to know what something is, you already know that it's data of some kind.  What is a function?  Data.  What is a list?  Data.  What is code?  Data.  This has a profound effect upon your ability to understand things.

Follow guidelines

Jeff Atwood would call this following the instructions on the paint can.  We tend to sneer at the idea of "best practices", but reality is that they're necessary.  There are a lot of problems out there that people have already dealt with and solved.  Why waste time not learning from others' mistakes.

...but don't worship them

As the old cliche tells us, rules were made to be broken.  Some of the most well known design patterns break the guidelines and have some of the worst code smells.  In fact, sometimes the guidelines conflict with each other.  Thus, don't blindly follow guidelines without knowing their purpose.  Instead, understand the rules, know when to follow them and when following them is the greater of two evils.
Filed under  //  guidelines   programming  

Comments (0)

Oct 29 / 6:42pm

The devil's in the details

There are two schools of thought in the programming world:

  1. Explicit is better than implicit (configuration over convention)
  2. A developer should only have to program the unconventional aspects of a program (convention over configuration).

We'll call #1 the Python school and #2 the Ruby school. In fact, I
would argue that this is an issue that's at the core of whether code
is considered "Pythonic" or "Rubyic" (I doubt the last one is a word).

So which school of thought is right? I personally think they both
are. It doesn't really take a whole lot to demonstrate that the
Python school of thought isn't always right. Think about it. Did you
know that the Python runtime has a component that goes around deleting
objects from memory totally implicitly? How unpythonic is that?

The Ruby school of thought takes a bit more work though. After all,
if it's unconventional, why should you have to configure it? Of
course, the problem here is in defining "conventional". What's
conventional to me is likely unconventional to others. And what's
conventional to others could be unconventional to me.

I wish I had more advice on how to reconcile these two schools of
thought. The truth is that I struggle with them daily. But I think
having an intuition about this is the dividing line between
"experienced programmer" and "newb". After all, if programming were
merely about "make everything explicit" or "make everything implicit",
any idiot could do it.

I think this is also the core skill for writing readable code. You
need to determine what details are relevant to each piece of code.
Whatever the case, you need to make a conscious decision as to what
details shine through and what details you obscure. Because if these
things happen on accident, they're almost guaranteed to be wrong.

Filed under  //  programming   python   ruby  

Comments (1)

Oct 15 / 5:14pm

Persistence is a subset of immutability

Whenever I tell someone I'm working on a persistent data structure 
library for Python, I almost always get the same response: 
"Persistent means that something is stored across process boundaries. 
I think you mean to say immutable data structures."  In fairness, this 
response is understandable.  But it's not quite right.  I'm going to 
address both of the points in the above statement. 

Persistent means that something is stored across process boundaries


Did you know that letter can mean a note you send through the mail or a thing you use to make up words?  Did you know that programming can mean writing computer programs or mathematical optimization?  I'm sure if I sat down long enough, I could think of more examples.  The point is that the language we speak is full of ambiguities and whatnot. 

Before we consider the computer science meaning of persistence, let's 
look at the common meaning.  If we look at the word persistent in 
Merriam-Webster, the first two definitions we run into are: 

1 : existing for a long or longer than usual time or continuously: as 
a : retained beyond the usual period  b : continuing without change in 
function or structure 

The meaning Python programmers tend to think of is the definition in 
part a.  That is, a persistent data structure is stored beyond the 
usual period (the process's lifespan).  However, when I talk about a 
persistent data structure, I tend to mean the definition in part b. 

Going with definition b, we can see that lists for example aren't persistent: 

 
>>> a = [1,2,3] 
>>> b = a 
>>> b.append(4) 
>>> a 
[1, 2, 3, 4] 
 

Tuples, however, are a different matter: 

 
>>> a = (1,2,3) 
>>> b = a + (4,) 
>>> b 
(1, 2, 3, 4) 
>>> a 
(1, 2, 3) 
 

Ah, but here's where the second part of this post comes in.

I think you mean to say immutable data structures

In the case of the tuple, you are correct to say that a tuple is 
immutable.  And I'm correct in saying that a tuple is persistent. 
Just like I'd be right to say that I own a car while someone else 
claims that I own an automobile.  However in both cases, they can't 
quite be used interchangeably.  After all, an automobile isn't 
necessarily a car.  It can be a truck or a van or an SUV. 

In the same sense, a persistent data structure is effectively 
immutable.  But an immutable data structure isn't necessarily 
persistent.  Let's consider the definition of both of these words from 
wikipedia: 

Immutable object


In object-oriented and functional programming, an immutable object is 

an object whose state cannot be modified after it is created. 


Persistent data structure


In computing, a persistent data structure is a data structure which 

always preserves the previous version of itself when it is modified; 

such data structures are effectively immutable, as their operations

do not (visibly) update the structure in-place, but instead always

yield a new updated structure. 


These definitions have a lot of common ground, but there is some 
difference between them.  Let's consider a data structure that is 
immutable but not persistent.  Prepare yourself.  This will be 
complex.  Ready? 

 
>>> 1 
1 
 

Has your mind been blown yet? 

In this sense, the number 1 is definitely immutable.  You can't change 
it.  It's always the number 1.  However, it's not persistent.  How do 
you update the number 1?  No matter what you do, it will always be the 
number 1.  Sure, you can get 3 by adding it to 2.  But in a 
mathematical sense, that's not really changing the number 1. 

In this sense, the number 1 is atomic.  It simply doesn't make sense 
to modify it.  Heck, you can't even copy it: 

 
>>> from copy import copy 
>>> a = 1 
>>> a is copy(a) 
True 

Conclusion

With this semantic difference in mind, remember that it's just that: 
a semantic difference.  Call them Bob if you want to.  However, bear 
in mind that when someone uses the word persistence in this way, 
they're not being inaccurate.

 

Filed under  //  functional-programming   persistence   programming   python  

Comments (0)

Oct 11 / 8:30pm

The other side of being a free electron

So my last blog post on the subject of free electon programmers seems to have been successful.  I've gotten responses in two forms:

  1.  This is so self-serving.  What about the other people on your team?  They have a special personality as well!
  2.  This really hit home with me.  But I still don't really feel like it's really helped me much.
My response to these points:  I couldn't agree more!  Here's the thing though:  it's much more difficult to say what other people want.  On the other hand, what better expert is there on what you want than yourself?  That was kind of the idea behind my last post:  "if you're struggling with somebody who has a personality like this, here's what you need to understand".

That said, I've learned a thing or two in my time that I think I can share.  Although this is mostly related to free electrons, a lot of this is useful for any programmer.

Other people have emotions

If I had to choose one area where INTPs and INTJs have the most difficulty, it's here.  This goes beyond your typical geek social idiocy.  Heck, it hasn't been more than a decade since it actually hit me that other people expect me to acknowledge their feelings.  Now I know this feels dumb.  And yes, you'll still forget.  But you'd be surprised at how much this helps you get along with people.

You see, people expect you to react in certain ways when they say something.  When they tell you a story about something that's exciting to them, show some interest no matter how inane it sounds.  When people are angry about something, empathize with them and try to find some common ground even if it's difficult.  If it's you they're angry at, resist the temptation to respond in kind.  If you're calm and rational about it, people will usually be willing to have a calm and civil discussion about things.

And this is the most important part:  if you did something wrong, apologize.  I know this can be hard, but if there's one thing that's never ceased to amaze me, it's humankind's capacity to forgive and forget.

It's a good thing that other people don't understand you

We've all been there before.  You have to spend an hour's worth of your time explaining the simplest and most benign thing in the world to someone else who just doesn't get it.  If the person is truly an incompetent idiot, you should be discussing things with your manager.  Otherwise, you should take the person's bewilderment as a complement:  you understand something they don't and they want to learn from you.  After all, if they really didn't want to learn from you, they'd probably just ignore you.

In fact, you'd do well to take a page from their book.  Next time you find yourself feeling that someone else's opinion is the stupidest thing ever, don't criticize them.  At least not immediately.  Instead, ask questions to get at why it is they feel the way they do.  You never know, they could have seen something you haven't.  You see, not only is it a good thing that people don't understand you, but it's also good that you don't understand other people.

Fight the urge to always go it alone

Most of the time, it's easiest for me to just go off on my own for a week or two, code something, and come back when it's done.  The thing is that I almost always end up with something that nobody else really gets.  You don't have to be joined at the hip with the entire team all the time.  But it's helpful to just spend some time at least explaining your code to someone else.  This is good even if for no other reason than it helps you learn what trouble other people are going to have with it.

Code reviews are really awesome for this.

People like you

Yes, even when you're being a jerk.  There have been several occasions where I've gotten into some rather heated arguments over mundane issues with people that were admittedly my fault.  And not once has anyone died because of it.  You see, if I had a choice between working with people who disagreed with everything I say or working with people who love all of my ideas, I'd go with the argumentative asshole every day.  People who think critically about other peoples' ideas are a rare asset for a software company.  And your coworkers will appreciate this if they truly want to do their jobs.

Choose your battles

Free electrons have a very ordered way of looking at the universe.  We tend to get frustrated if something disturbs that way of thinking.  And that's led me to some long arguments.  

Next time you get into an argument with someone, ask yourself this:  "If I just let this person win, how big of a deal will it really be?"  If your answer is "not a big deal", then just let them have their way.  If you can't answer the question, then you probably are arguing over nothing and should let them have their way.  

However, if your first instinct is "this is a huge deal" and you can explain why, then stick to your guns as much as possible.  Generally, people will be willing to cede the important fights to you if you stick to this strategy.

Conclusions

Admittedly, I'm borderlining on making free electron programmers the new "duct tape programmers".  If that's the case, then so be it.  If there's one thing that nobody can say about Joel Spolsky's piece on JWZ, it's that people didn't consider his way of thought.

However, I'd like to hear feedback.  What other piece of advice do you have for programmers like me?  Is there anything else that I'm missing or getting wrong?
Filed under  //  programming  

Comments (0)

Oct 10 / 12:18pm

The "free electron" programmer

I have to say that I really liked reading Rands in Repose's excellent blog post about "free electron" programmers.  Unrelated side note:  Did you know that Simply Hired has a graph showing the median salary of these kinds of programmers?  Apparently the median salary is around $78,000.

At any rate, I feel that this is a subject I can add to.  I say this because I consider myself a free electron.  And I'm not saying that to be cocky either.  It's just that Rands's description of the free electron is almost a word-for-word description of the Architect (INTP) or maybe the Mastermind (INTJ) personality type.  I just so happen to be an Architect. I think that people with both of these personalities were almost born to be programmers.  They're naturally inquisitive and love nothing more than to understand complex systems.  But they can really be destructive if not dealt with properly.  And they're so rare (about 1-5% of the population) that many people may simply be unaware of how to deal with them.

At any rate, here is a list of general pieces of advice to get along well with your free electron (in no particular order):

Listen to them

Alright, so I lied when I said this was in no particular order (at least for this point).  This is the single most important thing to do to get the most use of your free electron and to keep your free electron happy.  Yes, we come up with some rather zany ideas that seem to not be grounded in reality.  And a lot of the time, that may be true.

But you also need to understand that free electrons have an unmatched ability to understand complexity.  This means that oftentimes, they may understand something you don't.  And sometimes, it is difficult to put complexity into words.  Thus, what seems like a zany idea to you might actually be a perfectly rational way of looking at things when you understand all of the details.

But you shouldn't just listen to them when they're right.  You also need to give them thoughtful consideration when they're quite obviously wrong too.  Nobody likes not being listened to, but free electrons hate it worse than anyone else.  And more to the point, they're driven to make people listen to them by whatever means are available.  If this leads to an overly long and drawn-out discussion of whether to use camel-casing or underscores, then so be it.

Be logical

A lot of people will come to the conclusion that free electrons are hard to get along with and inflexible.  However, this is a person who hasn't learned to frame their arguments properly.  A lot of people tend to put things in emotional terms.  Emotional arguments have little impact on free electrons.

Remember how I said that free electrons hate not being listened to?  Responding to a free electron's arguments with emotion is equivalent to not listening to them.  However, you will find that a free electron is willing to listen to arguments about how their idea is the stupidest thing you've ever heard as long as you can present logical reasons why.  

Conversely, they're likely to not take others' feelings into consideration in discussing things.  Therefore, you need to understand that if they say something that comes off as insulting, they probably didn't mean it as such.  If this bothers you, it is helpful for you to point out when they're being rude as long as you do so calmly.

They may not realize how talented they are

Remember how I said that there are two personality types that can be free electrons?  Here is the area that differentiates them.  Masterminds tend to be very much aware of their talents, so this section doesn't really apply to them.  Architects usually aren't.  They assume that every programmer can understand what a monad is just by reading the wikipedia page on them.  Sometimes, you just need to get them to slow down for the programmers who don't spend their weekends writing compilers for fun.

They need direction sometimes

Free electrons tend to be focused on the big picture.  However, they're not very detail-oriented.  This means that they tend to be great at getting an overall framework for a solution set up.  They tend to not be very good at putting the finishing touches on things.  Thus, free electrons tend to be good at starting projects.

This is all well and good, but sometimes you need them to stick with something.  Typically, a free electron will want to move on to something else when they feel they've learned everything they can from the current project.  You can help to counteract this effect if you can point out other interesting things that are still left to do in the current project.

You have to be careful with this approach though.  If a free electron is forced to work on a project they find uninteresting, they will find a way to make it interesting.  You don't want to assign them to a little modification of one method just to find out that they've changed your piece of software's entire architecture.

...but most of the time you should just leave them alone

Free electrons are very autonomous individuals.  They'll probably dislike pair programming and hate micromanagers.  But that's fine because free electrons can do amazing things with little to no direction.  When teamwork is required, you should keep interactions with the free electron mostly at the high level.  If there's too much tedium involved, they'll probably turn very toxic very quickly.

Also bear in mind that free electrons tend to keep their thoughts to themselves.  In other words, if it seems like there are 10 levels of intricacy behind everything they say, it's because there is.  There are two things that can cause this:

 1. If your free electron is an Architect, they could very well assume that those 10 layers of complexity are obvious to you.
 2. They may just not feel like explaining it to you.

While number 2 makes free electrons sound like snobs, take a moment to think of the world from their perspective.  As stated, free electrons are ridiculously rare.  That means that they've spent their entire life trying to explain their way of thinking to people who will probably never be able to understand it because they just don't think the same way.

This leads them to unconsciously come to the conclusion that explaining things to people is hard and avoid it at all costs.  This makes getting the full story out of a free electron a bit like pulling teeth.  The best approach is to show some initiative.  Get them to talk about those subjects in normal conversation.  Chances are, they'll be more than happy to share if they feel you're genuinely interested in the subject and not just asking about it because it's your job.

They can't do it alone, no matter how much they claim otherwise

Free electrons can do amazing things that no other engineer can do.  But they have weaknesses that need to be balanced out.  This means they need good managers to help direct them and good engineers who can make up in the areas they lack.  The best free electrons are the ones who realize this.  Don't expect them to ever admit it to you though.
Filed under  //  programming  

Comments (11)

Sep 6 / 9:34am

The Rise of SYDNI, or YAGNI is Only About Problems, Not Solutions

I've got a new programming methodology to propose. I call it SYDNI
(Sometimes You Do Need It). It is a response to the problems that I
see with YAGNI. In fairness, I don't dislike YAGNI. In fact, I agree
with it 100% (well, maybe 95%). But to truly appreciate it, you need
a bit of context.

On YAGNI


 
I've almost started thinking of YAGNI almost as a recursive way of
thinking. That is to say that I've begun to think of YAGNI as being
something that uses itself to implement itself. Allow me to explain.

What is YAGNI?


 
YAGNI stands for "you ain't gonna need it." I don't want to make this post
an in-depth discussion of what YAGNI actually is, so click the
Wikipedia link if you aren't familiar with YAGNI. The important thing
to take away from reading about YAGNI is that it's saying that you
shouldn't implement functionality if you don't need it.

What YAGNI ISN'T


 
YAGNI sounds like a pretty straightforward way of thinking. And in a
lot of ways it is. But it's more nuanced than one may think at first.
 The "recursive" element of YAGNI that I speak of above is that YAGNI
(in my opinion) is a very specific solution to a very specific
problem, and that problem is over-engineering.
 
And YAGNI does its job well (especially in the context of Test Driven
Development). I tend to find myself throwing out a lot less code when
using YAGNI.
 
A lot of people take YAGNI to mean that the simplest solution is
always the best. That isn't the case. Or at a very minimum, that
shouldn't be the case. There's a key thing about simplicity
that should be understood: it's defined by the problem, not the
solution. This is key to understanding why YAGNI is so useful. Once
you've gotten to the point of choosing a solution, YAGNI is no help to
you. Thus, you have to use YAGNI to choose problems, not solutions.

You're not in school anymore


 
In school, things are always so simple. You're assigned a problem.
And you're given a grade based on how well you solved that problem.
The real world is more complex.
 
You see, people too often forget that software developers don't just
define solutions to problems. After all, aren't all feature requests
nothing more than a statement of a problem? And isn't choosing
software features a decision about what problems you will solve?
 
However, once you've chosen a problem to solve, there's still the
issue of how to solve it.

Sometimes You Do Need It


 
In solving a problem, YAGNI's usefulness starts to fade. It does have
some importance. You do have to make sure your solution is solving
the problem you set out to solve. However, beyond that, YAGNI just
doesn't apply. In fact, it is likely harmful. That's where SYDNI
comes in. Although SYDNI's name is something of a jab at YAGNI, the
principle itself isn't. Instead, SYDNI can be thought of as a
complement to YAGNI. A yin to YAGNI's yang (alliteration for the
win).
 
Oftentimes, thoughts may enter your head that start with something
like "we'll never need..." or "this will never have to...". This kind
of thinking is helpful when choosing the problem to solve. However,
it's destructive when choosing a solution. In a couple of years,
there is only one thing that will be certain about the software you're
writing: it will be different. And it will be different in ways you
can't have predicted or imagined. If you're using YAGNI
appropriately, you're choosing the easiest problems to solve.
However, at least a few of these problems come out of left field.
 
Therefore, I would put SYDNI this way: ideally, a piece of software
will be no more simple or complex than the problem it is trying to
solve. Therefore, there is danger not only in solutions that are
overly complicated, but there is also danger in solutions that are
overly simple.
 
This leads to another conclusion: if SYDNI is followed appropriately,
the complexity of your source code is a direct measure of how complex
the problems it is solving are. The reverse is true as well. The
complexity of the problems you're solving is a direct measure of how
complex your source code will be.

But I don't live in an ideal world!


 
The key hole in SYDNI is the word "ideally". Unfortunately, some
problems just don't have perfectly compatible solutions. Therefore, a
key decision to be made is whether it is better to err on the side of
over-engineering or under-engineering a solution. We are now delving
into the realm of many disputes between programmers. Many people
(mis)educated on the arts of YAGNI will say that it is always better
to tend towards under-engineering. If this were true, YAGNI wouldn't
be as useful to as many people as it has been.
 
Even more unfortunately, there is no "one size fits all" answer of
whether it is better to over-engineer or under-engineer. It is highly
situational and care must be taken to arrive at the appropriate
solution. If you don't believe me, consider the following two
questions:

  1. Which life support machine would you rather be hooked up to?
    •  A machine whose software developers always did the simplest thing possible 
    •  A machine whose software developers went out of their way to anticipate possible problems and planned for each of them 
  2. Which one-page web app do you feel would be easiest to maintain?
    • An application that is implemented as two or three source files and a few database tables 
    • An application with a highly normalized database, highly modular source, and great flexibility 

 

I should hope that the answer to number 1 is obvious. And why it is
the correct answer should also be obvious: if you missed a particular
contingency, people can die. Thus, it makes sense to err on the side
of over-engineering.
 
But number 2 is a little bit less obvious (and maybe more debatable).
However, I would err on the side of under-engineering. After all, no
matter what changes come up, a one-page web app is still a one-page
web app. The worst case is that the app would be rewritten from
scratch. That's not to say that you need to throw caution into the
wind and ignore normal good practice. Rather, it's saying that it's
not really a good idea to stress much over how maintainable that
application is.
 
Therefore, when deciding on a solution, there are two things that need
to be decided upon beforehand:
 
 1. How complex the problem is.
 2. Whether under-engineering is more harmful than over-engineering.
 
Once you get those two things squared away, it should be easy to get
an idea of how complex the solution should be.

Filed under  //  agile   programming   sydni  

Comments (0)