Software Tools: Counting Characters

The ability to count characters (or words or lines) is incredibly useful – especially if you are working in a text-based scripting environment (like Linux). Having a simple tool to do that would be especially helpful. What would a character-counting tool look like?

# charcount - count characters in standard input
  character getc
  character c
  integer nc
	
  nc = 0
  while (getc(c) != EOF)
    nc = nc + 1
  call putdec(nc, 1)
  call putc(NEWLINE)
  stop
end

This is Ratfor again. Why don’t we use a real language? (Ahem! Ratfor is a REAL language!) Because by using a simple language like this, we can show how these functions work in a very simple and understandable way. From there, it’s easy enough to convert it to Java or Python or whatever language you want.

So here are some noteworthy points about the code above:

First, we introduce the character data type. What’s the difference between a character and an integer? For the purposes of the book, it’s just a documentation thing. A character has a specific use. It is used to read and write character text data. Integers are used to count things. So just like the NEWLINE is a special name for a specific, system-dependent value, a character is a specific name for a value with a specific purpose.

Next, we call the putdec() function. That is a special function (which we’ll define later) that takes a numeric value as the first parameter, formats it in a character string the size of the second parameter, and outputs it to the “standard output”. It does this by using the putc() function (as we’ll see later). So we’re already taking advantage of the software tools we’ve built already.

Finally we notice that there is a separate call to putc() to write out the NEWLINE character. That seems like a waste. Why would we do that? If we want to write multiple numbers on the same line, we can’t have putdec() inserting newlines. So we keep the function simple and that keeps it flexible. And it all hangs together in such a simple, clever way!

It’s like it was designed to be that way!

Refactor Bad Code Series #1: Joining forces to avoid bad code

Let’s be honest: there is a lot of really bad code out there. The tsunami of bad code cannot be overcome by the relative handful of gurus who have mastered code craftsmanship. Books become outdated, and the examples in training courses are so contrived that they can’t be applied to real work situations. Developers these days need byte-sized chunks of wisdom using real-world examples that can be applied to the work they are doing today. Continue reading “Refactor Bad Code Series #1: Joining forces to avoid bad code”

Master Your Tools

There are many skills necessary to be a successful software developer: language skills, mathematics, problem analysis, procedural thinking, patience, among many others. And for an area of specialization there are specific technologies to learn: Javascript, Hibernate, EE4J, etc.

All of these skills are essential to be able to perform as a competent software developer. But mastery in the craft of software development only comes with time and practice. Continue reading “Master Your Tools”

Coding for Christmas

One thing that has been promoted by software craftsmanship leaders for decades is the important of practicing your craft. The 10,000 hour rule put forth by Anders Ericsson and popularized by Malcolm Gladwell talks about having consistent, deliberate practice to become a master in a field. This isn’t just writing “Hello world” thousands of times. It’s deliberate practice specifically designed to help you learn new skills and refine the skills you have. Continue reading “Coding for Christmas”