Minimalist programming for the web

I’m not the most minimalist person in real life: I always have a ton of things going on, be it gardening, cooking, crafting or coding projects. This is a pretty common plight for a programmer since we like to tinker. But when I code something for production and not as a toy project, I believe that minimalist is something important to keep in mind. This way, my code is simpler and easier to maintain for me and for other people.

There are many aspects to minimalist in a software project, from your technology choices from the start to good coding practices when the project is ongoing.

Keeping you code simple

The number one priority is readability and easy of maintenance, by you and by others. Trying to reduce the length of variables names or function names at all costs gets in the way of being understood. You want to keep a reasonable length so the names are clear while still being able to fit a line of code on the screen. Another good thing is to use a common coding standard for all the code on a project: there is no one true coding standard, but it’s easier to understand code without distractions when it all looks similar. In doubt, you can just use the same standard as the library or framework you are using.

Also, you want to eliminate pointless complexities in your code. First, all the code in your codebase should be actually used. Don’t code things just in case they may be needed. It just adds more moving parts that needs to be maintained, and when the case you coded it for finally happens, you’ll most likely find out that what you need is a bit different than what you implemented anyway. Following the same ideas, parts that are no longer needed should be removed, not just commented or left lying around. You can always retrieve the code you removed using source control if you ever need it again.

Finally, keep the things you are doing simple, for example, don’t use CSS selectors from hell if you could simply add a meaningful class to your markup instead. Also, useless conditions should be eliminated: if doing a piece of logic every since time has not negative consequences and has a negligible performance cost, you can do it all the time. It may even be faster to just run the code instead of checking a condition! Also, you can use returns to leave methods as early as possible to reduce the levels of indentation and make code easier to read.

Less is More Minimal Simplicity Efficient Complexity Concept

Using libraries

If your goal is maintainability, you can’t always go with the shiny new toy: something new is always a risk, and you don’t want to spend a lot of time with it if it’s going to be dropped in a few months. So, when I create a new project, I aim to start with as little components and tools I can get away with. The simplest code that is closest to the HTML/CSS/JavaScript standards are always the best for long-term maintainability.

For example, for a ASP.NET MVC web application, even the default template is pretty bloated: the ASP.NET MVC framework itself (and the dependencies like the Entity Framework), jQuery and a good control library like jQuery UI is more than enough. Any additional library that you add have a cost: they will need to be maintained and updated, and you have to keep yourself up to date with all the new upgrades. If you are not using the library at the moment or only a tiny part, it’s a lot of noise to sort through while you could be doing something that moves the project forward.

Don’t get me wrong, I’m not anti-library: I don’t believe in reinventing the wheel, and if a part of my application has special requirements not covered by the .NET framework, I’ll gladly use a library that implements it. For me, a good library to use is one that does the job it needs to and not a few dozen other things, has good documentation and is regularly upgraded and maintained. It also should not take over my architecture by forcing me to use complicated patterns and by pulling along any other dependencies.

Separating concerns

I’m not a testing wizard, but there are many things you can do to help yourself start on the right foot even before you start writing that first unit test. First, you should have a simple but clean architecture, separating the business logic in the model, the controllers getting the data and the views handling the display logic. The truth about the business logic should be kept in one place in your architecture so it is easier to modify it. For performance and usability, you can add a few validations to the client-side JavaScript, but the server-side code saving the data should always has the last word. A validation that’s on the client can always be bypassed anyway, so you should never trust something sent to the server by the browser.

Also, on a lower level, you should make it clear what are the responsibilities of each class and method is doing and document each of them properly, including all the edge cases. I don’t believe in zero comments: the documentation is there to explain why you are doing something and how to use that method, not just to repeat details about the inner working of the method. A method should do only one thing: if it gets too big, it’s going to be hard to know what are all the possible code paths. Nice and small methods or views with clear responsibilities are easier to reuse in your code, and don’t need as many new tests since it’s known code.

Sign up now to get my newest articles about making applications and my ongoing side project delivered straight to your inbox.

5 Great Snacks For the Coder’s Brain

You’re coding and you’ve been in the flow for a few hours. Everything is going just fine, but then hunger strikes suddenly. Since you don’t have anything on hand, you go to the cafeteria or to a nearby cafĂ© to grab a muffin or a pastry full of sugar. When you sit down to resume coding while eating your snack, you feel good for a few minutes, but the sugar spike soon wears off. You start feeling like crap, and your productivity screeches to a halt. Sounds familiar?

OneDoesNotSimplyGrabASnack

This happens to everybody from time to time. Working with your brain makes you hungry very fast, but eating a large sugary snack to compensate and topping it with an energy drink is the least productive way to go about it. If you’re well prepared and have good quality snacks on hand, you won’t feel like crap after eating and you will be able to resume coding in no time. Great snacks are also power foods that are great for your health, so it’s worth adding them in your diet.

So here are a few snacks you should try:

  • Nuts and seeds: this is one of the best power snack to keep around since they store well and you don’t need to eat a lot to feel better. You should be making your own mixes if you can, since most trail mixes available on the market are too salty, too sugary or both. You can add all your favourite nuts, but almond, walnut, brazil nuts, pumpkin seeds and sunflower seeds are all great choices. You can add a bit of quality dark chocolate or dried fruits to the mix for more variety.
  • Fresh fruits: All fruits are also good snacks. You can bring along anything from bananas and apples, but for pure nutritional values berries are the best. I don’t recommend fruits like kiwi that need to be peeled since you don’t want to mess around when you want something quick. Fruits can also be kept at your desk for a few days if you want to bring some in advance.
  • Yogurt or kefir: Yogurt is a great source of protein and probiotics, but you should choose yogurt with as little sugar as possible: you can add your own fruits if it’s too bland for you. I’ve started drinking kefir recently, which is a fermented milk drink with even more good bacterial than yogurt. It’s interesting but it’s an acquired taste, being a lot sourer than yogurt.
  • Egg, fish or avocado: Those require a bit more preparation, but the proteins and good fats that make it worth it. Boiled eggs takes only a few minutes for a week’s supply and are easy to eat. Avocados are also fast to prepare: just cut them in two, add some mustard or lime juice and pack them in your lunch. Half an avocado is enough for a snack, and it can easily be eaten with a spoon. And you can bring canned sardines or smoked salmon, which requires no preparation.
  • Vegetable sticks: All raw vegetables that can be cut into sticks like carrots, celery and peppers are good options. If you don’t have time to cut vegetables, you can buy bags of precut vegetables at the grocery store. Pair them with some hummus dip for added protein and you’ll have a great snack on hand. This is a good way to add more vegetables to your diet, we could all do with more of them.

As an aside, if you really like muffins and can’t do without, you can make your own whole grain muffins and use less sugar. Homemade muffins will give you less of a crash than white flour and allow you to add more fruits, nuts and seeds. You can also make smaller portion when you bake them yourself, since most store-brought muffins are huge. This is a bit heavier than the other snack options, taking more energy to digest. Many people have trouble when they eat too much grains or gluten, so you should try it and see what works for you.

Which snack are you going to try? Do you have a favorite snack that’s not on the list? I’m always on the lookout for new ideas, so please share your routine!

Dealing with the outside world for programmers

To work with the outside world where business and entrepreneurs lives, you must understand that we are in an alternate universe. For a programmer, playing with technology is fun. We live in a world where building your own server and rolling out your own custom solutions to every problem is something desirable. If something is hard to master and uses bleeding edge technology, it’s even better!

But when you step out in the real world where entrepreneurs of all shape, size and technical skills build businesses and deliver value, they don’t care about what is going on under the hood. When people need a business solution, what they really want is something that makes their job easier. That’s perfectly logical: who would implement a solution that makes their live harder and more unpleasant?

So, considering this, the role of a programmer in the business is to use technology to solve problems for people. Your job is greasing the wheels of business to make sure everything goes smoothly, regardless of the technology involved behind the scene. If your solution slows down people and gets in the way, you failed your client or your employer and this is something that should be a priority for you to fix.

cin-hammer

If you don’t fix your software when this happens, people using it to do their work will find a way around it instead of working with it. A tool is pointless without people using it, and if less and less people are trusting your software, the information in it will become unreliable. Software is alive and should evolve with the need of its users: if you don’t care for it, it will wither and die.

Sometimes this will be at the intersection of business needs and fun: a new tablet application for users needing mobility or a new web site that works with the newest browsers. But it won’t always be fun and unicorns: at other times, doing your job means maintaining an old PHP web application that’s used only by a few people, but that is essential to keep the business going. Or it means making sure that your application works fine on old Windows XP desktops that are still in use even if you’re developing in on the latest MacBook.

So, you have a choice: if you want to deal with the outside world and make people glad to have a programmer around, then you need to learn that the best solution is not always the complex one. Yes, this can mean that the solution to their problem is a WordPress installation and not a custom solution that took you a year to code, but your job is to make people happy.

Will you come with me outside?

And for any entrepreneurs reading this, where are we failing you?