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?

Tag Cloud