it-ebooks - Java for small teams
Here you can read online it-ebooks - Java for small teams full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:
Romance novel
Science fiction
Adventure
Detective
Science
History
Home and family
Prose
Art
Politics
Computer
Non-fiction
Religion
Business
Children
Humor
Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.
Java for small teams: summary, description and annotation
We offer to read an annotation, description, summary or preface (depends on what the author of the book "Java for small teams" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.
Java for small teams — read online for free the complete book (whole text) full work
Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Java for small teams" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.
Font size:
Interval:
Bookmark:
Do not use reflection in your code (i.e. anything from the java.lang.reflect
package).
Reflection is a powerful tool; it allows Java to do things that would otherwise be either impossible or require large amounts of boilerplate code.
But, while it is sometimes useful when creating a framework or library it is unlikely to be a good way to solve the types of problem we encounter in normal server-side application code.
So why would we want to avoid using a powerful tool that Java provides?
Reflection has three main drawbacks:
Reflection moves errors from compile time to runtime - this is a Bad Thing
The compiler is our first form of defense against defects and the type system is one of the most effective tools we have to document our code. We should not throw these things away lightly.
Refactoring and code analysis tools are blind to reflection.
Although they may make some attempt to take it into account, the additional possibilities reflection creates for how a program might behave means the tools can no longer provide rigorous guarantees that they have understood the program. In the presence of reflection refactorings that would otherwise be safe may change program and analysis tools may report incorrect results.
In the same way that Reflection makes it harder for automated tools to understand code, it also makes it harder for humans to understand code.
Reflection introduces surprises.
This method is never called, I can safely delete it. Oh. Reflection.
I can safely change the behavior of this private method as I know where it is called from. Oh. Reflection.
Good code is all about getting good and timely feedback. The sooner you find out something is wrong the easier it is to fix.
Working on a legacy project, where the only way to discover if a code change is good is to deploy it to a dev/test/qa environment, is frustrating and demoralizing.
Make sure your project has a well designed development workflow - the effort of setting this up will be repaid many times over.
Ideally all feedback would be instantaneous, but in practice it is either impractical or impossible to get all feedback this way.
Instead software development is organized as nested levels of feedback, as shown here:
Implementing hashCode
and equals
is not straightforward. Do not implement them unless it is necessary to do so. If you do implement them, make sure you know what you are doing.
It is well known that if you override equals then you must also override the hashCode
method (see Effective Java item 9).
If logically-equal objects do not have the same hashCode
they will behave in a surprising manner if placed in a hash based collection such as HashMap
.
By "surprising", we mean your program will behave incorrectly in a fashion that is very difficult to debug.
Unfortunately, implementing equals
is surprisingly hard to do correctly. Effective Java item 8 spends about 12 pages discussing the topic.
The contract for equals is handily stated in the Javadoc of java.lang.Object
. We will not repeat it here or repeat the discussion of what it means, that can be found in Effective Java and large swathes of the internet. Instead we will look at strategies for implementing it.
Whichever strategy you adopt, it is important that you first write tests for your implementation.
It is easy for an equals method to cause hard-to-diagnose bugs if the code changes (e.g. if fields are added or their type changes). Writing tests for equals methods used to be a painful and time-consuming procedure, but libraries now exist that make it trivial to specify the common cases (see Testing FAQs).
This is the simplest strategy and the one you should adopt by default in the interests of keeping your codebase small.
Most classes do not need an equals method. Unless your class represents some sort of value it makes little sense to compare it with another so stick with the inherited implementation from Object.
An irritating gray area are classes where the production code never has a requirement to compare equality but the test code does. The dilemma here is whether to implement the methods purely for the benefit of the tests or to complicate the test code with custom equality checks.
There is, of course, no right answer here; we would suggest first trying the compare-it-in-the test approach before falling back to providing a custom equals method.
The custom equality checks can be cleanly shared by implementing a custom assertion using a library such as AssertJ or Hamcrest.
Effective Java tentatively suggests having your class throw an error if equals is unexpectedly called
@Override public boolean equals (Object o) { throw new AssertionError(); // Method is never called }
This seems like a good idea but, unfortunately, it will confuse most static analysis tools. On balance, it probably creates more problems than it solves.
Most IDEs provide some method of auto-generating hashCode
and equals
methods. This is an easily-accessible approach, but the resulting methods are (depending on the IDE and its settings) often ugly and complex such as the ones generated by Eclipse shown below:
@Override public int hashCode () { final int prime = ; int result = ; result = prime * result + ((field1 == null ) ? : field1.hashCode()); result = prime * result + ((field2 == null ) ? : field2.hashCode()); return result; }
@Override public boolean equals (Object obj) { if ( this == obj) return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; MyClass other = (MyClass) obj; if (field1 == null ) { if (other.field1 != null ) return false ; } else if (!field1.equals(other.field1)) return false ; if (field2 == null ) { if (other.field2 != null ) return false ; } else if (!field2.equals(other.field2)) return false ; return true ; }
Unless your IDE can be configured to produce clean methods (as discussed below) we do not generally recommend this approach. It is easy for bugs to be introduced into this code by hand editing over time.
Java 7 introduced the java.util.Objects
class that makes implementing hashCode
trivial. Guava provides the similar com.google.common.base.Objects
class which may be used with earlier versions of Java.
@Override public int hashCode () { return Objects.hash(field1, field2); }
The Objects
class also simplifies implementing equals a little by pushing most null checks into the Objects.equals
method.
@Override public boolean equals (Object obj) { if ( this == obj) // <- performance optimisation return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) // <- see note on inheritance return false ; MyClass other = (MyClass) obj; return Objects.equals(field1, other.field1) && Objects.equals(field2, other.field2); }
Font size:
Interval:
Bookmark:
Similar books «Java for small teams»
Look at similar books to Java for small teams. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.
Discussion, reviews of the book Java for small teams and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.