Welcome, Guest: 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) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Advanced Programming Techniques (java) (1707 Views)

DHTMLSQL - A Very Advanced Wrapper For Mysql Database! / Secret Communication Techniques / Financial Modelling & Techniques; Spss (2) (3) (4)

(1) (Reply)

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 cool. most/all by other functional programming languages.

0.) Generics
-----------------
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.

Pair<A,B>
{
private A aa;
private B b;
public Pair(A a,B b)
{
this.a = a;
this.b = b;
}

public A first(){return a;}
public B second(){return b;}
}

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.

2.) Range
------------
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.

e.g
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.

students grades.

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.

netbooks -
chrome books -
pc laptops -
gaming laptops-
workstations -
rugged laptops-

A range can reduce the membership checking code by
implementing it internally.

e.g
if(age>10&&age<13)
person = pre-teen
if(age>13&&age<20)
person = teenager
if(age>20)
person = tween/adult etc

with a range object the boiler checks go away.

preTeen.within(age);
teenager.withi(age);
//...
the difference is clear.

3.) Conditions
-------------------
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:

interface Condition<T>
{
public boolean matches(final T t);
}

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.

class aClass
{
public filter<T> Collection<T> (Collection<T> col,Condition<T>wink
{
List<T> list = new LinkedList<>();
if(col.isEmpty())
return Collections.unmodifiableList(list);
else
Iterator<T> iter = col.iterator();
while(iter.hasNext())
{
T t = iter.next();
if(condition.matches(t))
list.add(t);
}
return list;
// actually lock it with a collections object
return Collections.unmodifiableList(list);
}
}

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.

Condition<File> pdfMatches = new Condition<>
{
public boolean matches(final File f)
{
return f.isFile()&& f.getName().toLowerCase().endsWith("pdf"wink;
}
}

then,
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);


4.) Differences
----------------------
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.

e.g
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.

like:
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
into parts.
an example.
say you want to separate a group of people according to more than one criteria,
all consecutively.

first - you want to separate the men from the women
then among the separated women, you want to separate the pregnant from the non
pregnant.
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.

in java-speak:

//a generic method for multiple filtering of a list..

public <T> List<List<T>> multiFilter(List<T> rawList , List<Condition<T>> conditions)
{
//ignore edge cases : null/invalid arguments
List<List<T>> res = new LinkedList<>();//create resulting list
Iterator<Condition<T>> iter = conditions.iterator();//get an iterator
List<T> remL = null;//pointer to the remaining unprocessed list
remL = rawList; //set remainder to initial argument
while(iter.hasNext())
{
Condition<T> c = iter.next();//current condition
Pair<List<T>,List<T>> pair = filter(remL,c);
//filter code from an earlier example
//assuming the pair holds matches and non matches.
//where the matching list is the first and the non matches is the second
//field in the pair object.
res.add(pair.first());
//set remainder to the second (non matches in the pair)
remL = pair.second();
//process repeats itself
}
if(!remL.isEmpty())//catch the dregs
res.add(remL);
// the resulting list , is nicely broken into chunks fulfilling each
//condition in the list of conditions
//return the result.
return res;
}

//format 2
multiFilter(List<T> rawL,Condition<T>... conditions)
{
return multiFilter(rawL,java.util.Arrays.asList(conditions));
// noda format of the method using variable args..
}

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:
conditions: nulls,folders,nonFiles,filesWithNoExtension,fileTypes.

pseudoCode:
------------
run thru the fileSystem, get filetypes and put in a set.
for each fileType <assuming its a string > create a Condition
Object for it.
e.g

fileTypeCondition implements Condition<File>
{
String ftype;
public boolean matches(final File f)
{
//get filetype
if it matches filetype it passes etc
}
}

create condition objects for the other conditions.
pack them into list ,in the specified order.
invoke, multiFilter
return result.

1 Like

Re: Advanced Programming Techniques (java) by Nobody: 9:22am On Oct 30, 2014
@Op,
You make sense die. grin cool

1 Like

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.
Thank you.

1 Like

(1) (Reply)

Learn How To Build A Professional Website With Blogger / Precise Financial Systems Ltd Now On Nairaland / Woocommerce Plugin For Gtpay Integration

(Go Up)

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

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2023 Oluwaseun Osewa. All rights reserved. See How To Advertise. 113
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.