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
Tuesday, March 26, 2013
Running binary code in C
Been messing with running arbitrary binary code in C. A fairly simple example that shows that you can call arbitrary binary code. This compiles in Visual Studio C++ 2010 Express.
#include
const unsigned char function_data[] =
{
0x55, 0x8B, 0xEC, 0x83, 0xEC, 0x40, 0x53, 0x56, 0x57, 0x83, 0x7D, 0x08, 0x00, 0x7F, 0x04, 0x33,
0xC0, 0xEB, 0x29, 0x83, 0x7D, 0x08, 0x01, 0x75, 0x07, 0xB8, 0x01, 0x00, 0x00, 0x00, 0xEB, 0x1C,
0x8B, 0x45, 0x08, 0x83, 0xE8, 0x01, 0x50, 0xE8, 0xD4, 0xFF, 0xFF, 0xFF, 0x8B, 0xF0, 0x8B, 0x4D,
0x08, 0x83, 0xE9, 0x02, 0x51, 0xE8, 0xC6, 0xFF, 0xFF, 0xFF, 0x03, 0xC6, 0x5F, 0x5E, 0x5B, 0x8B,
0xE5, 0x5D, 0xC2, 0x04, 0x00
};
int main()
{
int (__stdcall *fptr)(int) = (int (_stdcall*)(int))function_data;
int a;
a = fptr(5);
printf("Test function returned: %d\n",a);
return 0;
}
It will only run on an x86 machine I suspect.
To generate the machine code, I used the object code that was compiled from the function I wanted to run. The only change I had to make was to convert the function call table to the correct locations. For instance, the sequence of bytes E8 D4 FF FF FF was E8 00 00 00 00, which is a relative call to offset 0. This would have been replaced by the linker with the correct address to the function (this is a recursive function). So I calculated the correct offset and stuck it in there. Same with the E8 C6 FF FF FF. D4 FF FF FF is -44 decimal.
In addition, it's possible to read directly from the object code, but then you'd have to do the function pointer replacements automatically, which I haven't set up yet. If there are easier ways to do that, I'd love to know.
#include
const unsigned char function_data[] =
{
0x55, 0x8B, 0xEC, 0x83, 0xEC, 0x40, 0x53, 0x56, 0x57, 0x83, 0x7D, 0x08, 0x00, 0x7F, 0x04, 0x33,
0xC0, 0xEB, 0x29, 0x83, 0x7D, 0x08, 0x01, 0x75, 0x07, 0xB8, 0x01, 0x00, 0x00, 0x00, 0xEB, 0x1C,
0x8B, 0x45, 0x08, 0x83, 0xE8, 0x01, 0x50, 0xE8, 0xD4, 0xFF, 0xFF, 0xFF, 0x8B, 0xF0, 0x8B, 0x4D,
0x08, 0x83, 0xE9, 0x02, 0x51, 0xE8, 0xC6, 0xFF, 0xFF, 0xFF, 0x03, 0xC6, 0x5F, 0x5E, 0x5B, 0x8B,
0xE5, 0x5D, 0xC2, 0x04, 0x00
};
int main()
{
int (__stdcall *fptr)(int) = (int (_stdcall*)(int))function_data;
int a;
a = fptr(5);
printf("Test function returned: %d\n",a);
return 0;
}
It will only run on an x86 machine I suspect.
To generate the machine code, I used the object code that was compiled from the function I wanted to run. The only change I had to make was to convert the function call table to the correct locations. For instance, the sequence of bytes E8 D4 FF FF FF was E8 00 00 00 00, which is a relative call to offset 0. This would have been replaced by the linker with the correct address to the function (this is a recursive function). So I calculated the correct offset and stuck it in there. Same with the E8 C6 FF FF FF. D4 FF FF FF is -44 decimal.
In addition, it's possible to read directly from the object code, but then you'd have to do the function pointer replacements automatically, which I haven't set up yet. If there are easier ways to do that, I'd love to know.
Monday, November 5, 2012
Why Lifting Weights is Good
- Improves your cognitive function: it stimulates your brain. Several studies have been done that show marked improvements in various mental activities: coordinating multiple activities, focus, and long term planning with both anaerobic and aerobic training. (http://scholar.google.com/scholar?hl=en&q=Exercise%2C+Brain+and+Cognition+Across+the+Lifespan&btnG=Search&as_sdt=0%2C11&as_ylo=&as_vis=1)
- Increases your metabolism. Stressing the muscles causes them to grow, causing a larger percentage of your body weight to be muscle, which increases your metabolism and thereby decreases fat. This is in contrast to low intensity exercise or endurance exercise where muscles do not grow much larger, so while calories are burned during aerobic exercise, the number of calories your body burns during normal operation is smaller than in a strength trained individual who has more muscle mass and therefore more resting energy requirements. (http://scholar.google.com/scholar?hl=en&q=resistance+training+resting+energy+requirements&btnG=Search&as_sdt=0%2C11&as_ylo=&as_vis=0)
- Increases bone density. Stressing the bones causes them to grow thicker and denser. Again, while aerobic exercise can help in this area, high intensity exercise helps increase bone density more. (http://scholar.google.com/scholar?q=resistance+training+increases+bone+density&hl=en&btnG=Search&as_sdt=1%2C11&as_sdtp=on)
- Decreases the risk of injury. If you are stronger, you're less likely to get injured during competition, or in every day life. (http://www.mendeley.com/research/effect-neuromuscular-training-incidence-knee-injury-female-athletes-prospective-study/ , http://scholar.google.com/scholar?hl=en&q=strength+training+injury+prevention&btnG=Search&as_sdt=0%2C11&as_ylo=&as_vis=0)
All of these things are good for guys and girls. Actually I think a lot of girls shy away from strength training because of the fear of appearing big. This is unlikely to happen. Most guys have to put in a lot of effort in order to gain size working out. Its way harder for girls, because females lack the testosterone that guys do so their muscles don't develop nearly as much. If anything any fat you have will be replaced with muscle, and you'll look trimmer even though you are heavier. Losing weight does not necessarily equal looking thinner.
Monday, October 29, 2012
Running postgreSQL on Windows XP Embedded
After attempting to run the installer and messing with permissions a lot, it ended up that the issue has to do with the DEV NULL driver not being available. See http://forums.enterprisedb.com/posts/list/2179.page.
Monday, July 23, 2012
Squats and Sprints
Since training for the Peachtree and then getting a deadleg playing lacrosse a week later, I have not worked out much.
But I'm going to start what I am going to call Squats and Sprints, a new workout program.
It's designed to be simple. But effectively for each workout you pick a squat exercise, a sprint exercise, and then anything else that you want to work on that day. The squats and sprints are mandatory, the other stuff is optional. The desire is to get to be doing heavy front or back squats and high intensity short to middle distance sprints at every workout, which ideally would be twice daily, every day of the week. But most people aren't going to have the time to get to that point. Even hitting heavy back squats and a high intensity sprint session twice per week will be adequate to get most people in great shape.
A typical workout would include:
Squats: warmup, 5x5
Weighted Pullups: warmup, 5x5
Sprints: 3x12x25yds full speed (goal is to complete each set of 12 in less than 1 minute, with 3 minutes rest)
But I'm going to start what I am going to call Squats and Sprints, a new workout program.
It's designed to be simple. But effectively for each workout you pick a squat exercise, a sprint exercise, and then anything else that you want to work on that day. The squats and sprints are mandatory, the other stuff is optional. The desire is to get to be doing heavy front or back squats and high intensity short to middle distance sprints at every workout, which ideally would be twice daily, every day of the week. But most people aren't going to have the time to get to that point. Even hitting heavy back squats and a high intensity sprint session twice per week will be adequate to get most people in great shape.
A typical workout would include:
Squats: warmup, 5x5
Weighted Pullups: warmup, 5x5
Sprints: 3x12x25yds full speed (goal is to complete each set of 12 in less than 1 minute, with 3 minutes rest)
Wednesday, April 4, 2012
The Will's Workout
My training schedule for the next few months is getting full. I'll be getting stronger with Bonn, Jumping higher with Curtis, and getting faster long distance by myself (with Bonn's helpful training eye).
My goals remain the same: twice body weight squat, sub 1 minute 400, sub 6 minute mile, sub 20 minute 3-mile, dunk, one armed pullup, with the additional goal of hitting a sub 45' 10k in July.
Bonn and I will be lifting heavy in the morning (6am, Monday-Thursday, when I don't have lacrosse practice), and doing various cross-training type activities. That will be where I work on squat, some explosive training, and the one armed pullup, as well as balancing out all the rest of the training with a solid amount of posterior chain and flexibility work.
Curtis and I will be doing high intensity explosiveness training in the evenings about twice a week. That will have to be scheduled on a week to week basis, but it will likely fall on Mondays and Wednesdays, at around 6-6:30pm.
My run training will take place when possible after work or later in the evening.
For lifting, I think we're going to move back to a more organized split workout, as presented by Kelly Baggett of Higher Faster Sports. We won't incorporate too many more explosive training exercises since I'll be getting those with Curtis, but I may try to work in some explosive lifts, including jump squats, and upper body plyometrics like medicine ball throws, and drop pushups / jump pushups, wood chops, etc.
The split will look something like the following:
Workout 1 - squat, hamstring
Workout 2 - chest, pullups
Workout 3 - deadlift, squat
Workout 4 - shoulders, rows
and adding any of the following supplemental workouts: abs, hips, forearms, calves, arms.
Additionally, each workout will have a warmup consisting of running or rowing or dynamic warmup (high knees, toe touch, butt kick, karaoke, backpedal, etc...), and each workout will finish with stretching.
Typically I'll be able to work out Monday, Wednesday, Saturday, with a possible additional Tuesday Thursday workout in there. Ideally I'd like to be squatting twice a week, which will happen If I get at least three workouts in per week.
So without further ado, here's my proposed workouts for the next week (starting April 5, 2012):
Thursday: dynamic warmup, squat (5x5), med ball hamstring curls (3x12), glute ham raise (3x10), dumbell calf raise (3x20), stretch
Saturday: row 10 min, incline bench press (5x5), weighted wide grip pullup (5x5), skull crushers (3x8), close grip weighted pullup (3x8), stretch
Monday: dynamic warmup, deadlift (6x3), jump squat (30% max, 10 reps/set, repeat sets until performance decreases), dumbell step up (3x8 each leg), weighted leg raise (3x10), stretch
Wednesday: row 10 min, overhead press (5x5), bent over barbell row (or machine seated row, 5x5), close grip bench press (3x8), medicine ball throws (overhead, sides, behind the back, 3x10 each), stretch
My goals remain the same: twice body weight squat, sub 1 minute 400, sub 6 minute mile, sub 20 minute 3-mile, dunk, one armed pullup, with the additional goal of hitting a sub 45' 10k in July.
Bonn and I will be lifting heavy in the morning (6am, Monday-Thursday, when I don't have lacrosse practice), and doing various cross-training type activities. That will be where I work on squat, some explosive training, and the one armed pullup, as well as balancing out all the rest of the training with a solid amount of posterior chain and flexibility work.
Curtis and I will be doing high intensity explosiveness training in the evenings about twice a week. That will have to be scheduled on a week to week basis, but it will likely fall on Mondays and Wednesdays, at around 6-6:30pm.
My run training will take place when possible after work or later in the evening.
For lifting, I think we're going to move back to a more organized split workout, as presented by Kelly Baggett of Higher Faster Sports. We won't incorporate too many more explosive training exercises since I'll be getting those with Curtis, but I may try to work in some explosive lifts, including jump squats, and upper body plyometrics like medicine ball throws, and drop pushups / jump pushups, wood chops, etc.
The split will look something like the following:
Workout 1 - squat, hamstring
Workout 2 - chest, pullups
Workout 3 - deadlift, squat
Workout 4 - shoulders, rows
and adding any of the following supplemental workouts: abs, hips, forearms, calves, arms.
Additionally, each workout will have a warmup consisting of running or rowing or dynamic warmup (high knees, toe touch, butt kick, karaoke, backpedal, etc...), and each workout will finish with stretching.
Typically I'll be able to work out Monday, Wednesday, Saturday, with a possible additional Tuesday Thursday workout in there. Ideally I'd like to be squatting twice a week, which will happen If I get at least three workouts in per week.
So without further ado, here's my proposed workouts for the next week (starting April 5, 2012):
Thursday: dynamic warmup, squat (5x5), med ball hamstring curls (3x12), glute ham raise (3x10), dumbell calf raise (3x20), stretch
Saturday: row 10 min, incline bench press (5x5), weighted wide grip pullup (5x5), skull crushers (3x8), close grip weighted pullup (3x8), stretch
Monday: dynamic warmup, deadlift (6x3), jump squat (30% max, 10 reps/set, repeat sets until performance decreases), dumbell step up (3x8 each leg), weighted leg raise (3x10), stretch
Wednesday: row 10 min, overhead press (5x5), bent over barbell row (or machine seated row, 5x5), close grip bench press (3x8), medicine ball throws (overhead, sides, behind the back, 3x10 each), stretch
Subscribe to:
Comments (Atom)
