|
1 | 1 | # java-best-practices
|
2 | 2 | Best practices in Coding, Designing and Architecting Java Applications
|
| 3 | + |
| 4 | + |
| 5 | +# Coding |
| 6 | + |
| 7 | +## Code Quality |
| 8 | +- More than everything else, code quality is an attitude. Either, the team has it or not. The attitude to refactor when something is wrong. The attitude to be a boy scout. As an architect, it is important to create an environment where such an attitude is appreciated. (There are always bad sheep, who take the code quality to such depth that it is not fun anymore, but I like them more than developers who keep churning out bad code). |
| 9 | +- Have a good static analysis tool(and is part of Continuous Integration). Sonar is the best bet today. Understand the limits of Static Analysis. Static Analysis is not a magic wand. For me, results from Static Analysis are a signal: It helps me decide where I should look during peer or architect reviews? |
| 10 | +- Have good peer review practices in place. Every user story has to be peer reviewed. Put more focus on peer reviews when there is a new developer or there is a new technical change being done. Make best use of Pair Programming. The debate is ongoing : Is pair programming more productive or not? I would rather stay out of it. You make your choice. However, these two scenarios are bare minimum: |
| 11 | + - Onboarding a new programmer. Makes him comfortable with all the new things he has to encounter. |
| 12 | + - Implementing a complex functionality. |
| 13 | +- Next question is how to approach a Code Review. Difficult to cover everything. I would make a start. When doing a code review, I start with static analysis results (for example, sonar). I spend 10 minutes getting an overview of components and/or layers (focusing on size and dependencies). Next I would pick up a unit test for a complex functionality. I feel unit tests are the best place to discover the dependencies and naming practices (I believe good names = 50% of maintainable code). If a programmer can write a simple and understandable unit test, he can definitely write good code. Next, I look for 4 principles of Simple Design. After this, there are 100 other things we can look for - You decide. |
| 14 | +- PDF - https://www.mindmup.com/#m:a1972695706ab201340d4b0beac29b8bfc |
| 15 | + |
| 16 | +## Performance |
| 17 | + |
| 18 | +- First and Foremost - NO premature optimizations. Any optimization decision should be based on numbers or past experience. In Donald Knuth's paper "Structured Programming With GoTo Statements", he wrote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of non critical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." |
| 19 | +- Session size - Small |
| 20 | +- Implement Caching whereever possible |
| 21 | +- Set initial size on Collections |
| 22 | +- Create necessary Indexes on Database. Collect Statistics and Remove unnecessary data |
| 23 | +- In critical parts - write unit tests for performance. |
| 24 | +- Be conscious about create as few objects as possible. Especially in loops. |
| 25 | +- Avoid String Concatenation |
| 26 | +- Close connections and Streams |
| 27 | +- |
| 28 | +## Load and Performance Testing |
| 29 | +- PDF Presentation https://www.mindmup.com/#m:g10B8KENIDghuHAUjlYVmlfSllzTzg |
| 30 | +- Have clear performance objectives. That’s the single most important objective. Decide Peak Load, Expected Response Time, Availability Required before hand. |
| 31 | +- Establish clear performance expectations with the Interface Services |
| 32 | +- An application does not work on its own. It connects with a number of external interfaces. Establish clear performance expectations with the Interface Services |
| 33 | +- The next important thing is to ensure that you mirror your production environment. A load testing environment should be the same as your production environment. We will discuss the exact factors involved later in this article. |
| 34 | +- Validate early : Do performance testing as early as possible. |
| 35 | +- Make it a regular practice to use profilers in development environment. ex:JProfiler |
| 36 | +- Make sure team is not making premature optimizations. Any optimization decision should be based on numbers or past experience. In Donald Knuth's paper "Structured Programming With GoTo Statements", he wrote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of non critical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." |
| 37 | +- Have Clear Strategy on How to Handle expected load. What would be the initial settings on the application server? Do you plan to use a clustered environment? Do you need a load balancer? |
| 38 | + |
| 39 | +# Design |
| 40 | +## Design Focus Areas |
| 41 | +- 4 Principles of Simple Design. https://www.youtube.com/watch?v=OwS8ydVTx1c&list=PL066F8F24976D837C |
| 42 | + - Runs all tests |
| 43 | + - Minimize Duplication |
| 44 | + - Maximize Clarity |
| 45 | + - Keep it Small |
| 46 | +- Object Oriented Programming. Have good object, which have well-defined responsibilities. Following are the important concepts you need to have a good overview of. These are covered in various parts in the video https://www.youtube.com/watch?v=0xcgzUdTO5M. Also, look up the specific videos for each topic. |
| 47 | + - Coupling : |
| 48 | + - Cohesion : https://www.youtube.com/watch?v=BkcQWoF5124&list=PLBBog2r6uMCTJ5JLyOySaOTrYdpWq48vK&index=9 |
| 49 | + - Encapsulation |
| 50 | + - Polymorphism : https://www.youtube.com/watch?v=t8PTatUXtpI&list=PL91AF2D4024AA59AF&index=5 |
| 51 | + - SOLID Principles |
| 52 | +- UML is next even though, formal use of UML is on the way down with Agile. However, I think UML is a great tool in the arsenal for a white board discussion on design. A picture is worth thousand words. I recommend having a good overview of the UML basics. Focus on these four before you move on to others. |
| 53 | + - Class diagrams |
| 54 | + - Sequence diagrams |
| 55 | + - Component diagrams |
| 56 | + - Deployment diagrams |
| 57 | +- Design Patterns. Following video covers all the major design patterns. https://www.youtube.com/watch?v=0jjNjXcYmAU. My personal view : Design Patterns are good to know. Have a good idea on what each one of them does. But, that where it ends. I’m not a big fan of understanding the intricate details of each Design Pattern. You can look it up if you have a good overall idea about Design Patterns. |
| 58 | + |
| 59 | +# Architecture |
| 60 | +- PDF : [https://www.mindmup.com/#m:g10B8KENIDghuHAZWh2SEhSNTdNNjA] |
| 61 | + |
| 62 | +##Great Architect |
| 63 | +- Creating a clean architecture based on sound principles. Architecture covering all Non Functional Requirements. |
| 64 | +- Having good governance in place. Good review processes in place for Architecture, Design and Code. |
| 65 | +- Ensuring teams are as productive as they can be. Right tools. |
| 66 | +- Ensuring teams are following the best engineering practices. |
| 67 | +- Ensuring clear communication about architecture with business and technical teams. |
| 68 | + |
| 69 | +## Qualities |
| 70 | +Most important qualities I look for in an Architect are |
| 71 | +- Impeccable Credibility : Somebody the team looks up to and aspires to be. |
| 72 | +- Super diagnostic skills : The ability to do a deep dive on a technology issue. When developers are struggling with a problem (having tried different things), Can he/she provide a fresh pair of eyes to look at the same problem? |
| 73 | +- Forward Thinker and Proactive : Never satisfied with where we are. Identifies opportunities to add value fast. |
| 74 | +- Great Communication : Communication in the widest sense. Communicating the technical aspects to the stakeholders, project management, software developers, testers, etc. |
| 75 | + |
| 76 | +## SOAP Web Services |
| 77 | +[[images/SOAPWebServices.png]] |
| 78 | + |
| 79 | +## REST Web Services |
| 80 | +- PDF https://www.mindmup.com/#m:g10B8KENIDghuHAYmFzM0daOU80SDA |
| 81 | + |
| 82 | +## Distributed Cache |
| 83 | +[[images/DistributedCache.png]] |
| 84 | + |
| 85 | +## Layers |
| 86 | +- PDF https://www.mindmup.com/#m:g10B8KENIDghuHAemVIS2RvT1JDOUE |
| 87 | + |
| 88 | +### Business Layer |
| 89 | +Listed below are some of the important considerations |
| 90 | +- Should I have a Service layer acting as a facade to the Business Layer? |
| 91 | +- How do I implement Transaction Management? JTA or Spring based Transactions or Container Managed Transactions? What would mark the boundary of transactions. Would it be service facade method call? |
| 92 | +- Can (Should) I separate any of the Business Logic into seperate component or service? |
| 93 | +- Do I use a Domain Object Model? |
| 94 | +- Do I need caching? If so, at what level? |
| 95 | +- Does service layer need to handle all exceptions? Or shall we leave it to the web layer? |
| 96 | +- Are there any Operations specific logging or auditing that is needed?Can we implement it as a cross cutting concern using AOP? |
| 97 | +- Do we need to validate the data that is coming into the Business Layer? Or is the validation done by the web layer sufficient? |
| 98 | + |
| 99 | +### Data Layer |
| 100 | +- Do we want to use a JPA based object mapping framework (Hibernate) or query based mapping framework (iBatis) or simple Spring DO? |
| 101 | +- How do we communicate with external systems? Web services or JMS? If web services, then how do we handle object xml mapping? JAXB or XMLBeans? |
| 102 | +- How do you handle connections to Database? These days, its an easy answer : leave it to the application server configuration of Data Source. |
| 103 | +- What are the kinos of exceptions that you want to throw to Business Layer? Should they be checked exceptions or unchecked exceptions? |
| 104 | +- Ensure that Performance and Scalability is taken care of in all the decisions you make. |
| 105 | + |
| 106 | +### Web Layer |
| 107 | +- First question is do we want to use a modern front end javascript framework like AngularJS? If the answer is yes, most of this discussion does not apply. If the answer is no, then proceed? |
| 108 | +- Should we use a MVC framework like Spring MVC,Struts or should we go for a Java based framework like Wicket or Vaadin? |
| 109 | +- What should be the view technology? JSP, JSF or Template Based (Velocity, Freemarker)? |
| 110 | +- Do you want AJAX functionality? |
| 111 | +- How do you map view objects to business objects and vice-versa? Do you want to have View Assemblers and Business Assemblers? |
| 112 | +- What kind of data is allowed to be put in user session? Do we need additional control mechanisms to ensure session size is small as possible? |
| 113 | +- How do we Authenticate and Authorize users? Do we need to integrated external frameworks like Spring Security? |
| 114 | +- Do we need to expose external web services? |
| 115 | + |
| 116 | +# Tools |
| 117 | + |
| 118 | +## Maven |
| 119 | + |
| 120 | +### Best Practices |
| 121 | +[[images/MavenBestPractices.png]] |
| 122 | + |
| 123 | +## IDE |
| 124 | +[[images/Eclipse1.png]] |
| 125 | + |
| 126 | +# Practices |
| 127 | + |
| 128 | +## Agile and Architecture |
| 129 | +- First of all I’m a great believer that agile and architecture go hand in hand. I do not believe agile means no architecture. I think agile brings in the need to separate architecture and design. For me architecture is about things which are difficult to change : technology choices, framework choices, communication between systems etc. It would be great if a big chunk of architectural decisions are made before the done team starts. There would always be things which are uncertain. Inputs to these can come from spikes that are done as part of the Done Scrum Team.But these should be planned ahead. |
| 130 | +- Architecture choices should be well thought out. Its good to spend some time to think (Sprint Zero) before you make a architectural choice. |
| 131 | +- I think most important part of Agile Architecture is Automation Testing. Change is continuous only when the team is sure nothing is broken. And automation test suites play a great role in providing immediate feedback. |
| 132 | +- Important principles for me are test early, fail fast and automate. |
| 133 | + |
| 134 | +## Modern Development Practices |
| 135 | +- Unit Testing and Mocking : We are in the age of continuous integration and delivery, and the basic thing that enables those is having a good set of unit test in place. (Don’t confuse unit testing with screen testing done manually to check if the screen flow is right. What I mean by unit testing is JUnit test’s checking the business logic/screen flow in a java method (or) set of methods). Understand JUnit. Here is a good start : https://www.youtube.com/watch?v=AN4NCnc4eZg&list=PL83C941BB0D27A6AF. Also understand the concept of Mocking. When should we mock? And when we should not? Complicated question indeed. Understand one mocking framework : Mockito is the most popular one. Easymock is a good mocking framework as well. |
| 136 | +- Automated Integration Tests. Automated Integration Tests is the second important bullet in enabling continuous delivery. Understand Fitnesse, Cucumber and Protractor. |
| 137 | +- TDD (actually I wanted to put it first). Understand what TDD is. If you have never used TDD, then be ready for a rude shock. Its not easy to change a routine you developed during decades (or years) of programming. Once you are used to TDD you never go back. I promise. This list of videos is a good start to understanding TDD. https://www.youtube.com/watch?v=xubiP8WoT4E&list=PLBD6D61C0A9F671F6. Have fun. |
| 138 | +- BDD. In my experience, I found BDD a great tool to enable communication between the ready team (Business Analysts, Product Owner) and the done team (Developers, Testers, Operations). When User Stories are nothing but a set of scenarios specified is GWT (Given When Then) format, it is easy for the done team to chew at the user story scenario by scenario. With tools like Cucumber & Fitnesse, tooling is not far behind too. Do check BDD out. |
| 139 | +- Refactoring. Is there an Architect who does not encounter bad code? Understand refactoring. Understand the role of automation tests in refactoring. |
| 140 | +- Continuous Integration. Every project today has continuous integration. But, the real question is “What is under Continuous Integration?”. Compilation, unit tests and code quality gate(s) is the bare minimum. If you have integration and chain tests, wonderful. But make sure the build does not take long. Immediate feedback is important. If needed, create a separate build scheduled less frequently for slower tests (integration and chain tests). Jenkins is the most popular Continuous Integration tool today. |
| 141 | +[[images/ModernDevelopmentPractices.png]] |
| 142 | + |
| 143 | +### Important Questions |
| 144 | +- How often is code commited? |
| 145 | +- How early are problems/defects found/fixed? |
| 146 | +-- Code Quality, Code Review, ST Defects |
| 147 | +- Broken Builds? |
| 148 | +- How often is code deployed to production? |
| 149 | +- How often is code deployed to test environment? |
| 150 | +- How different is deploying code to local or test environment from deploying code to production? |
| 151 | +- What steps are in continuous integration? More steps in continuous integration means more stability. |
| 152 | + - Compilation |
| 153 | + - Unit Tests |
| 154 | + - Code Quality Gates |
| 155 | + - Integration Tests |
| 156 | + - Deployment |
| 157 | + - Chain Tests |
| 158 | +- How long does a Continuous Integration build run for? Is there a need for multiple builds? |
| 159 | + |
| 160 | +## Productive Developers |
| 161 | + |
| 162 | + |
| 163 | +## Code Reviews |
| 164 | + |
| 165 | +### Good Code |
| 166 | +- PDF : https://www.mindmup.com/#m:g10B8KENIDghuHANmRyVmpGb3lINEE |
| 167 | + |
| 168 | +### Peer Reviews |
| 169 | +- PDF : https://www.mindmup.com/#m:g10B8KENIDghuHAbWdfWVdrVU1VMEU |
| 170 | + |
| 171 | +### SONAR |
| 172 | +- One Slide from PDF - Making use of SONAR |
| 173 | +- https://www.mindmup.com/#m:g10B8KENIDghuHAbWdfWVdrVU1VMEU |
| 174 | + |
| 175 | + |
| 176 | +# Indian IT |
| 177 | + |
| 178 | +# Campus Interview Guide |
| 179 | +- https://docs.google.com/document/d/1IP6HNgpMdCAAJEREowfBj2XPLBrPcIIFNIGFPGX0-ao/edit#heading=h.byb5dl2v0c4p |
0 commit comments