brunops
-
Are you able to write your code in less lines?
06 Feb 2013Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupéry
Are you able to write your code in less lines? I’m always asking myself this question. I consider it to be a great way to improve coding skills, I mean, yeah right, you’ve got your stuff working, but, are you able to improve it? I’m not saying to sacrifice legibility towards less lines, but instead, to face it as an exercise, to test your own abilities. A way to dig deep into your programming language syntax peculiarities.
As soon as I started programming, I considered less code as better code. I sure changed my mind about that now, as you can see in Get better by challenging yourself, but I still think this is a great way to really understand how statements work and interact with each other.
What are you talking about? Ok, let’s see an example. Consider a C code to loop through a string and print its characters
First our declarations, they won’t change
First attempt
Okay, that’s a great loop, congratz, but… Are you able to write your code in less lines?
So you did a for loop, so cute, isn’t it? Can you write less?
LESS!
They’ll all output the exact same result! My point is, lots of concepts are required to do this piece of unreadable code, like: more than one statement inside a loop condition, pointer arithmetic, operators precedence, and so on!
There are many things you can learn, like:
- an empty, infinite, for loop
- for loops with more than one inicialized varible or increment statement inside it, separated by commas
- nasty pointers arithmetics
- call functions inside functions inside functions.. in a way to shrink 10 lines into one
I’m not saying to ship this ugly code as final, but to practice. Stuff you can understand and that other people won’t, or at least have a hard time figuring it out. Suddenly you’ll find yourself able to shrink codes to a point its legibility is still unaffected, but mostly important, you’ll learn many different ways of doing the same thing, thus, evolving.
And remember: There is more than one way to do it! TIMTOWTDI
» Read full post -
Get better by challenging yourself
23 Jan 2013I consider legibility to be one of the main goals of a code. Maybe your boss won’t agree with me at a first glance but, believe me, he should. Hours and hours of development are spent recklessly only to understand what another simple minded, careless programmer did. And sometimes it’s worse, because this programmer was you a couple of days ago.
» Read full post -
Random quote
27 Dec 2012The seek for perfection without moderation is frustration
» Read full post -
JavaScript and Lookbehinds
27 Dec 2012I’ve been poking around with some redirects that I needed to perform with RewriteRules in Apache configuration. This story deserves its own post, and I hope to post it soon. So, long story short, I had to deal with some complex regular expressions (regexes). The regex started simple, but soon grew bigger when covering other scenarios and trying to keep it in only one expression.
I basically had a couple scenarios that I wanted my regex to match (or not), and needed to keep testing it, as any minor change could compromise the whole test suite. “That’s simple”, I thought, “I’ll create a super JavaScript code that will perform all my tests at once”. It started pretty well, I had to figure out how Apache treats urls and deal with the RewriteConds, but not a major problem. And after that I was able to simulate its behavior and check the results on a <irony>nice, extremely awesome, super stylized HTML table</irony>, really great, right?
Basic HTML for the tests:
JavaScript to Simulate Apache RewriteConds and Perform tests:
First 18 tests and results
At first it was okay, and I managed to perform pretty much all of my tests using it. I know that I could (maybe should) have used a test library for it, like Jasmine, buy hey, it’s cool to reinvent the wheel once in a while, just to get the feeling that you can.
This kind of tests don’t garantee that everything will work once rewriten to Apache rewrite rules, as they were only a simulation. So I still had to test it again on Apache once it was ready. Just to make sure… after all, regular expressions are pretty much the same everywhere, correct?
Then, I continued to perform all my tests, trying out different odd combinations of possibilities. So, I was in the exercise of breaking my code, fixing the regex, breaking the code again, fixing the regex once more, and so on and so forth… Suddenly, I needed to use a lookbehind constructor in my regular expression to cover my tests. This is the point when things start to get weird.
I had my regex set up an ready to roll, I had the great Regular-Expressions.info opened, things looked right, expressions should work, but they simply didn’t. I was trying to figure out why a simple expression as /(?<=a)b/ig.test(“ab”) didn’t match. Didn’t even have a correct syntax!
Then, it hit me. In a glimpse of lucidity, I remembered that once upon a time, when studying a bit of regular expressions, I may have read that some regex flavors do not support everything. So googling a bit, I’ve ended up in the Lookarounds Section. As we can see, our dear friend JavaScript does not support lookbehinds. No one is perfect.
I didn’t want to discard all my cool test suite that I’ve coded with such care. So I start searching for workarounds on how to simulate lookbehinds in JavaScript. I have found some solutions over the web, but nothing really worked well for me. And then, I realized that simulating over a simulation, is not what I call trustworthy.
Time for a different approach. I would have to discard all my code, but that happens. The approach should be something that we really can put some confidence on. I couldn’t find anything really helpfull at first, so I got a bit demotivated..
I forgot it for a while and started browsing random stuff, when I decided to check out the Yeoman project. Reading a bit, I’ve found about PhantomJS, and then, CasperJS. And here is where the story start getting interesting.
And that’s the next subject. Automated unit testing using CasperJS. Hopefully, with less story, and more coding.
» Read full post -
Cool .bashrc File
13 Dec 2012While working in Ubuntu, I feel my bash terminal as my home. Afterall, I spend a big part of my time there, so I need it to be nicely enjoyable.
Here’s my .bashrc with my own configurations. It is a collection of configurations that I’ve found over the interwebz, and the lastest version can be found in my Gist.
It’s a tweaked version of the .bashrc found here.
I added the cowsay with the a fortune as well, so every time I open a new terminal I get a nice quote. Awesome.
How cool is the cow?
In order to work properly, it requires git, fortune and cowsay installed.
It’s easy to notice that most of the colors definition are not used at all, but I like to keep all color definitions, so I’m able to change anything whenever I feel like.
» Read full post