473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding Duplication of Code

I have some logic that currently processes values retrieved by looping
through a HashTable/ListDictionary structure (a hash table that contains
ListDictionary objects as its values). Logically it's just a bunch of rows
and columns - just like in a DataTable or row set returned by a DataReader.
You can visualize this as 3 columns and N rows.

I need for the same logic to be able to process identical data - but by
processing DataRows of an ADO.NET DataReader.

I don't want to create two separate methods - one to process the DataReader
and another to process the hash table - as the logic is identical with the
exception being the structure being processed.

I'd appreciate suggestions for having one method that can process both a
DataReader and the hash table structure as described above.

The p-code for the logic currently implemented with a hash table structure
is this:
1. - initialize variables
2 - start loop through hash table
3 - populate 3 string variables from the hash table
4 - process the 3 string variables
5 - finish looping through hash table
6 - return one value to caller

Of course none of the processing logic really cares about the source of the
data (beyond syntax required to loop and to retrieve the 3 string values).

One option is to have the method only process *a* hash table structure. In
this case, the method would receive the hash table as a parameter - and when
null, would create a new hash table structure from scratch and populate it
from the db via a DataReader. Then it proceeds to process the hash table
with no knowledge of where the hash table came from.

Another method is to have the looping logic differentiate between what is
being looped through (hash table or DataReader return set). It would be
capable of looping through either the hash table or DataReader return set
based on some flag variable. I think this code might get kind of ugly
pretty quickly.

Both of these methods would work - but I'd appreciate some other ideas or
feedback on these two.

If you are wondering what's going on here (the bigger picture)... live user
interection is supported by the hash table structure (live user data exists
only in the hash table structure and only optionally saved to the db), while
automated processes work from the saved data only (from the db). I'm trying
to come up with one method that can be used by both live user processing as
well as the automated jobs.

Thanks!

-G
Nov 16 '05 #1
2 2329
Take a look at the Strategy pattern.

1) create an interface (IRowMover) for both objects that provide for
"movenext" and "get value(n)" where n is 0,1, or 2
2) inherit a component from that interface that implements the functionality
with a hash table
3) inherit another component from that interface that implements the
functionality against a data reader

now, in your controller class:
a) if the user's data is coming from a data set, instantiate the dataset
child object. If the data is coming from the hashtable, instantiate the
hashtable child object.
b) pass the new object, of type "IRowMover" to your class which does the
work.

That class will never know, or need to know, if it is spanning a hash table
or a data reader. All it knows is that it needs some data, and that it's
time to move to the next row.

Does this make sense?

--- Nick

"Guadala Harry" <GM**@NoSpam.co m> wrote in message
news:OV******** *****@tk2msftng p13.phx.gbl...
I have some logic that currently processes values retrieved by looping
through a HashTable/ListDictionary structure (a hash table that contains
ListDictionary objects as its values). Logically it's just a bunch of rows
and columns - just like in a DataTable or row set returned by a DataReader. You can visualize this as 3 columns and N rows.

I need for the same logic to be able to process identical data - but by
processing DataRows of an ADO.NET DataReader.

I don't want to create two separate methods - one to process the DataReader and another to process the hash table - as the logic is identical with the
exception being the structure being processed.

I'd appreciate suggestions for having one method that can process both a
DataReader and the hash table structure as described above.

The p-code for the logic currently implemented with a hash table structure
is this:
1. - initialize variables
2 - start loop through hash table
3 - populate 3 string variables from the hash table
4 - process the 3 string variables
5 - finish looping through hash table
6 - return one value to caller

Of course none of the processing logic really cares about the source of the data (beyond syntax required to loop and to retrieve the 3 string values).

One option is to have the method only process *a* hash table structure. In
this case, the method would receive the hash table as a parameter - and when null, would create a new hash table structure from scratch and populate it
from the db via a DataReader. Then it proceeds to process the hash table
with no knowledge of where the hash table came from.

Another method is to have the looping logic differentiate between what is
being looped through (hash table or DataReader return set). It would be
capable of looping through either the hash table or DataReader return set
based on some flag variable. I think this code might get kind of ugly
pretty quickly.

Both of these methods would work - but I'd appreciate some other ideas or
feedback on these two.

If you are wondering what's going on here (the bigger picture)... live user interection is supported by the hash table structure (live user data exists only in the hash table structure and only optionally saved to the db), while automated processes work from the saved data only (from the db). I'm trying to come up with one method that can be used by both live user processing as well as the automated jobs.

Thanks!

-G

Nov 16 '05 #2
>>>Does this make sense?<<<

Yes - thank you. I'll give it a whirl.

-G

"Nick Malik" <ni*******@hotm ail.nospam.com> wrote in message
news:mnGUc.2734 05$a24.235158@a ttbi_s03...
Take a look at the Strategy pattern.

1) create an interface (IRowMover) for both objects that provide for
"movenext" and "get value(n)" where n is 0,1, or 2
2) inherit a component from that interface that implements the functionality with a hash table
3) inherit another component from that interface that implements the
functionality against a data reader

now, in your controller class:
a) if the user's data is coming from a data set, instantiate the dataset
child object. If the data is coming from the hashtable, instantiate the
hashtable child object.
b) pass the new object, of type "IRowMover" to your class which does the
work.

That class will never know, or need to know, if it is spanning a hash table or a data reader. All it knows is that it needs some data, and that it's
time to move to the next row.

Does this make sense?

--- Nick

"Guadala Harry" <GM**@NoSpam.co m> wrote in message
news:OV******** *****@tk2msftng p13.phx.gbl...
I have some logic that currently processes values retrieved by looping
through a HashTable/ListDictionary structure (a hash table that contains
ListDictionary objects as its values). Logically it's just a bunch of rows and columns - just like in a DataTable or row set returned by a

DataReader.
You can visualize this as 3 columns and N rows.

I need for the same logic to be able to process identical data - but by
processing DataRows of an ADO.NET DataReader.

I don't want to create two separate methods - one to process the

DataReader
and another to process the hash table - as the logic is identical with the exception being the structure being processed.

I'd appreciate suggestions for having one method that can process both a
DataReader and the hash table structure as described above.

The p-code for the logic currently implemented with a hash table structure is this:
1. - initialize variables
2 - start loop through hash table
3 - populate 3 string variables from the hash table
4 - process the 3 string variables
5 - finish looping through hash table
6 - return one value to caller

Of course none of the processing logic really cares about the source of

the
data (beyond syntax required to loop and to retrieve the 3 string values).
One option is to have the method only process *a* hash table structure. In this case, the method would receive the hash table as a parameter - and

when
null, would create a new hash table structure from scratch and populate it from the db via a DataReader. Then it proceeds to process the hash table
with no knowledge of where the hash table came from.

Another method is to have the looping logic differentiate between what is being looped through (hash table or DataReader return set). It would be
capable of looping through either the hash table or DataReader return set based on some flag variable. I think this code might get kind of ugly
pretty quickly.

Both of these methods would work - but I'd appreciate some other ideas or feedback on these two.

If you are wondering what's going on here (the bigger picture)... live

user
interection is supported by the hash table structure (live user data

exists
only in the hash table structure and only optionally saved to the db),

while
automated processes work from the saved data only (from the db). I'm

trying
to come up with one method that can be used by both live user processing

as
well as the automated jobs.

Thanks!

-G


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
23114
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
20
5250
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
4
3641
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
61
3294
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again and again. I know to remove code-duplication I have to make functions and pass arguments to them but I am not able to think of a way doing it. Can you post some example for me, out of this code:
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.