Featured

Tips for clean coding in java

I am required to read quite a bit of code written by other people, and over the years I’ve found that there are some problems that tend to recur again and again. The idea with this blog entry is to write up some tips , in the hope that it can help people out there who struggle to write clean code.

These are the tips for clean coding or good coding.

 

Keep classes small

So far you’ve created a few classes. After generating getter/setter pairs for even the small number (by the standards of a real-world Java class) of attributes, the Person class has 150 lines of code. At that size, Person is a small class. It’s not uncommon (and it’s unfortunate) to see classes with 50 or 100 methods and a thousand lines or more of source. Some classes might be that large out of necessity, but most likely they need to be refactored. Refactoring is changing the design of existing code without changing its results. I recommend that you follow this best practice.

In general, a class represents a conceptual entity in your application, and a class’s size should reflect only the functionality to do whatever that entity needs to do. Keep your classes tightly focused to do a small number of things and do them well.

Keep only the methods that you need. If you need several helper methods that do essentially the same thing but take different parameters (such as the printAudit() method), that’s a fine choice. But be sure to limit the list of methods to what you need, and no more.

Name methods carefully

A good coding pattern when it comes to method names is the intention-revealing method-names pattern. This pattern is easiest to understand with a simple example. Which of the following method names is easier to decipher at a glance?

  • a()
  • computeInterest()

The answer should be obvious, yet for some reason, programmers have a tendency to give methods (and variables, for that matter) small, abbreviated names. Certainly, a ridiculously long name can be inconvenient, but a name that conveys what a method does needn’t be ridiculously long. Six months after you write a bunch of code, you might not remember what you meant to do with a method called compInt(), but it’s obvious that a method called computeInterest(), well, probably computes interest.

 

Omit needless code!

It’s quite common to find code that’s commented out, but still hanging around. This is mainly bad because it bloats the code unnecessarily. People seem to do this because they want to have the possibility to bring the code back, either because they are writing an alternative they are not sure about, because they don’t dare to delete it. However, in nearly all cases there is no real reason to keep such code around. You should be using version control, and that means you could always find any deleted code again.

Also, since this code is no longer compiled or executed there is no real difference between commenting it out or deleting it. You’ve still made the change. The difference is that now it’s quite likely that this code will slowly move out of sync with the code around it, so that by the time you find you want it again it will no longer work. That actually makes the code dangerous, because it tempts you to include code without understanding what it does. (If you really understood it you could retype it quite quickly even if it was deleted.)

Of course, commenting out code while you are working on the code is fine. I do that a lot. However, I never check in code that is commented out, and whenever I find such code I delete it, without asking anyone or telling them. If they wanted the code they shouldn’t have commented it out. And it will be in the version history, anyway.

I chose this topic because i want to write code in a clean, efficient and faster way. I also chose this topic because since we will be studying it in the CS-343 class , why not learn ahead to get much more understanding on it.

Even though i knew some things about clean coding, i learned a lot. Some stuff i didn’t know where there. With these materials i have gain from other blogs or website added to my little knowledge of clean coding , i hope it will help me develop better coding in a clean, efficient and faster way.

 

I got the above information from this link ::: https://www.ibm.com/developerworks/library/j-perry-writing-good-java-code/index.html 

And

http://www.garshol.priv.no/blog/105.html

Advertisements

Apprenticeship Patterns – Sweep the Floor

For this week, i read on “sweep the floor”. I found it very interesting. As a programmer, there are a whole lot of unfinished project you eventually will not complete on time or not will not complete it at all and will abandon it. As  i was reading i saw some of the things why developers forget, why they decide not to complete a project or why they think this particular project is not a that necessary.

From the readings to my understanding, having finished projects demonstrates to potential employers or employers that you can deliver what you set out to deliver, but it depends on what you mean by “project”.

If you are doing the project with a view to having a complete product that showcases all of your skills and may be something you want to make money out of eventually then yes you should finish it, or at least show that it is actively being worked on.

If you are doing the project to learn specific things (how to stream video, password encryption, what ever) then once you have that aspect working it’s less vital to have a fully functional product as you have effectively completed the project. What you should have is something that can you can show prospective employers to demonstrate your skills.

For example, this might just be a web site that says “Welcome back, Derek odam” after successfully logging in and nothing else – but that’s fine as you are showing off the code behind the website that demonstrates you understand password encryption and secure connections etc.

Sometimes because of a code statement or situation that can not be solved, a project is left unsolved. Which most of the time from the beginning of the project all the information given were to make the project a successful one or even sometimes the better one of the new project. In my small knowledge or opinion i think checking for all unsolved situation will be very helpful in a very long run.

I actually enjoyed reading this part of the pattern because, it makes my know that without the love for the work you are doing, you will always be procrastinating leading to unsolved issues. Which is very bad. Sometimes not very  bad since maybe you are a deadline to which a project has to be completed. In that case skipping a small problem will be a wise decision to take.

Apprenticeship patterns – Record What You Learn

This week, i read on “record what you learn”. On the reading, i got the importance of recording what you have learned. As it is generally said, it is good to keep notes. During your craftsmanship or the process leading you to the real word job. You will need to jot what ever you learn, whether from your master, from colleagues or from your own researches. By doing this you are able to learn more skill faster.

For instance, you have learned a new computer language just because you want to create a new project. But in order for you to complete the project, you will have to learn an additional language. Since it is a new language, it is likely you will forget a whole lot of stuff. And at the same time you will need to learned this new second language as well. So in this case, while learning the computer language for the project. It is required for you to jot up some notes just in case you are learning the second language and its time to join both languages together to complete the project, you will be able to recall all what you have learned or what actually you might have forgotten.

if you want to become the best in your field, taking notes is your ticket to making it happen. As you build your career and keep track of what you’re learning, you’ll have easy access to your very own refresher courses.

Quality notes not only help you build a strong arsenal of knowledge, but they can help make a difference in the lives of those you care about. If a new co-worker needs to get caught up on a project they’re working with you on, they can refer to your notes to catch up without feeling overwhelmed. If a friend misses class due to a family emergency, your notes can help them get back on track. It’s a win-win situation for everyone involved.

Taking records is a nice action that reveals much about a person’s  patience, determination, and attention to details. It also shows you’re efficient and don’t allow what’s important to fall away. From the this pattern, i have come to realized, the reasons for all the blog pages and a whole lot most instructors guide us through.

 

Sprint Retrospective – 2

Hi readers, on my sprint 2, we planned making sure everyone’s was ready to start with the testing. some team members were able to start with the angular and get the sever up and running. Some were stuck, like me. With me, i was able to get most of the things to work, like installing the latest java, nodejs/npm, the karma and the angular itself. Everything felt alright till i tried building the up the project given to us. When i did run the build command, it gives a lot of warning and also many errors. Did not no where i went wrong, so i consulted one of my team member who has finished with his set up and was able to build and also run the server command. He went through, noticed my @angular-devkit/architect was not installed. And also many many of the angular packages were not installed. So he decided for me to uninstall it and reinstall it back.

So i tried uninstalling it but to no avail. So i decided to use the vmware application to install linux OS since i was not able to uninstall the angular. I also learned, linux is pretty very cool when it comes to these kind of stuff. So after installing the linux OS, i did the same process as i did on the windows. Installing the latest java, nodejs/npm, protector, karma and angular. Everything worked fined and when i test for the angular as in the version. I noticed maybe all the packages were installed. That was great till i tried building on the project. when I run the build command, it game me a whole lot of warnings. But this time, it did not give me errors unlike the windows.

For now I am just stuck with making my angular work. Hoping will get help from a team member to see if that will help. If not, i will have to share with someone who’s program is working fine.

While i was also trying to fix my “angular problem” , i was also learning a whole lot of things on angular. Did a whole lot of search, searching on what could be the problem why my angular is not setting up. This made me start learning from scratch. It was really annoying. But i had no choice if i wanted to make this work. I followed most examples and also practiced most of them on my own. I notice when i tried creating a new angular application, it works fine but when i tried running an old project or building on an old project, it give me all kinds of warnings.

Through the week on my angular project and finding ways to make my angular project work. I got to find out that javascript has been upgraded. Which is called ES6. ES6 is a major update to JavaScript that includes dozens of new features. With a focus on simplicity and readability, this course is an efficient way to find out what is new in JavaScript and most importantly when and how you should use it.

I went through this new updates on javascript. I notice a lot of differences and was almost like typescript. It wa very easy to understand and work with. Has less codding comparing it to the old javascript style of coding.

The second sprint, made me learned a lot on angular, learned new things i did not know and also got to know about the new upgrade of javascript. I like working on web development stuff. so i was really excited i got to know about this new update on javascript. I hope the next sprint, things became very smooth and easy.

Apprenticeship patterns – Your First Language

This week i picked the pattern, “your first language”. I was very interesting reading it. I got to know more about why you should learned your first language to perfection. At least master it well before learning another language. From the reading, get a language you like and very easy to use, is the best way to be a better programmer by mastering it. The reason is because all the programming languages follow the say pattern. Only different statement but the same principle. So by mastering one and feeling very comfy about it, make it very easy for you to learn new programming language with ease.

From the readings, to my understanding. I got to know by becoming very good at your first language, you need to ;

  • Use the language all day, every day. Usually this means being full-time employed in the language.
  • Read all you can about the language. Especially, “best practices” and idioms.
  • Join a users group to talk with others about the language and what they do with it.
  • Work with other people’s code! There is no faster way to learn what not to do in a language than to have to clean up after someone who did something awful.
  • Support the code you write – every bug becomes a tour of your worst decisions!
  • Study computer science and languages in general
  • Learn a very different language. A great compliment to C would be a functional language like Lisp. This will turn the way you think about your procedural language inside out.
  • Learn to use the frameworks and APIs available for that language.
  • Take the time to do your own experiments with the language. SICP is not applicable to C, but the attitude of learning a language by testing its limits is a very productive one.
  • Read the history of the language to learn why it was made the way it is.
  • Attend conferences to hear the language authors speak, or to hear what industry leaders are doing with the language.
  • Take a class in the language.
  • Teach the language to others

As I said, reading this pattern made my understanding broadened. I really know by following the pattern given from the book will be a great fit for anyone who want to have a better and easy life at work in the future as a computer software or programmer.

Apprenticeship Patterns – Share What You Learn (week-4)

On my readings on the patterns, “share what you learn”. By teaching what you already know about your profession, you’ll learn more, make new connections with people, and find new opportunities. And by doing that you will may find it very fun.

From the reading, to my understanding, another reason you shouldn’t wait to start sharing with others is that it will help you learn. Research has shown that when we explain something to other people, we come to understand it better ourselves. The process of teaching or sharing what we have learned to others, helps us recognize gaps in our own understanding and better organize information in our minds.

Also one thing i notice about sharing what you learn with others will give your talents more exposure, thus giving the people you interact with the opportunity to identify you as a valuable expert. Helping others can help you build your reputation. Also sharing what you learn can be a great tool for everyone. All you need to do is to be permanently connected to the hot business topics and offer your expertise every time you can. When people are open to prove their value through their competence, it’s easier to notice the ones likely to organize people and to take initiative.

Let’s take for instance, you are in the team, by sharing  what you learn or known and talking about certain decisions and procedures, the new guys or juniors could easily acquire new sets of skills. Create an environment where everybody is encouraged to ask questions, and help professionals in all your locations and job positions stay updated with the latest information in their field. Also by sharing what you learn, increases the productivity of your team. You can work faster and smarter, as you get easier access to the internal resources and expertise within your organization. Projects don’t get delayed, people swimmingly get the information they need in order to do their jobs and your business fills the bill.

I really enjoyed reading the pattern because i got the some main importance of sharing, strategic ways of sharing and things that i do not have to do.

Sprint-retrospective 1

On our first sprint, we talked about karma, the setting of GitHub, help with other members of the team, went through things they did not understand. For example, some were having issues setting up the karma. I myself, was having issues on it too. During gathering, we talk on what has been done, things that will be done including things that will be needed for the project.

During the week i learn learned angular. The reason for learning angular is because, the project we have been handed to do, was done with angular. And i’m not all that very good at it. For me to be able to help the team, i decided to learn it. I started with the basic even though i have studied them. I wanted to know if i still can do the basic. I then practice a whole lot on it. And since it goes along with html,java-scripting, i did a little revision on html and java-scripting. It was fun going back to JavaScript. My first programming language.  Because i have visited JavaScript for a very long time i visited some websites just to refresh my mind on it.

while going through the ng2-amrs folder, i realize, have to learn Typescript. Typescript which is the same as JavaScript but it just is a sub of JavaScript. It is well organized than the JavaScript. for instance, when creating classes, with typescript, it is easy and less code while you can do the same with JavaScript but will have to implement a whole lot of code and it is not easy too. I got some tutorials on from https://www.typescriptlang.org/. It really helped me.

Next thing i did, was looking for the  best IDE which i will use for the project. I wanted to pick eclipse but i found out that most of the members of the team were using visual studio. And since i’m with the team, thought having the same IDE will be the best. Just in case i do have a problem, one of the team members can help me out. And beside that, i learnt visual studio is very easy to use. So after the IDE i will be using, i started doing some research on it. during the research, i got to know it belongs to Microsoft. I downloaded, and tried a hand on it. It was a little bit confusing. But with time as in the next two day, i got to know how to use it. very very cool application. I did a little fast learning on YouTube. How to use typescript/angular on the visual studio.

The reason why i took this step for the week was to be very well prepared for the project. Also to be very active with the group discussion and what is being worked on. Also to be able to handle small matters so that i don’t worry other team members, by doing so we speed up on our work/project. The week was fun.

links : https://www.w3schools.com/js/

https://www.typescriptlang.org/

https://visualstudio.microsoft.com/vs/older-downloads/

IndividualApprenticeship Patterns – Draw Your Own Map

Draw you own map was the topic i picked for this week. I really enjoyed reading this pattern. It gives me the understanding of how to beat or overcome the huddles of learning new stuff and become very good at what we do or love.

From the reading, i also got to know, in order to be happy at your work place or your next career. You will have to pick a field you enjoy which most of my time i will be spending. The stories that Desi and Chris’s gave, give me the knowledge that planing your career at an early stage helps a lot. It makes you know what to expect and also what you want. Also from their stories, they could have prevented ending up in a a job they dislike by getting more info or doing more research about their next career or job. In case you did your research and landed to the job and later found out that it is not good for you. It is not late to change or learn new stuff. The pattern also gives ways on how to draw you own map. To me, it was very easy to understand and also felt it is the best way. The actions taken when drawing the map will definitely land you to the dream job you are looking for.

I really like this pattern(draw your own map. It has showed me a lot on how to really design my map in terms of my next career or job. it has also open my mind not just on my field of studies after graduating but also in life in general. I realized this technique that book showed, applies to everything. Not only for programmers or computer science students. I even made my own map which was really fun. I add four desirable jobs i will want to have for the next six years to come. On map diagram, about the desirable jobs, i included jobs no related to my field. Added fields like being a chef and nurse. Also added things i should be learning, whether things not related to my field or new programming languages.