After joining back from month long vacation, my manager greeted
me by asking me to do code review of basket full of new/existing automation scriptsL. When I started, I found
that the automation scripts were not up to the mark and needed lot of
improvement to be considered quality scripts.
I ended up creating a basic coding guideline document for my
team which I am sharing with all of you. Please note that these guidelines are for Java Programming Language.
Coding Guidelines:
- Comments at method level: For each method that we
write, we should provide comments suggesting what the method is expected to do.
In large projects where multiple teams work, comments are very helpful.
- Comments at
code level: Inside any
method, we must write comments where ever we are implementing a complex functionality. Other teams / team members will have tough
time understanding this complex piece of code if there are no helpful comments.

- Final Variables: We must ensure that as a practice, all final
variables must be defined as all uppercase letters connected by underscore.

- Extensive use of try-catch blocks: At no point, we would want
our automation scripts to crash because of uncaught exceptions. I have not seen
try-catch block being used often. As a practice, any piece of code that we suspect
can throw an exception must be enclosed between a try block followed by a
corresponding catch block or a finally block.
- Local
variables VS Instance variables:
When writing code, start off by declaring variable locally and then move it to
instance level as per requirement. There is no point declaring all variables at
instance / class level when it is not required.
- Do not hard code xpath: At no cost, should us hard code xpath directly inside a
method. Instead declare it as final variable at class level and then use it
where ever required. This point is crucial from code maintenance point of view.

- Reuse existing code: Don’t
be afraid to reuse existing code rather than duplicating it. Have a look at this
example:

- No hard coded values: Do not use hardcoded
values. For some reason let say we need to pass integer 5 as parameter to some
methods. Rather than passing 5 straight away, we must declare an instance
variable and initialize it with 5. Now pass this variable as parameter. Easy
maintenance.

Hoping that above listed coding guideline will help you
write effective and optimized code. Please do not forget to comment your thoughts about this
blog.





Hoping that above listed coding guideline will help you
write effective and optimized code. Please do not forget to comment your thoughts about this
blog.