1. Remember C++ destructors Remember C++ destructors? No? Then you might be lucky as you never had to debug through any code leaving memory leaks due to allocated memory not having been freed after an object was removed. Thanks SunOracle for implementing garbage collection! But nonetheless Bears Khalil Mack Jersey , destructors have an interesting trait to them. It often makes sense to free memory in the inverse order of allocation. Keep this in mind in Java as well, when you鈥檙e operating with destructor-like semantics: 鈥?When using @Before and @After JUnit annotations 鈥?When allocating, freeing JDBC resources 鈥?When calling super methods 2. Don鈥檛 trust your early SPI evolution judgement Providing an SPI to your consumers is an easy way to allow them to inject custom behaviour into your library code. Beware, though, that your SPI evolution judgement may trick you into thinking that you鈥檙e (not) going to need that additional parameter. True Bears Riley Ridley Jersey , no functionality should be added early. But once you鈥檝e published your SPI and once you鈥檝e decided following semantic versioning, you鈥檒l really regret having added a silly, one-argument method to your SPI when you realise that you might need another argument in some cases: 3. Avoid returning anonymous, local, or inner classes Swing programmers probably have a couple of keyboard shortcuts to generate the code for their hundreds of anonymous classes. In many cases Bears David Montgomery Jersey , creating them is nice as you can locally adhere to an interface, without going through the 鈥渉assle鈥?of thinking about a full SPI subtype lifecycle. But you should not use anonymous, local, or inner classes too often for a simple reason: They keep a reference to the outer instance. And they will drag that outer instance to wherevery they go, e.g. to some scope outside of your local class if you鈥檙e not careful. This can be a major source for memory leaks Bears Akiem Hicks Jersey , as your whole object graph will suddenly entangle in subtle ways. 4. Start writing SAMs now! Java 8 is knocking on the door. And with Java 8 come lambdas, whether you like them or not. Your API consumers may like them, though, and you better make sure that they can make use of them as often as possible. Hence, unless your API accepts simple 鈥渟calar鈥?types such as int Bears Walter Payton Jersey , long, String, Date, let your API accept SAMs as often as possible. What鈥檚 a SAM? A SAM is a Single Abstract Method [Type]. Also known as a functional interface, soon to be annotated with the @FunctionalInterface annotation. This goes well with rule number 2 Eddie Goldman Color Rush Jersey , where EventListener is in fact a SAM. The best SAMs are those with single arguments, as they will further simplify writing of a lambda. 5. Avoid returning null from API methods I鈥檝e blogged about Java鈥檚 NULLs once or twice. I鈥檝e also blogged about Java 8鈥檚 introduction of Optional. These are interesting topics both from an academic and from a practical point of view. While NULLs and NullPointerExceptions will probably stay a major pain in Java for a while, you can still design your API in a way that users will not run into any issues. Try to avoid returning null from API methods whenever possible. Your API consumers should be able to chain methods whenever applicable: 6. Never return null arrays or lists from API methods While there are some cases when returning nulls from methods is OK, there is absolutely no use case of returning null arrays or null collections! Let鈥檚 consider the hideous () method. Was that null check really necessary? Most IO operations produce IOExceptions, but this one returns null. Null cannot hold any error message indicating why the IO error occurred. So this is wrong in three ways: 鈥?Null does not help in finding the error 鈥?Null does not allow to distinguish IO errors from the File instance not being a directory 鈥?Everyone will keep forgetting about null Kevin White Color Rush Jersey , here 7. Avoid state, be functional What鈥檚 nice about HTTP is the fact that it is stateless. All relevant state is transferred in each request and in each response. This is essential to the naming of REST: Representational State Transfer. This is awesome when done in Java as well. Think of it in terms of rule number 2 when methods receive stateful parameter objects. Things can be so much simpler if state is transferred in such objects, rather than manipulated from the outside. 8. Short-circuit equals() This is a low-hanging fruit. In large object graphs, you can gain significantly in terms of performance, if all your objects鈥?equals() methods dirt-cheaply compare for identity first: 9. Try to make methods final by default Some will disagree on this Cody Whitehair Color Rush Jersey , as making things final by default is quite the opposite of what Java developers are used to. But if you鈥檙e in full control of all source code, there鈥檚 absolutely nothing wrong with making methods final by default, because: 鈥?If you do need to override a method (do you really?), you can still remove the final keyword 鈥?You will never accidentally override any method anymore 10. Avoid the method(T鈥? signature There鈥檚 nothing wrong with the occasional 鈥渁ccept-all鈥?varargs method that accepts an Object... argument: 1 void acceptAll(Object... all); Writing such a method brings a little JavaScript feeling to the Java ecosystem. Of course, you probably want to restrict the actual type to something more confined in a real-world situation Leonard Floyd Color Rush Jersey , e.g. String.... And because you don鈥檛 want to confine too much, you might think it is a good idea to replace Object by a generic T: More About the Author
We offer innovative and customized Java Training in Bangalore. Best Java course in Bangalore with Certified Experts. Go to free demo classes, get prepared by 8yrs experienced tutors and furthermore get hands on live project. Visit: Total Views: 155Word Count: 875See All articles