where to check for nils or empty?

J

John Honovich

I am processing arrays of URLs. I call a series of methods to delete or
modify those urls. I have a risk that those methods will return an empty
or null array.

I am trying to figure out where and how to best check if the array is
null or empty.


Right now, I am doing it like this:

def process array
array = firstReview array
return if array.nil? || array.empty?
array = secondReview array
return if array.nil? || array.empty?
# repeat again and again
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end


My approach works currently but feels ugly and repetitive.

Any advice for improving this?

Thanks,

John
 
C

Christopher Dicely

Maybe something like this?

class Whatever
def initialize
...
@reviews = [:firstReview, :secondReview, ...]
end

def process array
@reviews.inject(array) do |arr, review|
break if arr.nil? || arr.empty?
send(review, arr)
end
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end
end
 
7

7stud --

John said:
I am processing arrays of URLs. I call a series of methods to delete or
modify those urls. I have a risk that those methods will return an empty
or null array.

My approach works currently but feels ugly and repetitive.

Any advice for improving this?

Some counter advice: inject() is the definition of slow and ugly. You
should reject any code that uses inject() out of hand.
 
R

Robert Klemme

I am processing arrays of URLs. I call a series of methods to delete or
modify those urls. I have a risk that those methods will return an empty
or null array.

I am trying to figure out where and how to best check if the array is
null or empty.


Right now, I am doing it like this:

def process array
array = firstReview array
return if array.nil? || array.empty?
array = secondReview array
return if array.nil? || array.empty?
# repeat again and again
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end


My approach works currently but feels ugly and repetitive.

Any advice for improving this?

There's a simple fix: do not assign to array. You can then do

first_review array and
second_review array and
third_review

Or you do

reviews = [
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
]

def process array
reviews.all? {|rev| rev[array]}
end

Btw, if the return value of your review is actually to mean something
I'd rather define clear semantics aka "nil means terminate processing"
and make sure that your review methods return the proper value. That's
easier than checking multiple conditions all the time (empty? or nil?).

Kind regards

robert
 
D

Dominik Honnef

I am processing arrays of URLs. I call a series of methods to delete or
modify those urls. I have a risk that those methods will return an empty
or null array.

I am trying to figure out where and how to best check if the array is
null or empty.


Right now, I am doing it like this:

def process array
array = firstReview array
return if array.nil? || array.empty?
array = secondReview array
return if array.nil? || array.empty?
# repeat again and again
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end


My approach works currently but feels ugly and repetitive.

Any advice for improving this?

There's a simple fix: do not assign to array. You can then do

first_review array and
second_review array and
third_review

Or you do

reviews = [
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
]

def process array
reviews.all? {|rev| rev[array]}
end

Btw, if the return value of your review is actually to mean something
I'd rather define clear semantics aka "nil means terminate processing"
and make sure that your review methods return the proper value. That's
easier than checking multiple conditions all the time (empty? or nil?).

Kind regards

robert


Your first approach won't work, as an empty array evaluates to true,
but the processing should stop on empty arrays
 
R

Robert Klemme

2008/2/17 said:
I am processing arrays of URLs. I call a series of methods to delete or
modify those urls. I have a risk that those methods will return an empty
or null array.

I am trying to figure out where and how to best check if the array is
null or empty.


Right now, I am doing it like this:

def process array
array = firstReview array
return if array.nil? || array.empty?
array = secondReview array
return if array.nil? || array.empty?
# repeat again and again
end

def firstReview array
array.reject! {|a| @MasterList.include? (a)}
end


My approach works currently but feels ugly and repetitive.

Any advice for improving this?

There's a simple fix: do not assign to array. You can then do

first_review array and
second_review array and
third_review

Or you do

reviews = [
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
lambda {|ar| ar.reject! {|a| ...}},
]

def process array
reviews.all? {|rev| rev[array]}
end

Btw, if the return value of your review is actually to mean something
I'd rather define clear semantics aka "nil means terminate processing"
and make sure that your review methods return the proper value. That's
easier than checking multiple conditions all the time (empty? or nil?).

Your first approach won't work, as an empty array evaluates to true,
but the processing should stop on empty arrays

Right. Thanks for pointing it out! That's why I suggested to define
the return value of the method to mean something and not just
accidentally use what bang methods return. See my remark in the last
paragraph.

Kind regards

robert
 
T

Todd Benson

Maybe something like this?

class Whatever
def initialize
...
@reviews = [:firstReview, :secondReview, ...]
end

def process array
@reviews.inject(array) do |arr, review|
break if arr.nil? || arr.empty?
send(review, arr)
end
end

You could also build the original array beforehand...

(array - [[]]).compact

Todd
 
T

Todd Benson

Maybe something like this?

class Whatever
def initialize
...
@reviews = [:firstReview, :secondReview, ...]
end

def process array
@reviews.inject(array) do |arr, review|
break if arr.nil? || arr.empty?
send(review, arr)
end
end

You could also build the original array beforehand...

(array - [[]]).compact

Also, depending on your data, you might have to do it backwards...

array.compact - [[]]

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top