Hey Juniors, Here are simple things you should do to show your professional styles

You have just graduated from university and started your professional career as software developer? Here are some tips which will help you become more professional in the eyes of your manager and co-workers. These tips are based on my experience when working with many young developers. And they are the mistakes that you should pay great attention to, although they are so simple that you often do not treat them seriously.

Indentation

Always keep your indentation consistent, never confuse you and your co-workers with strange lines of code, like this:

if (active)
a = 5
else
  b = 6

And use only 1 style of indentation in your code. It means that you have to decide whether to use Tab or Space for indentation, how many spaces you should use (2 or 4 is very common). I am using Sublime Text and it allows me to use my own styles consistent among different source files in a project or different projects. Here is my current Sublime setting that you can take as reference:

tab_size: 2
translate_tabs_to_spaces: true
trim_trailing_white_space_on_save: true

Separate methods with blank line

Yeah, this is so simple and basic, but many developers forget about it somehow when I review their code. This of course does not affect the logic of you application, but it is very uncomfortable for others to look at your code. Here is one example of bad code in this style:

def new
  #something here

end
def edit

  #another thing here
end

You see that, right? It is hard for even you to quickly know that there are two methods in this code. And this is even worse because this developer put redundant blank line above the end of the first method. Don't do that, always put blank line above your method definition to separate it with other methods, like this:

def new
  #something here
end

def edit
  #another thing here
end

Separate code blocks with blank line

To make it easier for you to debug your code, make sure to separate a complex logic into different part so that it could be easily understood. And example of difficult-to-understand code is:

def update_order
  @order = Order.find(params[:id])
  @order.update_total
  if @order.state == "delivery"
    @order.state = "payment"
  end
  if @order.ignore_payment?
    @order.state = "completed"
  end
end

I should rewrite the above code like this

def update_order
  @order = Order.find(params[:id])

  @order.update_total

  if @order.state == "delivery"
    @order.state = "payment"
  end

  if @order.ignore_payment?
    @order.state = "completed"
  end
end

You now can see that it becomes much easier to understand the logic in this method

Pay attention to spaces

Remember to put spaces around operators, don't do the followings:

@order= Order.find(params[:id])
@order =Order.find(params[:id])
@order=Order.find(params[:id])
@order     = Order.find(params[:id])
@order = Order.find( params[:id] )

It is better to write them like this:

@order = Order.find(params[:id])

However, this should be reversed in HTML code, I don't like the following code:

<div class = "col-md-6"></div>

I would rather get rid of the spaces around the '=' characters in HTML code, to make it like this:

<div class="col-md-6"></div>

And another thing, do not put space in front of separators such as commas or colon, and do put a space after them. Example of bad code:

var a , b,c;
a = 5;b = 6 ;

Good code would be

var a, b, c;
a = 5; b = 6; //actually, you should separate them into two lines, I put them in one line for illustration

Use plural for list of items, singular for a single item

If you have an array of students, please remember to use plural form of your variable, like the following:

students = Student.all

Do not confuse other people by doing something like this in your code:

#controller
student = Student.all

#views
student.each do |s|
  student.print_name
end

Others will think: What the heck this guy is doing, should student be a single item?

Or even worse, use a variable with completely different meaning with its context. For example:

today = Time.now 

should be

today = Date.today #or Date.current

Pay attention to spelling

The final thing I want to mention in this list is if you are uncertain about a word or phrase, please look it up in a dictionary to make sure it is correctly spelled. Never use a misspelled word in the code, especially for something that could be used by many other people in your team. An example:

class Chalenge 
end

It should be

class Challenge
end

It is said that simplicity is the ultimate sophistication. Following these simple rules and you will become a better developer. Of course, there are many more things to pay attention to and I will introduce them later in another post.

Happy coding!