Wednesday, 21 August 2013

Tips to be a better programmer

Tips to be a better programmer

There should be only ONE single exit point to each method (use if-else
whenever needed).
When using if-else, make sure to place the SHORTER code snippet in the if:
if (cond) { } else {
}
Do NOT throw exceptions if you can avoid it, it makes your code MUCH
slower, if you feel like throwing something and then catching it – go play
ball with your dog. If you don't have a dog get one – they're awesome!
Do NOT try to do many things on the same line – when you'll get an error –
it will be harder to debug, example how to NOT write your code:
String result = hasInformation()? getState() : (hasMoreInformation() ?
getOtherState() : getState());
Look for code pieces that look the same and if you find any – REFACTOR
your own code!
Meaningful names are a MUST. If you're not sure, meditate on it for
another minute. Still not sure? ask your colleagues for their opinion.
Whenever you can use HashMap instead of List/Queue – use it!
If you can use caching instead of I/O (usually DB) – use caching
If a nice and simple regex can do the job – use it!
Do not use regex for parsing (for example: HTML/XML/json) – there are
special tools for that in each language, for example in Ruby, Java etc.
Print to Log. You should have at least two levels of logging: DEBUG and
ERROR. The latter should be the default. Nice tip: you can send your self
a text when a critical error occurs, by sending an email to @
Use stackoverflow – not only for asking questions! take a few minutes,
every day, and try to answer questions – you'll be surprised how much
you'll learn from it!
A rule of thumb: if your method is over 50 lines – split it. If your class
is over 500 lines – split it. If you think you can't split it – you're
doing something wrong. This recommendation is a "language dependent" and
shouldn't be taken "as is": while 50-line method feels natural in Java/C#
– it will be considered very long in more expressive languages such as
Ruby & Python.
Writing a code which is "self explanatory" is great but also very hard, if
you're not sure how "obvious" your code is – use code-comments.
When writing code comments always assume that the reader doesn't know what
you're trying to do. Be patient & explain. No one is going to bug you
because your comment was too long…
You want to improve? read books, blogs, technical online newspapers join
relevant groups on Linkedin, update yourself with the latest technologies,
go to conferences, got the point ?
Practice makes perfect: solve code-challenges, join hackathons, go to
meetups etc
Experiment with different IDEs until you find the one that "feels right",
study it carefully and make sure you know the major features (including
plugins). Tune-up the keyboard shortcuts – it will make your workflow
smoother.
Whenever you find a bug, before you fix it, write a unit-test that
captures it. Make sure it does.
and my favorite:
Don't get lazy – RTFM

No comments:

Post a Comment