Saturday, November 3, 2012

Coding Guideline

Least Privilege Principle:
  • Put as much restriction as possible. 
Class:
  • Single Responsibility Principle - There should be only one reason to change a class. 
  • Class should always be responsible for maintaining its valid state. 
  • Class shouldn’t expose its internal implementation. 
  • Tell, don’t ask - Get the information you need from others, and then do your own calculations and make your own decisions. Don’t give up control to others! 
  • Main should be just the entry point of program, it shouldn't contain any program logic. 
  • Class containing main shouldn't contain any other fields or methods. 
Constructors:
  • Constructor should do minimal amount of work to make the object in valid state. 
  • Use factory or builder pattern when you need parameter validation. 
Variables:
  • No public variables. 
  • Variables should be exposed only by properties. 
  • Don't reuse variables. 
  • Always try to minimize the scope of variables. 
Methods: 
  • A method should do only one thing and does it well.
  • Don't mutate parameters of a method; if you do, return that parameter and make it explicit. 
Comments:
  • Comments that contradict the code are worse than no comments. 
  • Comments aren't replacement of documentation. 
  • Do not comment WHAT you are doing, but WHY you are doing it. 
Static:
  • Don't use static variables unless you have a compelling reason as static variables introduce global state. 
  • Think twice before introducing static methods. Does it belong to an object? 
  • Static methods shouldn't change any object's state. 
  • Regardless of state, static methods should give same output for same input set. 
Inheritance:
  • Favor composition over inheritance. 
  • Don't use inheritance for adding extra functionality. 
  • Don't use inheritance for code reuse. 
  • Use inheritance only when there is an "IS A" relationship 
  • Always obey Liskov Substitution Principle. 
Interface:
  • Code against interface as it reduces coupling, separates behavior from implementation 
  • Interface should be granular - an interface should only contain logically related set of behaviors. 
Validation:
  • Don’t trust user input or external data. Always validate them. 
Exceptions:
  • Exceptions are for exceptional scenarios, not for normal logical code flow. 
  • Don’t swallow exceptions without proper explanation 
Contract:
  • Design by contract. 
  • If you assume anything make it explicit by using assert/contract. 
  • For external library adopt Defensive Coding. 
Collections:
  • Use empty collection instead of null. 
  • Use list instead of array as list allows more flexibility unless there is a compelling reason. 
Optimization:
  • Premature optimization is root of all evil. 
  • Always optimize algorithm and data structures. 
  • Don’t micro-optimize as modern compilers are better in micro optimization. Micro optimization often leads to unreadable and buggy code. 
  • Optimize only when you need to. 
  • Don’t optimize without profiling your code or knowing the bottleneck. 
Miscellaneous:
  • Code for humans, not for machine. As machine only understands 0 or 1. 
  • Avoid Deep Nesting. 
  • Whatever you do, be consistent. 
  • Limit line length. Shouldn’t be more than 120 characters. 
  • Refactor early, refactor quick. As more time elapses, refactoring becomes more hard and costly. 
  • Write the least amount of clearly understandable code as number of bugs increases with LOC. 

Advice for undergrad students


  1. First read Teach Yourself Programming in Ten Years. This is one of the best articles for freshers. Every advice given in that article is priceless and applicable to not only to programming, but to any other profession.
  2. You may be interested in programming competition or research sort of thing. But whatever you do, learn at least one programming language very well and also its advance features. If you don’t have proper language skill, then you can’t know for sure your algorithm is going to work. Yes, you can learn any language you want, but to keep pace with software industry learn at least one object oriented language. C++, Java, C# would be great choice as they are widely used and have great libraries.
  3. Although there are many programming paradigms, object oriented paradigm is the dominant paradigm in software industry. Almost all the popular languages support object oriented paradigm. You should have knowledge how to design classes, assign responsibilities and properly sew them together to produce reusable, readable and testable code.
  4. You should learn at least one dynamic language for scripting. You may be a fan of static typing, but there’s some scenario where dynamic typing really comes in handy. Python, Ruby and Perl are great choice. Dynamic languages are also very popular in web development. If you really feel interested, you can learn one functional programming language. Functional programming is gaining popularity due to the rise in parallel and multi-core programming. F# which is supported on .NET and Scala on JVM are two great options for learning FP.
  5. Develop one great academic project to awe your future employer. It doesn’t necessarily have to be a big project. But keep your code clean and make your design elegant. And it should be developed in an OOP language. Remember, as you don’t have any industry experience, these sort of projects really come in handy to prove your coding skill.
  6. Use source control to keep track of modification of your source codes and keep backup of your codes. Although Subversion may be still very popular. But if you haven’t any prior experience with subversion, then you should start with distributed version control system. Git and Mercurial are two most popular distributed version control system. You can start with any of them. By comparison mercurial is somewhat easier than git.
  7. Keep your academic projects hosted in any online code hosting site.It serves multiple purposes. You have a safe backup storage, you can access your code anytime anywhere, provided that you have a decent internet connection and anyone (including your interviewer) can have a look at your code. There are several popular code hosting sites, among them github is the most popular one which uses
  8. Learn about refactoring and code smell. For writing clean, readable and maintainable code, they are extremely important. We usually code our academic project in hastily and haphazard manner with no code organization. We do this, because no one is going to read our source codes apart from us and no one has to maintain our academic projects. This sort of throw away coding may be acceptable for some scenarios in academia. But in software industry, this is simply unacceptable. Because, industry projects aren’t just for few months development and maintenance. Many projects are being actively developed and maintained for decades. Term projects or assignments have tiny codebase and industry projects have much more bigger codebase. If they aren’t written cleanly manner, it would be next to impossible to develop and maintain these projects. So focus on code readability.
  9. Many CS students aren’t familiar with testing. And many of them don’t even think testing is an important part of development. Writing testable code properly is an essential skill of a software engineer. Be familiar with software quality assurance and different kinds of testings - acceptance testing, regression test, integration test and most importantly unit test. You have to work with SQA engineers side by side. So if you don’t have any idea about their task, you may face some difficulties.
  10. Unit testing has become industry standard nowadays. And writing unit test isn’t testers’ responsibility, it’s programmers’ responsibility. At any software farm which maintains code quality, you have to write unit tests for your own code and make sure that every unit test passes. Unit testing isn’t all about testing per se, it’s about designing your code properly and making sure your individual module works in every probable scenario.
  11. Learn about software development methodology. Agile software development is currently the most popular one. Learn how projects are developed and delivered to the client using agile methodology. You can start with scrum which is widely used, a variant of agile methodology.