They say you learn something new every day.

Posts tagged ‘coding’

Command Line ImageMagick (23/12/2012)

For some websites that I run I fairly regularly need to resize some photos to a certain width. ImageMagick is a great application for command line image manipulation.

The syntax to convert a file to a specific width and the corresponding height (which is what I want to do) is:

convert InputName.jpg -resize 550x Outputname.jpg 

What I’ve found useful is running command line scripts through vbscript:

Set wshShell = WScript.CreateObject(“WSCript.shell”)
wshShell.Run “cmd.exe /C convert InputName.jpg -resize 550x Outputname.jpg”, 0, True

I know you can do all sorts of clever things from the command line, but not quite as many as vbscript can. I find it really useful to be able to access all of the vbscript logic, and read and write text files and so on, while at the same time being able to issue commands to the command prompt.

New lines in Notepad2 (31/10/2012)

I’ve always been a big fan on Notepad2. It’s far better than the standard Notepad, has syntax highlighting, more features and is very fast.

It’s also just a single executable file and tiny. There are plenty of notepad replacements, but this one is so simple, that it’s my go to replacement.

One thing I’ve often wanted to do though, is add or remove line breaks. I’ve never managed to do this before, but today I realised it’s actually very simple, I was just being stupid.

I came across a blog that reminded me how to do it. There are a couple of different new line characters: \r or \n, but if you want to add them in, you just need to remember to tick “Transform backlashes”.

Brilliant. Another minor inconvenience solved.

  

Even More Basic (22/03/2012)

Somewhat ironically, given yesterday’s post, I found a way of making my application even simpler today.

I don’t want to go into the specifics, because this site is about general lessons, but I didn’t notice this simplification yesterday.

It involved changing one of the dependencies, and in my head yesterday I’d viewed them just as “fixed” points that I had to work around. Luckily, I was able to change them, and so make what was coming in simpler.

Once I could do this, it simplified the whole internal workflow – basically rather than splitting something into two arrays and searching either of them, I just left it as one and searched it all as once.

I think the lesson is, when you look at a problem, look at all parts of it. A friend of mine at work often says, “What’s the exam question here?”. I think it’s a good way to look at it: often people get so bogged down in the detail, they forget what it is exactly they’re trying to do.

Couldn’t give a Flying Duck (15/03/2012)

There’s a great post on CodingHorror about solving problems by explaining the problem very clearly.

Bob pointed into a corner of the office. “Over there,” he said, “is a duck. I want you to ask that duck your question.” 

I looked at the duck. It was, in fact, stuffed, and very dead. Even if it had not been dead, it probably would not have been a good source of design information. I looked at Bob. Bob was dead serious. He was also my superior, and I wanted to keep my job.

I awkwardly went to stand next to the duck and bent my head, as if in prayer, to commune with this duck. “What,” Bob demanded, “are you doing?”

“I’m asking my question of the duck,” I said.

One of Bob’s superintendants was in his office. He was grinning like a bastard around his toothpick. “Andy,” he said, “I don’t want you to pray to the duck. I want you to ask the duck your question.”

I licked my lips. “Out loud?” I said.

“Out loud,” Bob said firmly.

I cleared my throat. “Duck,” I began.

“Its name is Bob Junior,” Bob’s superintendant supplied. I shot him a dirty look.

“Duck,” I continued, “I want to know, when you use a clevis hanger, what keeps the sprinkler pipe from jumping out of the clevis when the head discharges, causing the pipe to…”

In the middle of asking the duck my question, the answer hit me. The clevis hanger is suspended from the structure above by a length of all-thread rod. If the pipe-fitter cuts the all-thread rod such that it butts up against the top of the pipe, it essentially will hold the pipe in the hanger and keep it from bucking.

I turned to look at Bob. Bob was nodding. “You know, don’t you,” he said.

“You run the all-thread rod to the top of the pipe,” I said.

“That’s right,” said Bob. “Next time you have a question, I want you to come in here and ask the duck, not me. Ask it out loud. If you still don’t know the answer, then you can ask me.”

It’s a really good point. Too often, we just haven’t sorted through things clearly enough in our head and that’s why we struggle to come up with the solution.

I read this, this morning. And while I haven’t had chance to try it out yet, I can think of times when I’ve done it. And now that I’m aware of it, I’m going to make a greater effort to think through the question when I’m facing a problem.

Couting Words (27/02/2012)

So, as I said, proof reading is hard. Really, to misquote Douglas Adams, hard. You just won’t believe how vastly, hugely, mind-bogglingly hard it is. I mean, you may think doing maths is hard, but that’s nothing compared to proofreading.

One of the things I find difficult to spot is duplicate words. Being a techie sort, I decided to code myself out of it, so I wrote a little internet app: the repeated word finder.

Basically it searches for cases of the same word being used in close proximity and highlights them. Obviously, there are lots of legitimate uses for repeated words (like both the ones in the illustration), and I know that you can never code better writing, but it helps you see the errors. My hope is that by highlighting these things  it’ll help me spot them.

It’s interesting – there are some things humans are good at, and some things computers are good at. Humans are very good at reading what should be there, and improving phrasing etc. Computers are very good at reading what is actually there and highlighting things that humans would just gloss over.

Hopefully, this is just the beginning of a larger proof reading tool. It’s something of a sister to the uber-wordcount tool, which needs a bit of a rewrite really. My plan is to handle all of this sort of thing – stats, wordcounts, etc, in one javascript based application. There’s no need to do anything server side with this at all.

I’ve written the app in javascript, and I have to admit, my javascript is rusty. I was quite exicited to find a javascript minifier. This is the original:

function countit(){

var formcontent=document.wordcount.words.value
formcontent = formcontent.replace(/\n/g, “

”)
formcontent = formcontent.split(” “)
var recentbits = “”

for ( var i = 0; i < formcontent.length; i++ )
{
if ( recentbits.toLowerCase().indexOf(” ” + formcontent[i].toLowerCase()) > 0)
{
formcontent[i] = “” + formcontent[i] + “
}

recentbits = “”

for (var count=0; count < 20; count++)
{
recentbits = recentbits + ” ” + formcontent[i-count]
}

}

var totalwords = formcontent.length

document.getElementById(‘totalwords’).innerHTML = “Output: (” + totalwords + ” words)

” + formcontent.join(” “)

}

And this is after minifying:

function countit(){var a=document.wordcount.words.value;a=a.replace(/\n/g,”

”);a=a.split(” “);var b=”“;for(var c=0;c0){a[c]=””+a[c]+””}b=”“;for(var d=0;d<20;d++){b=b+” “+a[c-d]}}var e=a.length;document.getElementById(“totalwords”).innerHTML=”Output: (“+e+” words)

”+a.join(” “)}

Obviously, you can’t read it, but it’s so much more compact.

Going through it, I don’t think it does anything cleverer than renaming all the variables to consecutive letters and getting rid of all the space. But it’s pretty nifty for loading into the live system.

Client side (25/02/2012)

I was reading an article in .Net Magazine the other day and someone had asked a developer:

“Is it better to run your code on the server side or the client side?”

 And the developer said something along the lines of:

“Client side. This will scale better since the users’ computers will do all the work”.

It’s kind of obvious really, but I realised I’ve been doing this wrong. Take my wordcount tool, for example (the design of which is beginning to look a bit crappy to me, which is good, because it means I’m getting better at designing things). It’s built in PHP, which means it uploads all the content to my server and runs the commands on my server.

If you think about it, this is kind of stupid for two reasons:

  1. I’m having to submit a load of data over the internet, which is slow.
  2. I’m having to run all the commands on my server. Which doesn’t scale and means my computer is doing the work.

This is a prime example of when doing it on the user’s computer would be better.

So I’ve started rebuilding it in javascript. It turns out I’m a bit rusty at javascript, and also PHP has some “lovely” built in functions (like a wordcount functions). I think I see what Jeff meant about PHP now. It’s such a weird language:

PHP isn’t so much a language as a random collection of arbitrary stuff, a virtual explosion at the keyword and function factory

Nevertheless, I still quite like it, but it makes it quite difficult to translate form PHP to javascript. I’m going to have to rewrite my wordcount code more or less from scratch.

This is no bad thing though – it’ll be some good javascript practice.

A Touch of Class (17/02/2012)

I think I suddenly understood classes and objects today.

I get this quite a bit with coding concepts. Or maybe concepts in general. I leave them mulling over in my head, and then suddenly, one day, I get them,

I think no one has ever really explained objects very well, and I think a key part of that is that I never got when I would use one. I think the fact that you don’t really need them makes it even harder.

But classes are basically like multiple functions in one. Here’s a demo one I built to explain them to myself:

Class TVProgram

Public StartTime
Public Test
Public ProgramTitle

Public Property Get ProgramDate
ProgramDate = Day(Test) & ” ” & MonthName(Month(Test)) & ” ” & Year(Test)
End Property

End Class

Set objTVShow = New TVProgram
objTVShow.StartTime = CDate(“17:30”)
objTVShow.Test = DateSerial(1999,9,17)
objTVShow.ProgramTitle = “TV Show”

wscript.echo objTVShow.ProgramDate & ” – ” & objTVShow.Test

So the first few lines:

Public StartTime
Public Test
Public ProgramTitle

Are like the Function variables. You assign values to these.

Public Property Get ProgramDate

Is like the Function output. You can have several of these in one class, and this is the real value to them and the thing I don’t think I got until now: classes are like functions with lots of outputs. This means you can define different outputs with the same data, or different parts of output for the same idea.

It gives me a nice warm feeling when I suddenly understand something like this. And it makes me wonder why I never understood it before. However, I think part of this comes from the fact no one ever really says what classes actually are:

You can use classes to describe complex data structures. For example, if your application tracks customers and orders, you can define two classes for them, each with a unique set of internal data (typically called properties) and functions (typically called methods). You can then manage customers and orders as if they were native VBScript subtypes. More important, because you assign a class its properties and methods (i.e., its programming interface), you have an object-oriented tool to improve VBScript applications.

Does that really help in any way? Or does that leave you even more confused by what they’re actually talking about?

Keeping on Top of Things (16/02/2012)

In many ways my last few days have been filled with thoughts about scope creep. As I said a little while ago, a lot of my projects are suffering from this. And partly it’s because I’m being a little bit too ambitious. So I’ve intentionally dialled back on what I’m doing to make sure I hit the core values.

Part of this has involved reviewing my coding, and I’ve reminded myself of the importance of building a central, maintainable framework.

“Maintainable” is the key word here because “code rots”:

Code is bad. It rots. It requires periodic maintenance. It has bugs that need to be found. New features mean old code has to be adapted. The more code you have, the more places there are for bugs to hide. The longer checkouts or compiles take. The longer it takes a new employee to make sense of your system. If you have to refactor there’s more stuff to move around.

You realise this when you come back to something you were working on several months ago, try to run it and it crashes.

  • Partly it’s because of that bug that you never got round to fixing but knew about and knew the workaround for.
  • Partly it’s because something else changed along the way and broke it.
  • Partly it’s because you’re not caring for it and fixing bugs and improving it any more.

As soon as you stop actively looking after it, it starts getting worse.

Of course, you can’t look after every project at once, but what you can do is write a centralised code library, look after that, and pull much of your stuff from that. That way, you’re actively looking after most of the code at once.

Jeff calls this “tending to your software garden” and I think I agree with him. I think I need to come up with more ways to keep my garden well looked after.

Becoming a Millionaire One Penny at a Time (10/02/2012)

I have no business sense. It’s just something I have to live with. I’m never going to be an entrepreneur. If I went on Dragon’s Den I’d be one of the ones you laugh at. Luckily, I work in the public sector, so this isn’t a problem.

However, yesterday, I realised that the secret to being rich – or at least, sustaining yourself on freelance work, or earning extra income – isn’t to come up with a million pound idea. It’s to come up with lots of small ways of making money.

The secret to Microsoft, for example, isn’t that they sell one £1billlion computer a year, they sell a million copies of Windows and Office.

I realised this when a friend asked me to host their website. It turns out my hosting allows me to resell it (like reseller hosting), so I can set myself up as a very small ISP. I don’t plan to do it in a big way, and I only plan to do it for friends and colleagues, with a bit of consultancy thrown in (I’ll set up their DNS for them or something like that, and advise them about their code). But I can charge a few pounds a month. Obviously, £2 a month isn’t very much, but if you get 50 people doing that, that’s £100 a month – or £1200 a year, and suddenly that’s quite a bit. It’s two months rent, for example.

And maybe that’s something that you could scale. If you set up with a friend, they could do some touting too, and suddenly you could have a few hundred people.

Really, it’s just a thought that lots of minor projects could come together to produce a significant income. Or at least a viable one. My Amazon Associates account has earned £85 in a couple of years, which is okay. Especially considering I don’t really advertise it, or have any particularly popular websites. It’s something that can only really go up.

So really, my revelation was in a way of looking at things. I don’t think I’m going to go freelance any time soon, but I like the idea of running a number of smaller projects as a web host and web consultant or something like that.

Hi again, Fidelity (09/02/2012)

I think I was too quick to re-judge High Fidelity. It’s actually really good.

Or rather, it’s got really good. I think, thinking about the beginning, it has got better. And there are definitely some bits that could probably do with a slight tweak. But other bits that are like this, that are just fantastic:

First of all, — actually, first of all and last of all — this business about not sleeping with Ian. How do I know she’s telling the truth? She could have been sleeping with him for weeks, months, for all I know. And anyway, she only said that she hasn’t slept with him yet, and she said that on Saturday, five days ago. Five days! She could have slept with him five times since then! (She could have slept with him twenty times since then, but you know what I mean.) And even if she hasn’t, she was definitely threatening to. What does ‘yet’ mean, after all? ‘I haven’t seen Resevoir Dogs yet.’ What does that mean? It means you’re going to go, doesn’t it?

‘Barry, if I were to say to you that I haven’t seen Reservoir Dogs yet, what would that mean?’

Barry looks at me.

‘Just … come on, what would it mean to you? That sentence? ‘I haven’t seen Reservoir Dogs yet?’ ‘

‘To me, it would mean that you’re a liar. Either that or you’ve gone potty. You saw it twice. Once with Laura, once with me and Dick. We had that conversation about who killed Mr. Pink or whatever fucking color he was.’

‘Yeah, yeah, I know. But say I hadn’t seen it and I said to you, ‘I haven’t seen Reservoir Dogs yet,’ what would you think?’

‘I’d think, you’re a sick man. And I’d feel sorry for you.’

‘No, but would you think, from that one sentence, that I was going to see it?’

‘I’d hope you were, yeah, otherwise I would have to say that you’re not a friend of mine.’

‘No, but — ‘

‘I’m sorry, Rob, but I’m struggling here. I don’t understand any part of this conversation. You’re asking me what I’d think if you told me that you hadn’t seen a film that you’ve seen. What am I supposed to say?’

‘Just listen to me. If I said to you — ‘

’ — ‘I haven’t seen Reservoir Dogs yet,’ yeah, yeah, I hear you — ‘

‘Would you … would you get the impression that I wanted to see it?’

‘Well … you couldn’t have been desperate, otherwise you’d have already gone.’

‘Exactly. We went first night, didn’t we?’

‘But the word ‘yet’ … yeah, I’d get the impression that you wanted to see it. Otherwise you’d say you didn’t fancy it much.’

‘But in your opinion, would I definitely go?’

‘How am I supposed to know that? You might get run over by a bus, or go blind, or anything. You might go off the idea. You might be broke. You might just get sick of people telling you you’ve really got to go.’

I don’t like the sound of that. ‘Why would they care?’

‘Because it’s a brilliant film. It’s funny, and violent, and it’s got Harvey Keitel and Tim Roth in it, and everything. And a cracking sound track.’

Maybe there’s no comparison between Ian sleeping with Laura and Reservoir Dogs after all. Ian hasn’t got Harvey Keitel and Tim Roth in him. And Ian’s not funny. Or violent. And he’s got a crap sound track, judging from what we used to hear through the ceiling. I’ve taken this as far as it will go. But it doesn’t stop me worrying about the ‘yet.’

The conversation here is amazing and very funny. And the way it jumps from the thought process to the conversation is very neat. I think that’s an interesting trick to segue between sections. It’s so slick you nearly don’t notice it.

I also love the rhythm of the conversation. Both speakers are pulling in different directions and it works so well. You get a feel for their characters, as well as being driven forwards by the plot.

I think the other two things to take away from this is the variance and rhythm of the sentences. You can have simple sentences with long words, or you can have long sentences with simple words.

I ran High Fidelity through the Uber Word Count tool that I wrote a while back. The tool, I notice now, needs some tweaking and styling. And I’m glad I can see that now, as it means I’m better than I was when I wrote it.

To be honest, it didn’t really tell me that much, other than most of the words were very short, but it was interesting to see the results. And how long the novel was, and how many different words were used.

I think if there’s one thing to take away from this, though, it’s conversation. Conversation, conversation, conversation in novels.

Tag Cloud