Jacob Baytelman - Full stack developer, project manager, CTO

How to write code that actually works

1. In small iterations. Type a line of code, 2-3 lines maximum. Upload it (or compile it) and see that it has no syntax errors and that it does whatever you expected it to do. Otherwise fix it.

Don't write a huge bulk of code at a time. If it has an error, and it's very likely to have an error, you will struggle to locate it.

2. If you do have a huge bulk of code and something is wrong with it, insert debug / log outputs after each 2-3 lines of code and run it. Examine your log outputs, thus you will see how it runs and what it outputs. If you can execute your code line-by-line in a debugger or have breakpoints, you are lucky. But even so there are situations which you cannot debug this way, because they happen too quickly, or are event driven, or whatever.

That's why, my old school approach is universal and logs are your best friends. After diamonds, of course.

3. If your huge code does not compile, simply comment out most of it, leave a small part which passes the compilation successfully. Then start uncommenting the remaining part, by small pieces. And try to compile. Thus you will find the cause of the problem.

4. If you have to implement something new for you, try googling it first. You do not necessary have to copy-paste ready code snippets, but you definitely have to see how others approached this issue and probably get some inspiration. Or a ready good piece of code :) or an API to solve your problem.

5. Always check all user's input. Nobody will do it for you. It's your responsibility to protect your code from sql injections, cross-site scripting and other vulnerabilities. Invest some time in learning about security. If you do not architect your system securely, you will face a nightmare trying to insert security into it later.

6. Don't neglect your users, whatever you code, make it friendly to them. Respect their data.

Check my advices for coding when you are in a startup.

J.Baytelman September, 2017