The principle of DRY (Don't Repeat Yourself) is often on my mind while programming. Obviously, we all try to avoid it.
If you follow it, you will inevitably write better code. Actually, I find that following it, almost blindly, leads to better code. Why? Primarily it's because repetition is hard to maintain. If someone comes along later and modifies one of the pieces of repetition but fails to modify the others, then an inconsistency is introduced which is likely to be a bug. So reducing repetition and creating abstraction to remove it is a natural way to ensure that when someone changes something, they keep the whole program consistent or 'correct'. The same idea applies to other areas of code. I consider the setting and unsetting of flags a form of repetition, because they require one to keep track of the instances of usage of that flag and it's function. It is better to abstract the behaviour of the flag into a component that has the feature of the flag. Removal of flags will lead to better software because it's easier to maintain: it makes it harder to break.
I suggest the way we build software currently is broken. Why do we write tests for our code? We do it so that when someone changes something, they don't break an existing feature.
However, I argue, that if the tools we were using were good enough, we wouldn't need tests. In fact, any piece of code, when you change it, should still be correct. This is possible to a small degree with statically typed languages. But still, its possible to write code that doesn't do what you want it to. It's possible to change something and the intended action doesn't happen, and you aren't sure why.
We could achieve mandated correctness in two ways. The first would be to become better programmers. This is unlikely to happen. Although I believe we could get closer by coming up with better techniques for abstraction. Such as thinking about when you write a feature into a piece of code, it should stand by itself, such that if you remove one line, that feature goes away. And any line of code that you remove will remove the very function that that line of code is performing. This isn't currently possible because our languages are still very procedural, but with higher levels of abstraction, usage of functional languages, it's possible that every piece of code will relate to a single feature of the software. And groupings of features will be collected into single functions or objects as they fit. Removal of only a fraction of a 'feature' (such that something is broken) will not be allowed by the compiler because it will realise that something is wrong. Even with current languages, I believe we can get closer to this by writing code in such a way that it cannot be misused. It should not be possible for someone to come along later and use your code in a way that is broken (I currently think about this a lot while coding). Executional correctness should follow semantic correctness.
The second way this will be achieved is when computers begin to ask us questions about what we want. We will tell the computer what our goal is, in English (or your language of choice), and it will clarify that goal for us, and then write a high level set of code that we can modify. Any changes we make that are not correct (that don't make sense, are not possible, or are contradictory) it will complain about and provide suggestions for us to fix, or ask questions to fix it itself. And then when we compile that "code" the computer will construct a piece of software that achieves our goals. But this is still along way off, and requires much higher levels of artificial intelligence.
Until then, let's try to write code that is harder to break. And keep writing tests for stuff that is difficult to make resilient.
Monday, April 25, 2016
Tuesday, April 5, 2016
Super Easy Salads
I'm getting into eating awesome easy salads which are simple and delicious. For instance
This one is
- 4 leaf salad mix
- Cherry tomatos cut in half
- Half an avocado
- Handful of cashews
- Several bocconcini
- Couple spoonfuls of canned lentils
- A few pieces of char-roasted peppers from a jar
- Salt and pepper
- Olive oil on top if you want a bit more fat
Subscribe to:
Comments (Atom)
