My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
The other day I thought I thought wanted an ||+= operator.

I want +||= or ||+=. Or 'if defined, add/concat, otherwise, define with default value.' I'm just being greedy I think.

You see, in Ruby, there's this "if x is not defined, define x" idiom that goes like this:

x ||= 1

I was sick of seeing that followed by x += 1 every time I used it. I wanted to combine it into one line of x ||+= 1. I spent a little time trying to figure out how to do that, or some acceptable alternative.

Then I realized that a ternary operator would get the job done on one line. blah['key'] = blah['key'].nil? ? 1 : blah['key']+=1 is too ugly for what I want.

blah['key'] = blah['key'] ? blah['key']+=1 : 1 is not significantly better.

But I didn't really want the redundancy. On top of that, I think ternary operators require too much thought to process, and I avoid them in favor of spelling it out.

Tim Pope had an obvious and simple suggestion: define your hash with the default value of 0.

blah = Hash.new(0)

If you do that, then each time you write hash[key] += 1 it will define a new key into the hash if key did not exist there before (and the value there will be defaulted to 0, then incremented), or it will increment it if it's already there. Perfect!

Thanks - I was thinking more along the lines of Object. Hash was only an example. Helps to look *within* instead of outside though

Except that the objects I wanted to have that power didn't have control of the hashes passed to them. And I wanted to be able to receive strings and arrays with similar behavior in the same object.

Or so I thought.

Actually, that will work just fine. My object model is just wrong, and will need to be corrected.

The real problem was just that my thinking was attached and working within the particular object model I had already constructed. The more I thought about it, the more I realized it was just wrong - this object should be constructing its own data structures, and it should be encapsulating them, and it should be providing methods to allow the outside world to use and manipulate.

This is not that new a concept: It's called object orientation. Perhaps you've heard of it.

Today's lesson: Before deciding that the language needs fixing, first consider if you're just trying to do something stupid.

Anyway, it (re)taught me a lesson that I see others forgetting (or having never learned) quite often: Before deciding that the language needs fixing, first consider if you're just trying to do something stupid or useless or redundant.

Did you eat an extra bowl of stupid this morning?

Sometimes the language is not what is broken. I surmise that more often, it's your programming that is in need of repair. It's such an obvious point, but one that seems so easily and often forgotten.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

There are no comments for this entry yet.

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner