How to write really good code

January 1st, 2016

1) Test *every single line*. I generally execute whatever I’m writing about once every 2-3 lines of code, once I’ve got the skeleton up. If you can’t do this quickly, change your workflow so you can.

2) Use human-language function names, variable names, method names, and class names. If a loop won’t fit on a standard IDE screen, use a human language iterator name instead of i.

3) If you find yourself cutting and pasting code more than once, use a method or a function.

4) If you’re not sure exactly how something is going to play out (like a regular expression, library system call, etc) create a very small program that only tests the way you are using it. This will encourage you to test out the possibilities involved more thoroughly, and once you get used to doing it it can be a very fast thing to do.

5) In #4, deliberately create bad data. Pass in empty strings, negative numbers, nulls, strings with SQL injection in them, strings that are too long, email addresses with no @ sign, and the like.

6) If you have a try {} catch {} – make sure you do something useful in the catch. If you need to break this rule (and occasionally you will) write a very descriptive comment as to why.

7) Know when you’re not in the headspace to code. Don’t try to program when you’re not capable of simultaneously seeing things as black and white and as shades of grey. Only you will know which emotions and thoughts make you write bad code – but in general, if you’re not in the place to program, don’t try. The rest of the team thanks you. Coding is the very essence of explaining rational thought about a subject in very very small steps, so if you’re not thinking rationally, you’re not going to write good code.

8) If you’re working on a very big project (20k+ lines), try to do all your developing in little testbed programs (as in #4) until you’ve got it working completely correctly. It is much faster to build and run small programs, and you also stand a much better chance of not stepping on someone else’s work

9) Use source code control. Check in early and often.

10) If you have a group of related data (for example, information about a customer), use a class. Even if it’s just carrying data – later you may find reasons to add methods to it. There’s a fine line to be walked here, however. You don’t want to use a class for the customer’s phone number’s format string. Usually.

11) Once you know and understand how to write code, you will see that a lot of the things your teachers tell you are basic rules are meant to be broken, occasionally. When you do break them, however, comment on which one you broke and why.

12) Build quality stuff. You never know when you will have to maintain it.

13) Refactor and rewrite. You will often get much better results the second and third time you write the same function. I actually prefer to write most things in a prototyping language (php, perl) before writing them in a compiled binary language, and I think switching languages also helps me write better code.

14) I generally write out psuedocode in human-readable language (english, in my case) before I start programming. This encourages me as a developer to think through what I’m trying to do before I start thinking about breaking it down into if/then/else

15) Every function should have a sane default path through it. switch() is a very powerful and useful tool because it encourages this. Sometimes the sane default thing to do is throw a exception.

16) For every variable that comes from ‘the outside world’ (a human out there on the net, a A/D converter, etc) make sure your code can cleanly handle completely unexpected values. What would happen if there were special characters? If it’s a signed type, did you handle negative? If it’s a nullable type, do you handle null?

17) While you’re doing #1, notice where the ‘pain points’ are. Use a profiler, or just capture high resolution timestamps. Figure out what ran slow and how you can make it run faster. This can be a fine art – for example, knowing which types of data sort will run better on the database engine and which will run better natively in your code takes quite some time to master.

18) When refactoring – if the source code is getting shorter, you’re doing it right.

19) Beware of excess convolution. Occasionally you will find you have done something silly like failed to initialize a useful variable set early in the process and instead calculated the value for that variable all over a set of nested function calls. If it feels confusing reading it, you probably need to rethink your approach.

20) Above all, have fun. Building really high quality things can be a great joy, if you let it. Remember to keep a positive attitude, and always work on having more patience. If you find yourself getting angry, you are probably not thinking rationally, see #7.

21) Remember, *nothing is set in stone*. One of the joys of programming in this era is you can go back and change your mistakes.

Root causes

December 22nd, 2015

One of the things I find frustrating about the current crop of politicians is that none of them are addressing root causes.

In particular, I think me and Bernie agree about the end goals. But most of what he’s saying are repeated band aids on symptoms of the problems with our resource allocation system. He’s not talking root causes. Raising the minimum wage is a example of a temporary band aid. The politicians should be talking about how our economic system can not accurately represent reality – by definition, money as we’ve currently conceived of it is a zero sum game. And reality is *NOT* a zero sum game.

Money is supposed to be, as far as I can tell, a medium for making our resource allocation system work – it’s supposed to model value. Now, I’ve already talked about the fact that any time we print more money, everyone is convinced the money is worth less, even though most people would agree that the amount of value available to us increases every year. Also, different resources have different values to people at different times in ways that are in no way related to the flow of money.

I still insist that if we have empty houses and homeless people, if we have people being paid not to grow food and people starving, we have a resource allocation system that is failing and we should be looking at how to improve it. But making laws about the minimum wage won’t do any long-term good – all it will do is slide around the prices of things. As a temporary band-aid, it’s a good step, but we should be talking and thinking about the underlying flow of resources. We’re at a point, technology wise, where we can track every kilowatt hour, every skilled man hour in every category, every gram of metal, every resource. By looking at those numbers, we could make budgets that actually made sense – instead of budgeting in dollars, for example, we could budget in doctor-hours and MRI-hours and lab-hours when talking about whether we can or can not afford to do health care for everyone. We can also look realistically at the costs involved in *not* doing health care for everyone – lost man-hours of work, lost creativity, and things like that. We can also look at the overhead-hours – the hours wasted doing incredibly dumb or even hurtful things. We can look at how much a eviction really costs us, for example, and realize how stupid we’re all being.

Analyzing root causes is important. more on this later.

Neural networks and politics

December 22nd, 2015

So, as most of you know, I’ve spent a fair amount of time lately researching natural and artificial neural networks. I had a interesting thought the other day.

While I am so far to the left politically that they don’t have a good label to hang on me – I want to redesign the resource allocation system, I think it’s possible to get almost everyone everything they need, and possibly even (with judicious use of technology) everything they want – I recognize that my inner republican is a core part of my neural network that needs to exist.

So, I am assuming (or perhaps guessing) that inside my own neural network, individual concepts / symbols are represented as clusters of neurons – or subnets, as I sometimes call them. All of the concepts that the republicans hold dear I think are key for individual operation – while you may not need the tendency to try and keep things the same, or even move into previous models (conservatism) to be heavily weighted, it is undoubtedly a symbol that you want to be neurologically active.

Very few decisions are truly binary on the iron, and generally I assume that concepts and ideas light up both the ‘new ideas / things that haven’t been tried yet’ section of my neural network (imagination) and the ‘previous experience suggests..’ section of my neural network. Conservative thinking keeps me from hitting ‘upgrade now’ every time my computer offers me the chance, which results in my computers being stable and reliable. It keeps me from buying things every time I feel a urge, which results in me purchasing the things I need and the best things I want, rather than ending up with a hoard of physical possessions taking up space in my life and my mind. It helps me build up the friendships that help me and avoid the ones that would hurt me. There’s no doubt that conservative thinking has a part in a complete operating neural system.

And, it’s very likely that it has a place in government. I don’t think I’d ever want our government to have *no* conservatives in it, because if naught else they provide the devils advocate to demonstrate the truth or falsity of new ideas, and they do also hopefully keep the liberals from changing the things that truly shouldn’t be changed. It’s a mistake to think your political opponents are your enemies. They’re only your enemies if they aren’t willing to yield when your ideas are clearly better than theirs – when they lose the ability to objectively judge concepts, and lose the ability to learn.

One huge problem

December 20th, 2015

One huge problem with being human is that human memory does not gain by being serialized.

Spanking

November 28th, 2015

So, as most of you know, I’m against spanking children because I’m against A) violence B) pain C) making someone else suffer. However, I had a interesting and disturbing thought this morning. Children need to learn that having violence directed towards them feels awful, so that they’ll understand not to direct violence towards other people. I was worrying, slightly, that only children who have never been spanked (or children without violent siblings) might never develop the degree of empathy to understand what it’s like to be the recipient of violence. This probably isn’t valid – I am curious how many mass shooters weren’t exposed to any violence, and how many were exposed to far too much. I don’t know of any way to get data on this, but it would be interesting to know whether mass shooters tend to come from abusive families, or families that never exposed them to the idea of violence.

One thing I do note is that the world in general seems to do a good job of trying to ratchet up fear and anger. A lot of our current political system seems to be designed to break people into two equal groups and make those groups angry at each other. Long term, it doesn’t seem possible for this to work.

Of course, I do feel a bit like a one-eyed man in the world of the blind. That no one else is even discussing value, only money, for example – that our government doesn’t discuss value on the floor of the senate and house, only money – that they make decisions that destroy value in order to make money, repeatedly, and no one calls them on it. That no one discusses the difference between their conscious experience and what’s actually happening.

One thing I do find encouraging is that I do see a lot of signs that a lot of people, or at least a lot of my friends, recognize that the world’s religions are fundamentally flawed. And just today, I read a article about how India is granting personhood to dolphins. I suspect personhood should be granted to any creature that we can get to prove to us that it is aware of itself. I don’t know exactly how to do this – people claim dogs fail the mirror test, although vision is not their primary sense and they do seem to pass a scent version of the mirror test. I’m pretty clear that dogs are people, and cats, and pigs. I’m not sure about sheep and cows. I know chickens are not people, and things with distributed nervous systems like crabs and lobsters.

Heinlien did a great discussion of personhood in the start of Moon Is A Harsh Mistress when he was talking about Mike the computer who was clearly a person, even though he wasn’t a human. In Heinlien’s theory, it takes a certain number of large associational neural networks, be they artificial or natural. I tend to think he had the right idea, although it’s a very interesting and odd thought that being a person is a emergent property. What’s really odd is that the human mind seems to have sufficient capacity to be several people (something I am more aware of than most people).

November 15th, 2015

So, I have three major financial goals at the moment. I achieved one of them this year (unless my situation changes drastically, I will be able to pay my taxes in full in April, having already paid half of them via estimated payments) and I promised myself that if I did that I would buy a piece of particularly expensive software I particularly covet (Vienna Strings, which is like Ivory for stringed instruments). I am having second thoughts – partially because of guilt over the fact that not all my friends are doing well, although I am helping out several of them, there’s still more who are struggling – and partially because there is repeatedly a little voice that tells me I should really get over my dream of going pro with my music, since I’ve still got so far to go to be there. I haven’t really decided what to do about it..

stream of conciousness

November 15th, 2015

So, I spent a fair amount of time avoiding facebook and other social sites over the last few days.

My reasons are that I don’t want to expose myself to the latest horrific disaster about which I can do nothing.

I’ve come to accept that cops are going to shoot innocents, religious nuts are going to use their religion as a excuse to hurt other people, and countries are going to fight incredibly stupid wars. I’m slowly coming to understand that there is a group of people – and for the most part it’s the group that’s interested in having power – who would far rather build hell than heaven. But I have enough nightmares of my own without tuning into other people’s. It’s not that I don’t care, it’s that I don’t think caring is a good idea. The worst part is I can trace a lot of this back to the actions of the united states. My tax dollars at work.

Recently, someone on my facebook pointed out that if I pay taxes, I help finance our army, and our school of terrorism which trained Osama, and our drone strikes. There’s not a lot I can do about this. Our “democracy” gives us precious little power over the Powers That Be. I don’t even have any reason to think our votes are counted accurately, and after reading about how electronic voting machines are made, I have a lot of reasons to think they’re not.

But I can choose my own actions, and I choose not to be exposed to the people that are being hurt. I can’t stop them from being hurt, so if I expose myself to the trauma TV, all I’m doing is hurting myself and therefore adding to the net misery of the universe. It’s a net loss for everyone.

I do wonder if all these attacks would be as popular if everyone reacted to violence by turning away. And the truth, painful as it is, is that 140 people is insignificant in terms of the number of people that will die on any given day. As Blue Oyster Cult says, “Another 40,000 coming every day.” That number has probably gone up as the world population has.

And, if you really believe we have a immortal component, this isn’t really such a tragedy. They’ve logged off of Earth, but they’re still around somewhere. We’ll see them again soon enough.

Is it really a good thing for us to all wallow in pain and fear? Why do we do this to ourselves? I remember after 9/11, the news ran constantly with images of the planes striking the towers. And no one really talked about our part in all this – how we used Osama as a pawn in the cold war, and got some of his family killed, and maybe that’s why he was so angry – and how we trained him in our school of how to be a terrorist in the first place.

Part of the problem, clearly, is the religious texts. But I’ve tried talking to religious people, and they don’t seem to see the inherent contridiction between there being a all-powerful God, and there being a number of contridictory religious texts lying around. I really wonder what they think – God *can’t* remove the ones that aren’t the truth, or he *won’t*? Really, I think it would do us all a lot of good to throw the past away. But religion appears to write itself into neural networks in a way that disables their ability to think critically about it’s contents, or recognize that, in the case of Islam and Christianity at least, it has a very high “this makes no sense!” factor.

At this point, I’m fairly convinced that a lot of my own insanity is wrapped around the fact that some of my mind accepted Christianity and some of it rejected it. I’m fairly sure the part that rejected it is the better person. I know there was a way to see it that made it a good thing, but it wasn’t how I saw it. And I can’t see it that way now. I can see it *enough* that way to understand where Christians are coming from. But not enough to think we should keep this and expose our children to it.

But, it still hurts. It still keeps me awake at night. I still wrestle with it, over and over. If we could stop believing so many stupid things, we could have heaven here and now.

I struggle so much with the idea that I seem to be smarter than a lot of the people around me. I’m convinced this must be something that’s wrong with my mind, that makes me incorrectly evaluate my own intelligence. Or that I’m not seeing the people around me as they really are. Or something. And really, there’s no objective way to measure, and my intelligence hasn’t bought me much. It hasn’t bought me my dream career, or my freedom. I’m so convinced there’s something wrong with the way my mind is configured, but I haven’t been able to change enough to be free.

A long time ago, when I was more inclined to lie, I told a lot of lies that I experienced later coming true. Sometimes I wonder if I should tell people I’ve achieved lucid dreaming to see if it happens. Or at least a end to the nightmares.

I have spent some time struggling with the old Calvinism thoughts, only brought up to date with a more complete understanding of what our minds are. Do I really have free will at all? Am I really making decisions, or does it just feel like I am? Am I just playing a tape? If so, where does it end?

I also sometimes feel like maybe we’re all the result of some incredibly simple, Conway Game Of Life process that was left running on a enormously powerful system. That there is no God, that the system is just burning idle cycles. Maybe we’re all the result of the hashing algorythm for a far more advanced race’s Bitcoin.

I think if there is free will, it must be partially in when a neural connection is made or not made. We must be choosing the shape of our minds. I really want to think there’s some bigger overarching pattern that will make what I’m currently experiencing fit into a beautiful complete picture when I see it from above.

Neural structures

October 23rd, 2015

I’ve been pondering what the difference is between the neural structures that represent data stored in our minds that is changeable and mutable (for example available programming languages, the contextual meanings of words, etc) and data stored in our minds that isn’t easily changable (for example religious beliefs and political ideas).

It seems pretty likely that religious beliefs are stored so immutably that people are unable to let go of them even when they see concrete evidence that they are incorrect, or that they are hurting other people. On the other hand, we have no problem letting go of ideas about, for example, product quality (we routinely adapt which brands we use according to who is currently producing the best products). I am wondering, physically and structurally, what this means about how these bits of data are stored. Since data is stored in our minds via the physical structure of neural interconnections, it’s very interesting that some bits of data tend to be more immutable than other bits.

Intelligent design

August 22nd, 2015

So, as my two readers know, I am a proponent of the idea of intelligent design, with the designers being *us* – it seems clear to me that since we have the ability to program, including at assembly language levels, that coding up DNA is not beyond us. People tend to counterargue that we can’t edit our own DNA, that our minds have no ability to change our DNA. Well, here’s another study discussing the idea that we can in fact do so..

http://www.theguardian.com/science/2015/aug/21/study-of-holocaust-survivors-finds-trauma-passed-on-to-childrens-genes

Ashley Madison data leak

August 18th, 2015

So, I’m of two minds about this.

On one paw, people shouldn’t really do this to each other (reveal information so private). I’m not really happy with the hackers who did this, and I feel like they’re just making the situation worse.

On the other paw, wouldn’t it be wonderful if we all didn’t *have* to keep secrets from each other? If we could in fact accept each other the way we are? I mean, I know from personal experience that we can’t.. but wouldn’t learning how to be better than feeling like we have to sneak around behind each other’s backs and use web sites like this one?

I have never used Ashley Madison.. I have a policy against pay dating sites in general and sites that look like they have the potential to add to the collective unhappiness of the world in specific.

I am still against possessive and coercive relationships, and I still am in favor of polysexuality and potentially polyfidelity.

Somehow I thought I’d have more to say about this