It is good practice to leave your database in a consistent state. There are different ways to do this. Foreign key constraints, indexes, typing of columns, are all strategies to keep your database in a consistent state. Transactions are another way providing you a tool to keep the database consistent: if one of the inserts or updates fail, your database will rollback to the state before the first in the series of inserts and updates within that transaction.
Some languages make it really simple to create a transaction. In Ruby on Rails it is simply opening a block:
User.transaction do
## ... all db operations are now in a transaction
end
But be cautious; transactions don't come for free: they lock the table or row, which is bad for performance. It can, by design, stop other processes from updating the same rows. And all this gets worse when transactions take longer, when for example they contain request to remote resources.
Hence, my approach to transact…
I'm quite familiar with writing ruby code. I've even expressed my love for the language long time ago. But some constructs still bug me. One of them is unless…
I've seen variants of this code:
temperature = 36.7
unless temperature > 37.2 || temperature 35.5 && temperature < 37.2
puts "Healthy temperature. Temperature is within the normal range."
end
or perhaps even nicer:
if (35.5...37.2).include?
puts "Healthy temperature. Temperature is within the normal range."
end
For projects I maintain, I try to keep dependencies up to date on a regular basis. But not all people work like that, some live by the adage of "if it ain't broken don't fix it", but that is not an approach I subscribe to in software development.
A common reason to update software dependencies is to fix security issues or bug fixes that plague the project at hand. My main argument in favour of making more frequent updates is that when you suddenly need to make an update (because of an imminent security threat) it won't be hard; when dependencies haven't been updated in a long time it can be hard to to make the update.
There are risks involved in updating dependencies: A new version might introduce breaking changes, things that you rely on suddenly don't work or exist anymore. It might even introduce new bugs that may not be apparent on the first run. And when your test suite is not on par, verifying if everything works as expected is time consuming. But that can all be address…
There are a few hard problems in computing. Correctly handling time, naming, preventing off by one errors… sorting text may not be one of them but recently we ran into a discussion where I couldn't make up my mind anymore. Hence, this post's topic: sorting text.
How do you sort the following words:
If you'd ask ruby I'd get:
%w[cheese Ape Drums dent Beer].sort
Results in:
Which in my useless and ramshackle programmer's brain translates to, well why not, it is sorted right?
But then we moved the data into a database which was correctly set up with a proper locale for 'collation', a term that I've seen but never meant anything to me until this problem. Collation is:
> the assembly of written information into a standard order.
(thanks Wikipedia - Collation)
Databas…
Recently I was reviewing a merge request of some front-end code, and a simple div, that changed a bit of the custom appearance of a block of text through a few custom classes, was changed in a call to a view component that then applied the same classes, passed onto the component through a more deeply nested hash.
> -
> +
’itis ](https://en.wiktionary.org/wiki/divitis) you get ‘TextComponent’-itis.. we don’t add better semantic or structural information to the page layout
Keep the code as is. And perhaps create a ticket (or annotate it with TODO:
) that you perhaps want to extract this ‘custom-class’ call into a true reusable component. While I don’t think there is something inherently bad with offering the option to override or add some custom classes to a component, a component should only be used if it adds structural meaning either from the developer's side of things or (better) from the consumer’s side of things (e.g. semantic output that can be p…
Still (2013) a great talk by Sandy Metz on testing, and how to do it right, without getting too theoretical. While this talk is on ruby, and it uses a Rails framework for testing, it really is applicable to any other language (only the syntax will probably be a wee bit shittier ;))
Watch Rails Conf 2013 The Magic Tricks of Testing by Sandi Metz on YouTube
(and while unit-testing is between brackets, in general, being a minimalist when writing code really is a good idea)
Webpacker is still opt-in for new Rails projects. But this might change. The JavaScript ecosystem is moving fast and new JavaScript frameworks are pushing customer’s expectations to higher levels. To use these frameworks with your Rails app, you had a few options:
npm
or yarn
into the asset pipeline yourselfI have been using rails-assets.org the past few years to keep my JavaScript dependencies up to date. It thought it was smart solution; instead of requiring individual developers to maintain Gem-wrappers, Gem wrappers are created on the fly by RailsAssets.org. It was smart and light weight on the developers side and worked perfectly with the Rails' Asset p…
I've been able to stay away from PHP-based projects for quite some time. Until recently. I needed a small API. The idea was that the API would be transferred to a relatively old server that had been running stable for years and the client didn't want to risk installing additional script interpreters on it. It might even have been my own suggestion, it would be a really small API, requiring no special changes on an already operational 24x7 managed server. On top of it I'd write a modern style front-end, running entirely in the browser.
Of course the API that was intended to be simple got a bit more complex. I wanted the API to output clean JSON messages, which required some data mangling, as data was stored in CSV's, TSV's, and misused XML-files or only accessible through crappy soapy API's. So what does present-day (well, the Red Hat PHP version I was able to use is still in 5.x-series) PHP look like? TL;DR: sometimes it was definitely ugly, but at I can happily live with the cod…
As the lead developer at HeerlijkZoeken.nl I wanted to try the new Rails ActionCable technology for a new feature: shopping lists. The idea is that you can walk in a store or on a market, mark an ingredient as checked when you add it to your (physical) basket and continue shopping. ActionCable can make the experience nicer because it, based on WebSockets, allows for real time notifying other viewers and editors of the same shopping list. No more shouting around in the supermarket: I’ve got the milk! Sure, nothing essential, but I needed an excuse ;)
(Note that we recently migrated from Rails 4, so not everything was in place in our app, just ignore the bits Rails already made for you; everything has been tested with Rails 5.0.0.1)
To start: You need a web server that can open multiple threads, so if you’re still using Webrick in development (which can’t rece…
In a previous post I described how simple integrating elasticsearch is with Rails for beginners. You could've been happy with the fact that you now have implemented full text search, but that too basic set up probably doesn't work that much better than adding a column to your model, throwing in all text in it and running a LIKE query (although elasticsearch does try to rearrange the results a bit).
In this post I will learn you two things that makes elasticsearch worth it.
Analyzers add some fuzziness to your searches. First, make sure your analyzer is in the right language, this will improve your results. You add the following bit to your model (I typically place it just below where the scopes and validation are defined).
settings index: { number_of_shards: 1 } do
...
I don't like complexity. Adding new items to your stack increases complexity. But sometimes it is worth it. When you need proper search and filtering, elasticsearch is worth it. Mostly because it isn't hard to set up at all, as you'll learn in this post.
Installing it on a Debian server is easy, simply follow their instructions (you'll add their package-server, and run apt-get install. On OS-X you can install it easily with HomeBrew (brew install elasticsearch
), but do make sure you have installed a JDK (e.g. openjdk-8-jre-headless
)
If you're not using something like Docker, you probably have to repeat the steps on your dev machine, your staging server and your production environment.
Note: When running on a low memory server, which isn't recommended for production, you should make sure that the configured heap size isn't too high, edit `/et…
Sorry non-techies, this is really for (ruby-)techies.
For some time I've been working on a ruby gem that helps me on my project work, and may also help other ruby programmers, to work with table imports and exports more easily. Although there are other gems that allow you to read and write to different formats, of which the roo-gem is probably the most well know, I was particularly interested in writing Excel files based on templates.
I wanted to offer my clients more user-friendly Excel files that used some of the more advanced functionality of modern Spreadsheets (AutoFilter, printer styles) that couldn’t be offered by just manipulating styles and formatting using the existing rubygems. Which got me started to think about creating templates to start from, instead of starting from scratch using one of the Gems. Using another Excel file, however, wasn’t as easy as expected and here is where the Workbook gem comes in: to make that easier. Ad…
Al weer 7 jaar geleden liet Robert Dijkgraaf in Zomergasten zien hoe alles weer uit van alles bestaat. Kleine onderdelen die samen weer grotere gehelen vormen die samen weer nog grotere gehelen vormen en zo verder, en terug. Hij illustreerde dit aan de hand van een alweer 30 jaar oud filmpje: powers of 10.
Powers of 10: Van 100 tot 1024 naar 10-14
Op hun eigen schaal lijken alle onderdelen waaruit iets bestaat weer mooie afgeronde dingen. En op een afstand lijken al deze onderdelen ook nog eens perfect samen te hangen.
Ook in ontworpen zaken zie je dergelijke herhaling en samenhang. Een chip lijkt in detail wel een metropool. En de printplaat waarop deze bevestigd zit eveneens. En in een echte metropool bevinden zi…
When you are in the business of creating software you'll have to know that not all software and business models allow for what you've probably done before you were making money in this field, basically copy & pasting & reusing software from all over the web. When you're working professionally you might be confronted with the fact that not all open-source libraries are as free, as in libre (or as in anarchy) as you would like them to be.
(btw I'm not a lawyer, but I do understand language and code)
The famous GPL requires you to license your derivative works with the same GPL as well. Not only your modifications to the library you are using need to be open-sourced, but also the code that you wrote using that other project as a library without which your project wouldn't work. …
Hyper agile development, yeah! Nee, ik ga het niet hebben over ontwikkeling voor embedded systems (systemen zoals pinautomaten tot magnetrons). Maar over ontwikkelen midden in een organisatie die werkt. Zoals journalisten in het leger, op die manier embedded. Niet Kennismaking, Ontwerp, Bouw, Testen, en uiteindelijk de Grote Oplevering (waarbij iedere stap een vertaalslag is zonder de bron om op terug te vallen). En zelfs niet het iteratieve ontwikkelen zoals dat in Scrum gangbaar is. Nee, iedere dag te maken krijgen met verzoeken: “Nu wil ik dit graag kunnen.” “Kun je dit voor mij doen.” En dan beslissingen nemen. Dat is embedded development voor mij.
Hyper agile. Beetje (erg) hectisch, maar daarom des te leuker. M’n code is van tijd tot tijd een rotzooi, in de loze minuutjes proberen op te schonen. En het moet blijven draaien, want ieder moment kan er een nieuw verzo…
Dit artikel van murblog van Maarten Brouwers (murb) is in licentie gegeven volgens een Creative Commons Naamsvermelding 3.0 Nederland licentie .