CPTTM software
developer newsletter issue #33, Kent
Tong, Editor
in Chief
|
Dear
Software Developers,
This CPTTM Software Developer newsletter is to bring useful news to
you, software developers in Macau, for references without obligations,
so that you can do your jobs easier and better! Hope you like it. if
you'd like to unsubscribe or recommend your friends to subscribe, just
email me at kent@cpttm.org.mo.
Old issues are available here.
Topics in this issue:
Free
eBooks from Microsoft for developers
Those
free ebooks covering wide spectrum of technologies for
developers, from Visual Studio through to SQL server, and also Windows
Phone for mobile application developers:
Tim Ma
Joda Time - a better date and time API for Java (Noda time for .Net)
I
believe no matter what kind of application you are developing. You
could not avoid time manipulation. In Java, we use native
java.util.Calendar and java.util.Date to handle date and time
problem. Due to their bad design and interface, we have a potential
third party library called Joda Time which really make our
life easier. It is open source and maturity which has been under active
development since 2002.
For a date creation example, when we use native
Calendar class:
Calendar
calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
Using Joda, the code looks like this:
DateTime
dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
We could use number to present month and 1 means January.
So what about modifying date object, let’s say we
add 30 days to a certain date, the JDK way:
Calendar
calendar = Calendar.getInstance();
calendar.set(2010, Calendar.JANUARY, 1, 0, 0, 0);SimpleDateFormat sdf
= new SimpleDateFormat("E MM/dd/yyyy
HH:mm:ss.SSS");calendar.add(Calendar.DAY_OF_MONTH,
30);System.out.println(sdf.format(calendar.getTime()));
Using Joda, two lines of code to do the same thing:
DateTime
dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
System.out.println(dateTime.plusDays(30).toString("E MM/dd/yyyy
HH:mm:ss.SSS");
We save some lines of code to create Calendar and SimpleDateFormat
instance. And one importance concept of Joda is that, the instance is
immutable. So anytime you call a method for a change. It will return a
new DateTime instance, and a handy “toString” method for easy
formatting output.
Of course, Joda is more than a fancy interface
library. It includes more features:
- ReadablePartial: Sometime you just care the
year/month/day like birthday, or the time of day. You could use
LocalDate and LocalTime to represent those information without the
complex of time zone
- Spans of time concept: Duration,Period and
Interval classes represent an absolute mathematical span, expressed in
milliseconds (such as 60 seconds in a minute and 24 hours in a day) to
be used to date manipulation.
- Timezone support.
- multiple times (that is, repeat) in a
meaningful way, then it is a partial.
- Chronology: different kind of calendar support:
ISO (the default), Coptic, Julian, Islamic. The library
currently provides 8 calendar systems.
- Better Performance Characteristics.
To know more about this project, the official
homepage is here:
http://joda-time.sourceforge.net/
Practical usage of Joda is here:
http://www.ibm.com/developerworks/java/library/j-jodatime.html
Noda
Time is a port of Joda Time to .NET, here is an
article from the
code project to help you get started.
Tim Ma
Eclipse Helios Release!
The
Eclipse Helios is the largest release by the Eclipse, including 39
different project team, 33 million lines of code are released. Since
there are several projects in it, it is also available as 12 different
Eclipse packages targeting different kind of developer groups,
including Java EE Developers , PHP developers , C/C++ developers and so
on.
Here
is some new features and projects highlights:
● The Web
Tools Platform (WPT)
project has updated to the latest Java EE Specifications (Java EE 6)
including, Servlet 3.0, JPA 2.0, JSF 2.0, and EJB 3.1. Also, Improved
support in the JavaScript
Development Tools,
including a JavaScript debug framework that allows for integration of
JavaScript debuggers, such as Rhino and Firebug.
● Support
for Git, a popular distributed version control (DVCS), is provided by
the new Eclipse
EGit and
JGit projects. The new EGit 0.8 release includes a new Git repositories
view and support for fast forward merging and tagging.
● Eclipse
Marketplace offers
the Eclipse community a convenient portal that helps folks find open
source and commercial Eclipse-related offerings and plugins.
● Eclipse
Xtext 1.0, a
popular framework for creating domain specific languages (DSL),
introduces 80 new features. With Xtext, you could easily to create a
IDE for your own language or framework. Common features for a
IDE such as code completion, syntax checking. Elysium is
built by Xtext which is a text-based music notation. Here is a screencast
introduction.
● Accceleo 3.0
implements the OMG Model-to-text (MTL) specification and provides the
features required for a code generator IDE. Here is a sceencast
- Acceleo
used to create an Android application which
shows how to build a code template from source which generates
corresponding code based on given object model.
More
information about the Helios release can be found at www.eclipse.org/helios.
The Helios packages can be downloaded now at www.eclipse.org/downloads.
Tim Ma
Why functional programming matters?
If
you’re a Java programmer, this short article will show you the basic
concepts of functional programming and the reason why it may be getting
more and more important in the near future. All sample code is in Java
so it should be very easy to understand.
What
is functional programming?
Functional
programming is just like our programming in Java or C#, but without
assignments. You may wonder is this possible at all? For example, to
calculate the total price of a list of product prices, one might write
in functional programming style (the code is still in Java):
abstract class List {
}
class EmptyList extends List {
}
class NonEmptyList extends List {
int head;
List tail;
}
class Foo {
int getTotalPrice(List ps) {
if (ps instanceof EmptyList) {
return 0;
} else {
final NonEmptyList nl =
(NonEmptyList) ps; //note1: initialization, NOT assignment
return nl.head +
getTotalPrice(nl.tail);
}
}
}
Note that the highlighted code below seems to be
an assignment, but it is not: It is an initialization and the variable
is declared as final, so no modification can be made. This is perfectly
allowed in functional programming.
So you can see that assignment is not really
required. But why the lack of assignment is a good thing?
To see the benefit, let’s assume that if a price
is >= 100 then you’ll give a 20% off discount. So, you may
modify the code as:
class Foo {
int getTotalPrice(List ps) {
if (ps instanceof EmptyList) {
return 0;
} else {
final
NonEmptyList nl = (NonEmptyList) ps;
final
int headDiscountedPrice = nl.head >= 100 ? (int) (nl.head * 0.8)
: nl.head;
final
int tailPrice = getTotalPrice(nl.tail);
return
headDiscountedPrice + tailPrice;
}
}
}
Note the three initializations. Because all
the variables and fields can't be modified, their order is unimportant
and, in a real functional programming language, the order can be
changed at wish without changing the return value. For example, you
could change it like:
class Foo {
int getTotalPrice(List ps) {
if (ps instanceof EmptyList) {
return 0;
} else {
final
int tailPrice = getTotalPrice(nl.tail); //nl is not initialized yet,
how it works?
final
int headDiscountedPrice = nl.head >= 100 ? (int) (nl.head * 0.8)
: nl.head;
final
NonEmptyList nl = (NonEmptyList) ps;
return
headDiscountedPrice + tailPrice;
}
}
}
Note
that when initializing the tailPrice variable, the nl variable hasn’t
been initialized yet. Will this cause a problem? No. In a real
functional programming language, each of three variables will be
initialized at the same time with a lazy expression. When the value is
really needed, the lazy expression will be evaluated. So, if the value
of tailPrice is needed but nl hasn’t been evaluated yet, it will be
evaluated and the calculation will proceed. No matter what execution
order is, the final total price will be the same.
Now,
let’s get to the core issue of why this is important. As the order of
these expressions are unimportant, they can be evaluated
concurrently. As nowadays we’re getting more CPU cores instead
of speedier single CPU, this programming model may become the
mainstream in the future as the evaluations of different expressions
can be done in different cores.
In
summary, there are some basic concepts in functional programing:
● Lazy
evaluation
● Functions
return values
○ Given
a set of values in the parameter list, the function can only have one
possible result.
● Functions
have no side-effects
○
Cannot modify variables passed to them
○
Cannot modify any global variable
○ Variables
only assigned once
If
you’d like to know more how the procedure programing pattern could be
applied in it ( such as: sequential execution or looping ), you could
check this full paragraph of this article : Learn
the basic concepts and significance of functional programming in 10
minutes
Kent Tong
ASP.Net course both for VB.Net and C# developers
Want
to learn how to build dynamic web sites, web applications and
web services in Microsoft's ASP.Net platform? No matter you are a fan
of VB.Net or C#. The upcoming course in Late August, Introduction
to ASP.Net by using C# and Visual Basic .NET , take
cares of two groups of developers.
In
this course, you will learn how to programing in C# and
VB.NET . You will learn Web Forms and how to use different
kinds of controls to contract it. Also, how to do event handling and
conversation, session in ASP.NET framework.
Also,
if you want to migrate your code from VB.Net to C#, the
course definitely helps because you will learn how to program
side-by-side in VB.Net and C#.
Tim Ma
Upcoming
courses for software developers
Feedbacks
Any questions, ideas or experiences to share? Contact me at 28781313
or kent@cpttm.org.mo. We also
have two other newsletters: CIO newsletter
and Network
administrator newsletter,
your friends may like to subscribe.
Until next time,
Kent Tong |
|
|