11 Tips That Will Help You Write Better Code
11 Tips That Will Help You Write Better Code
11. {
12. return
false;
13. }
14. if
( hours < 24
20. {return
false
21. ;}
There is no right or wrong, but just the way you feel most
comfortable with.
2) Make comments
There is a love and hate relationship with comments and there
is a fine line between helpful comments and redundant ones.
It is, in fact, very useful to use them to explain the code,
especially complicated passages. Comments will help you and
others understand why you did what you did. It may seem
obvious to you when you are writing it, but will others who
work on the code understand it? Will you remember what you
did after a month or a year?
Comments are worthwhile as long as they are a few and written
for key passages. Too many comments at every single line will
make the code completely unreadable and messy, no matter
how neat the style and consistent the indentation.
3) Consistent name scheme
Usually, Languages have their own naming conventions. For
example, java uses camelCase. Naming needs to stay
consistent, otherwise, it will very difficult to find things inside
the document.
There are two main ways to name things:
o camelCase: which means that every word of the file
name is capitalized except for the first one, e.g.
nameFilesConsistently
o Underscores: in this case you write underscores
between each word, e.g. name_files_consistently
A typical example is a web page: most pages will have the same
header and footer, but there is no need to copy-paste the same
code onto every page, just link to it.
9) Code refactoring
Every developer knows this – you write some code because you
want to finish the feature and in the end, it works. When you
then look at it after some time you think – did I really write
that? I could have shortened it so much! So you start rewriting
that piece of code better, probably shorter, without changing
any functionality.
Recommended reads:
Design Patterns: Elements of Reusable Object-Oriented
Software
Clean Code
Head First Design Patterns: A Brain-Friendly Guide