Posts

Showing posts with the label Programming

Code Simplicity Takeaways

Code Simplicity is a good book if you are wondering how to simplify a new or existing software so it is more maintainable and easier to develop. In a nutshell, only-as-needed design and incremental development are the two foundations of simplicity. Only-as-needed design By designing based on what is known now , the design can be created as simple as it needed to be. Simple design where components are broken down to its minimal requirement also tends to be simpler to extend, change, fix, or remove from a system. Simple design also eases new programmers into the system and speeds up their time to start contributing. Simple design might come from re-using existing solutions that might only fit most of the requirements rather than creating new ones that fit all requirements. When a system is using a maintained library or technology, it outsources a complex part that is not part of its purpose. However, be careful of libraries or technologies that lock the system into their environment t...

Experiment #1: Can JavaScript run something like a concurrent thread/process? No.

So, I know it is stated frequently that JavaScript is "a single-threaded" language. But it is non-blocking thanks to Web APIs. However, I was thinking whether it means that we can run something concurrently like in some other languages like Java, Go, or Python. I mean ES6 includes Promises and it was enhanced by async-await. So, is it possible to make use of Promises to run multiple things in the same time? It was fairly a simple experiment. All you need is A function that acts as a Promise (an async function), A method that will be run as a Promise and as a synchronous function, and A "container" function that holds both async function and plain function. With that three method in mind, this is the code that I came up with. function timelapse ( id ) { for ( let i = 1 ; i <= 1 _000_000; i++) { if (i % 500 _000 === 0 ) { console .log( ` ${ new Date ().getTime()} ${id} ` ); } } } async function asyncFunction ( id ) { ...

Ruby on Rails Time, Date, DateTime Cheatsheet

Rails has a lot of handy methods built-in that sometimes all you have to do is know it. In consequence, there are an overwhelming methods available to get specific time and/or date. This cheatsheet should provide some practical methods for Time, Date, DateTime that may help you in developing with Rails. I have also included an example output of each method so you don't have to manually enter it in rails console to know what the outcome would be. Here is a brief overview of what methods will be covered in this cheatsheet. Feel free to skip to the methods that you need or scroll around to know what methods there are. Maybe it will help you in the future. Enjoy! Current time and date ( now , current , today ) Specific day ( yesterday , tomorrow ) Specific moment in time ( beginning_of_minute , end_of_minute , beginning_of_hour , end_of_hour ) Specific time of the day ( beginning_of_day , middle_of_day , end_of...

SQL WHERE and HAVING clause (with examples)

Image
In SQL, there is two clause used for filtering: WHERE and HAVING . Most of the time WHERE clause is sufficient. However, the reason why HAVING clause exist is because WHERE clause cannot filter aggregates. To show an example of how each clause can be used, let's say we are running an ice cream company. We produce several flavours of ice cream. We sell these ice creams to stores in multiple countries. Each store would have different stocks of our ice cream varieties. Our users are from all over the world. From that example, we have the following schema of the database. In the following section, I will show some examples for both clauses in PostgreSQL . I have created the database dump  so you can experiment with it yourself. WHERE clause We can use the WHERE clause to search stores that is available in a county with the following query. SELECT * FROM stores WHERE country = 'ID'; We can also use it to find users who cannot access o...