|Register On Nairaland / LOGIN! / Trending / Recent / New|
Stats: 2,989,943 members, 7,279,794 topics. Date: Saturday, 28 January 2023 at 12:37 AM
|Advanced Programming Techniques (java) by kambo(m): 11:34pm On Oct 29, 2014|
powerful Techniques to ease your programming burden
In programming, abstraction is power.
Abstraction is also expressibility.
Higher abstractions = More Power = more expressiveNess and elegant programs.
and hopefully less boiler plate code.
here are some stuffs i've discovered that also make
programming easier for me. Some have been pre empted by the new
java release (java . most/all by other functional programming languages.
Beneath the object types i'll be listing is
the powerful java Generics facility.
Generics was a major milestone in the java language evolution.
And it was well worth it.
It made the language mor complex but for the gains
,for the gains.. it was worth it .
I really wondered how we got on without generics!!
but you dont see how primitive you are until you've
experienced a better alternative.
1.) pair data type
using java generics a pair could be created as this.
in logic and mathematics the pair corresponds to the tuple.
its ease and power comes from saving the coder writing
boiler plate one off classes .
too many times in programming, a result is needed that
comprises of more than one object.
Pairs shine in such situation.
A workaround could be by using a collection: an array, a list, a set
etc but that's overkill.
Use a pair and you'd be left gaping why o why you'd never used it all this while.
pair's are great for filtering data sets.
e.g suppose you combing thru a directory looking for files
with a particular extension, and also wanting to know those
files that didnt make the cut,
your could return the final result in the form of
a pair: Pair<List<File>,List<File>>
where the pair.first() = matching list
pair.second = non-matching list.
The need for a range data type is so obvious yet so ignored
by the java lang.
ruby and i think python have it.
A range object would save writing cumbersome akward codes filled
with tangling if statements.
just pack a range and your good to go.
A range is used to partition an set into distinct classes.
age ranges? see even the last word hints that its a distinct
object in the english language yet its not realized in mainstream langs.
here is an example where range is king of the track.
A = 70-100
B = 60-69
C = 50-59
D = 40-49
E = 30 - 39
F = 0 - 29
other ranges are :
laptop types price ranges.
chrome books -
pc laptops -
A range can reduce the membership checking code by
implementing it internally.
person = pre-teen
person = teenager
person = tween/adult etc
with a range object the boiler checks go away.
the difference is clear.
using conditions to filter collections .
this technique is so useful that its now a defining point
in java 8 (the whole lambda shenaningan).
In other languages it called Predicates.
its a core tool in functional languages.
using generics a condition could be defined as:
with the condition elevated to Object status,
filtering operations can be abstracted even further instead
of being hard backed into unrelated classes.
here's an example where using a condition shines.
searching thru a file system and accumulating files according
to a criteria.
first create a generic filter method.
now you have a generic condition object and a generic filter method.
You're reading for business.
what could you filter with this.
say the file system.
you want to gather all pdf's in your hard disk.
assuming you have a list of files in a folder
your result is as easy as:
List<File> matches = filterClass.filter(List<File> filesInFolder,pdfMatches);
This is rarer to use though. But sometimes
when dealing with a collection , you want to do the equivalent
of an arithmetic minus operation.
it can be so combersome because java's collection
api doest natively provide for this operation.
an example screaming for a difference operation:
take the file type issue.
say the file name is "Test.java".
You want to get the fileType - so you do some string mangling and extract the type . its "java"
next you want to do get the name - more string mangling ,you get
the name = "Test".
but all you hard work was logically equivalent to saying
fileName - pureName = extension/type
fileName - extension = pureName.
this is a simple example.
But this operation arises over and over again when working with
collections. sometimes going the other way and just getting the difference is more efficient.
say in your file system , you have files of different types:
txt,pdf,java,c,cpp , zip,rar
and you want a list of all files except one or two types.
a diff method would be refreshing.
Collection<File> filesInFolder - Collection<File> unwantedFiles;
looping and packing in a collection would work.
but its messy for more than one differentiation.
easier would be:
Collection<T> difference(Collection<T> colA,Collection<T> colB);
more diff'ing could continue. chained diffs
difference(Collection<T> colA,difference(Collection<T> colB,Collection<T> colC));
Now for some really intricate data operations,
think of combing, Pairs,with differentiation,and Conditions
(all under the abstraction foundation of generics),
to work on data collections!!
It's in situations like this that - there is just no elegant alternative
to these powerful techniques.
an all in one example:
In this example, Pair, Condition and filtering will be used to cut up a list
say you want to separate a group of people according to more than one criteria,
first - you want to separate the men from the women
then among the separated women, you want to separate the pregnant from the non
then you want to separate among the pregnant the married from the unmarried.
The among the married you want to separate those that married from their tribe
from those who married inter-tribally.
so many conditions-.
without generics and the techniques listed above it can be cumbersome to write.
so here's how the examples so far can make a more elegant solution.
to solve the first example problem just create conditions matching
each criteria and invoke the generic method on them.
noda multiFiltering example:
segregating files in a system:
run thru the fileSystem, get filetypes and put in a set.
for each fileType <assuming its a string > create a Condition
Object for it.
create condition objects for the other conditions.
pack them into list ,in the specified order.
|Re: Advanced Programming Techniques (java) by Nobody: 9:22am On Oct 30, 2014|
You make sense die.
|Re: Advanced Programming Techniques (java) by Jeffflo: 10:57am On Nov 22, 2015|
Op, i saw your contribution to another thread, one that is focused on hadoop big data, with emphasis on using o'rielly documentation for learning. I have a question on it sha: after reading up, what did you do by way practice? Sandboxes (i.e. MapR, Cloudera) are what i know of for now.
|Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health |
religion celebs tv-movies music-radio literature webmasters programming techmarket
Nairaland - Copyright © 2005 - 2023 Oluwaseun Osewa. All rights reserved. See How To Advertise. 113