Posts in the “programming” category

To be a programmer is to develop a carefully managed relationship with error

“To be a programmer is to develop a carefully managed relationship with error. There’s no getting around it. You either make your accommodations with failure, or the work will become intolerable.”

~ Ellen Ullman (via this tweet)

This quote makes me think of all those years of exception-handling with Java. I never knew there was a better way to handle errors, so I developed a strategy of letting my exceptions bubble up to the controller level (as in model/view/controller), where I would deal with them. These days I know I can use Option/Some/None in Scala, as well as Try/Success/Failure.

Programming tip: Start with something easy in the morning

One of my favorite tips to get on a roll at work is to save some easy tasks for the next day. I could have finished the last three tasks on this list in a couple of minutes last night, but I’d rather spend ten minutes on them this morning to warm up my brain with something easy.

Always visualize data

I’m sure there must be other ways to mathematically see these differences, but I agree with the general concept that it can be easy to be misled by data. (Image from this Twitter page.)

How to call/test a web service from a browser

Web service FAQ: How can I test a web service from a browser?

I’ve been working with web services a lot this past week, specifically with Apache Axis2, and here are a few notes I've learned about hitting one of my web services from a browser.

Looking at the WSDL for a web service from a browser

To view the WSDL document for a web service I created named UserService, I just went to the following URL in my browser:

Treat your code like poetry and take it to the edge of the bare minimum

“Treat your code like poetry and take it to the edge of the bare minimum.”
~ ILYO

“I have learned magnitudes more from code I have maintained over code I have written from scratch.”
~ Viktor Klang, in this tweet

I have no idea who or what ILYO is, but I like the “poetry” part, and dislike the “bare minimum” part. As the second quote implies, code should be written so you can read it a year or two from now.

A Markdown cheat sheet

This is a simple Markdown cheat sheet. I created it for my own needs, so I can find what I use and need quickly. If it’s a helpful resource for you too, cool.

[toc hidden:1]

Programing: failure, success; repeat

Programming is an interesting profession. You fail dozens or hundreds of times a day, then take a moment to celebrate a little victory. Then you move on to your next failure/success.

The meaning of the word “reify” in programming

I don’t recall hearing of the words “reify” or “reification” in my OOP years, but that may be because I studied aerospace engineering in college, not computer science. Since learning functional programming (FP) I often see those words, so I thought I’d try to understand their meaning.

Background

I ran into the word “reify” when I saw code like this:

trait Foo {
    //...
}

object Foo extends Foo

I’d see that code and eventually saw that someone described that last line as a “reification” process.

What “reify” means

The short answer is that the main definition of reify seems to be:

“Taking an abstract concept and making it concrete.”

For the longer answer, I found the following definitions and examples of reification.

Gimp “Script-Fu” example (functions, parameters)

I’ve mentioned this before, but if you’re interested in learning Lisp/Scheme programming, the makers of gimphelp.org have a nice collection of plugins for Gimp 2.8 that you can download, and they’re all written in Scheme. (Well, a variation of Scheme.)

Here’s the body of a Gimp plugin file named FU_contrast_auto-contrast.scm:

(define (FU-auto-contrast
        img
        drawable
        merge-flag)

    (gimp-image-undo-group-start img)
    (if (not (= RGB (car (gimp-image-base-type img))))
             (gimp-image-convert-rgb img))   

   ; Create a new layer
   (define value-layer (car (gimp-layer-copy drawable 0)))

   ; Give it a name
   (gimp-item-set-name value-layer "Contrast Adjustment Layer")

   ; Add the new layer to the image
   (gimp-image-insert-layer img value-layer 0 0)

   ; Set opacity to 100%
   (gimp-layer-set-opacity value-layer 100)

   ; Set the layer mode to Value
   (gimp-layer-set-mode value-layer 14)

   ; Adjust contrast
   (gimp-levels-stretch value-layer)

   ; Merge down, if required
   (if (equal? merge-flag TRUE)
       (gimp-image-merge-down img value-layer 1 )
       ()
   )
   (gimp-image-undo-group-end img)
   (gimp-displays-flush)   
)
(script-fu-register "FU-auto-contrast"
    "<Image>/Script-Fu/Contrast/Auto Contrast"
    "Automatically adjust contrast of drawable"
    "Mark Lowry"
    "Mark Lowry"
    "2006"
    "*"
    SF-IMAGE    "Image"         0
    SF-DRAWABLE "Current Layer" 0
    SF-TOGGLE   "Merge Layers?" FALSE
)

If you’re at least a little comfortable with Lisp/Scheme — and also Gimp — that code is relatively easy to understand. For instance, the define function creates a variable named value-layer, and then other Gimp functions like gimp-item-set-name and gimp-image-insert-layer modify value-layer.

At the end of the code, the script-fu-register shows how to register a plugin so that Gimp will find it and display it in the Gimp menu system.

Observations

A few observations come to mind:

1) Like other languages that don’t show their types, you can’t tell value-layer’s type from looking at the code, though you can assume that it’s something like Layer.

2) For some reason Lisp makes it more clear than other languages that you can’t tell what the function parameters are. They’re based on the positions in the list, and seeing "Mark Lowry" in there twice makes you wonder why it’s there twice. The only way to know what the parameters are is (a) guessing by what’s in each field, and (b) looking at the script-fu-register documentation. (This is true in other programming languages, but it really stands out here, possibly because of the formatting.)

Gimp Script-Fu function examples

If you are looking for Gimp Script-Fu function examples (as I was), the Gimp function examples used in this code are:

  • gimp-image-undo-group-start
  • gimp-image-convert-rgb
  • gimp-layer-copy
  • gimp-item-set-name
  • gimp-image-insert-layer
  • gimp-layer-set-opacity
  • gimp-layer-set-mode
  • gimp-levels-stretch
  • gimp-image-merge-down
  • gimp-image-undo-group-end
  • gimp-displays-flush
  • script-fu-register

The complete source code

Here’s the complete source code for this plugin, including the initial comments and license:

; FU_contrast_auto-contrast.scm
; version 2.9 [gimphelp.org]
; last modified/tested by Paul Sherman
; 02/15/2014 on GIMP-2.8.10
;
; 02/15/2014 - convert to RGB if needed
;==============================================================
;
; Installation:
; This script should be placed in the user or system-wide script folder.
;
;    Windows Vista/7/8)
;    C:\Program Files\GIMP 2\share\gimp\2.0\scripts
;    or
;    C:\Users\YOUR-NAME\.gimp-2.8\scripts
;    
;    Windows XP
;    C:\Program Files\GIMP 2\share\gimp\2.0\scripts
;    or
;    C:\Documents and Settings\yourname\.gimp-2.8\scripts   
;    
;    Linux
;    /home/yourname/.gimp-2.8/scripts  
;    or
;    Linux system-wide
;    /usr/share/gimp/2.0/scripts
;
;==============================================================
;
; LICENSE
;
;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;==============================================================
; Original information
;
; Automatically adjusts contrast of the current
; drawable by duplicating the layer, setting the
; new layer to Value blend mode, then running
; Auto Levels on the Value layer.
;==============================================================

(define (FU-auto-contrast
        img
        drawable
        merge-flag
    )
    (gimp-image-undo-group-start img)
    (if (not (= RGB (car (gimp-image-base-type img))))
             (gimp-image-convert-rgb img))   

   ; Create a new layer
   (define value-layer (car (gimp-layer-copy drawable 0)))

   ; Give it a name
   (gimp-item-set-name value-layer "Contrast Adjustment Layer")

   ; Add the new layer to the image
   (gimp-image-insert-layer img value-layer 0 0)

   ; Set opacity to 100%
   (gimp-layer-set-opacity value-layer 100)

   ; Set the layer mode to Value
   (gimp-layer-set-mode value-layer 14)

   ; Adjust contrast
   (gimp-levels-stretch value-layer)

   ; Merge down, if required
   (if (equal? merge-flag TRUE)
       (gimp-image-merge-down img value-layer 1 )
       ()
   )
   (gimp-image-undo-group-end img)
   (gimp-displays-flush)   
)
(script-fu-register "FU-auto-contrast"
    "<Image>/Script-Fu/Contrast/Auto Contrast"
    "Automatically adjust contrast of drawable"
    "Mark Lowry"
    "Mark Lowry"
    "2006"
    "*"
    SF-IMAGE     "Image"         0
    SF-DRAWABLE "Current Layer" 0
    SF-TOGGLE     "Merge Layers?" FALSE
)

Such a disappointing feeling when a book is a letdown

I just spent 45 minutes reading a new book about a programming language I was excited to learn, then slammed it shut and said, “Poorly organized, too many words, not enough code.”

That’s always such a disappointing feeling when you have that initial excitement about a programming language (or technology), and then a book is such a letdown. (I really hope people don’t view my books that way.)