Quick trick about HTML, CSS and Ordered Lists

While coding around some of the requirements in my current project I got challenged by one of them regarding ordered lists, aka <ol></ol>, and the vertical alignment of the ordinals for the list items, aka <li></li>. The requirement was simple, we wanted the ordinals to align vertical in the middle of the <li> content, the problem is that you don’t have much control on ordered lists with HTML and CSS, despite that you can select where to place your ordinal (inside or outside) you can’t not explicit define the vertical alignment and the <li> content can have one or multiple lines.

By default the ordinal is positioned on top like this:

  1. My first line for the first item
    My second line for the first item
  2. My second item

and we wanted to be like this:

  1. My first line for the first item
    My second line for the first item

  2. My second item

Pretty easy and quick, you might already figured out whats the trick behind it, but let me go through it. The position for the ordinal in ordered lists is driven by the contents inside the <li> which is not a good thing in my opinion since you’re changing the way you code and the way you style your content because of other stuff. Anyways the trick rely on two things: the display: inline-block; and vertical-align: middle; and to do that you have to wrap your contents with a <p> or any other container tag that supports this two styles. You might as well have a class for it and forget about the inline style that I just used for demonstration, you can use this code as reference:

<ol class="vertical-aligned-ordinals">
  <li>
    <p>My first line<br/>
    My second line</p>
  </li>
</ol>

.vertical-aligned-ordinals li > p { 
  display: inline-block;
  vertical-align: middle; 
}

The result is that any value you set on vertical-align will drive the position of the ordinal and you should be all set, it’s a pretty useful trick if you have lists with big contents and if you want your lists to have a this look. Thanks to Rafael Magrin @rafaelmagrin who had this insight in one of our pairing sessions.

That’s it, a quick trick and a useful feature.

JavaScript and Testing

So there is a lot of buzz going on around testing JavaScript code and around doing TDD and a lot of people are writing blog pots on how to write tests with a couple of different frameworks like jasmine, jstestdriver, jstest, therefore they’re not really describing the main purpose, the main focus about it nor why, where and when you should write tests for JavaScript, which can lead to a big mistake for the first time tester/reader since adding tests to something that is not going to increase the value of your code is not a good way of writing tests.

JavaScript is on web development for a long time and it has been used for many different purposes, but recently the web development community discovered that JavaScript is becoming part of the core features which any web application must have, especially if the app needs some level of user interaction or user experience. What most people don’t know though, or at least turn a blind eye on it, is that if JavaScript is that important to the app being built, it will sure add some behavior to it and this is bad because I assume that every developer knows, or at least should know that if you have any code that will affect your application behavior you should have tests for it.

That is sadly the most common error I’ve seen in most of the projects that uses JavaScript: developers don’t write tests for something if he or she underestimate the potential change in the app that the code can lead to, e.g. sometimes a developer just say “Ok I’ll just bind this click” or “I’ll just hide this and them show again” and ignore the fact that this simple actions will add some kind of behavior to the flow of the app, the potential changes that this may lead to the app has been underestimated and the code that follow this way of thinking will become lost and not maintainable for a long time until someone finds it and finally discover what it means or just delete because it has no use anymore, not to mention the many hours lost trying to find the code in case it leads to or has been involved in a bug.

So how to tackle that? writing tests for the browser behavior like clicks or showing hiding stuff is annoying since you have to mock the whole page. The solution that me and the folks from our project found very useful is to have objects that will represent the behavior for that specific scenario, it’s kinda of a Model View Presenter / Passive View but a little bit more intrusive on the Model since the model isn’t the actual Domain Model and it’s a middle point between the Domain and the View. Using this pattern we found easy to write tests around the Controller and the JavaScript Model itself and we don’t need to mock the whole page.

One real quick example that I have in mind from one of our (me and Rafael Bandeira @rafb3) discussions about how to write tests for JavaScript is regarding showing images in a carousel widget. The scenario was something like “I have a couple of images on the page and I have to get some of those images based on a business rule and show in the carousel”. Our idea to solve this problem and have tests was to create an object ImagesCollection and populate it with all the images via another object CarouselController and all of it would be implemented with JavaScript. The Images collection itself would have Image objects that the CarouselController would be able to manage and based on the Domain Model it would be able to set it to be visible or not. The CarouselController would be also responsible to load or unload the carousel widget and populate it with the images that were supposed to show there.

By doing it, these are the behaviors that we were able to test:

- The Domain Rules being applied to the Image object to be visible or not. (Image object)

- The creation of the collection containing all the images in the page. (CarouselController responsability)

- The filtering and querying of the images based on a criteria. (ImagesCollection responsability)

- The management of the carousel widget and the collection, which was achievable by quick mocking the ImagesCollection and the widget. (CarouselController responsability)

So yeah, all the behaviors we wanted were tested and will be easy to maintain in the future. There’s a couple o things that need to be mentioned though:

- Since we created an intermediate for the Domain Model, aka. JavaScript Model, we need tests to be sure that this model actually reflect the Domain Model rules otherwise you can end up having models with different rules and kinda lose control of your business.

- Despite in this way you’re going to have tests to your JavaScript it doesn’t mean that the browser will actually behave like your tests want to, this means that even though we wan’t things to be on the screen (like the carousel) it doesn’t mean that it will be there because this depends on other variables like styles, server calls, browser specific issues, etc. and this stuff can be captured with other levels of testing like functional tests or even manual exploratory tests, like said before you can actually write your JavaScript tests to achieve this (mocking the whole page), but in my opinion it’s painful and this pain doesn’t worth the result.

Having these things in mind I hope you’ll be able to write valuable JavaScript tests. Another thing that I wanted with this post is to show that JavaScript testing isn’t just about using one or other tool/framework, despite that I do prefer jasmine. JavaScript testing is (as any other test/language) more about how much do you want your code maintainable and how quick you want to have feedback about your UX code.

By having JavaScript tests in the right place for the correct features you not only increase your code maintainability but also reduce the time in which you receive feedback for the changes you made. It doesn’t mean that if you don’t write JavaScript tests you’re with big problems… it means that by not writing JavaScript code you’re not going to get the feedback for your changes anytime soon than your end user testing. To an agile project this time, the maintainability and the quick feedback is valuable.

Source Control with Mercurial and Visual Studio 2010

This post is a very quick one. I’ll show you how to get your Visual Studio environment ready to work with Mercurial.

First let me talk a little bit about source control, nowadays source control is very common mainly because of  Github whom spread the Git Distributed Version Control all over the internet also people became smarter and realized that by using a source control you can code without the risk of losing or messing up everything and your code files will be organized. So Mercurial is also a Distributed Version Control System and is very similar to Git in many ways, the reason why I prefer Mercurial is because it’s more Windows friendly than Git,  let me rephrase that just to be clear… when I’m on Windows I use Mercurial, on Linux Git.

If you do not use any version control, even for your personal projects I really recommend that you start to use it, you’ll not regret.

Let’s get started with the setup, first of all go to Mercurial Download Page or get the installer here and install it.

That’s it… thank you for your time! (kidding…)

Now we must get our Source Control Extension to Visual Studio, you can get it here HgSccPackage and if you don’t like this one  there is another option Visual HG but be aware that Visual HG requires TortoiseHg and that’s why I don’t use it.

After installing the Extension, you just have to set up your Source Control in Visual Studio, to do that go to Tools > Options… in the Source Control tab select Mercurial Source Control Package.

Also be sure that you have referenced the folder with mercurial client (hg.exe) in your PATH environment variable.

And now you’re set… you can browse any solution on the Solution Explorer and in the context menu (right click) you can see the Mercurial option.

Now have fun with it and tell me how do you feel after that.

If you get excited using it, you can check BitBucket which is a Github like site to create your own remote repositories for free.

Hi

This is my new blog (what ?seriously!?!) … yes it’s, I want to share my knowledge to the internet and also hear what internet want to tell me, so if somehow you ended browsing this site feel free to comment or add your thoughts in here.

Some days ago I was thinking to post something about “open mind” and the result of that was just a discussion with my friend regarding the meaning of this term, so for me to have a open mind is to accept every thing that you see, feel, smell, touch, taste, hear or learn. And because of that the term is misunderstood since you’ll have no fixed opinion about things you only accept it. Instead of being open minded I’ve came up with the term open controlled mind, which means you’ll accept things but sometime you just face it and maintain your own thoughts about it.

When I first came into ThoughtWorks Brazil I have met so many people with different points of view and knowledge that I wanted to be as much as I can a open minded person and I found that being like that I would just surrender to new things and never create a pragmatic thought about it. So I started to think about the things I already knew but never closed my mind to new things. That was and continues to be the most valuable thing that we have @ TW, you can be whatever you like and think whatever you want share this with people and still have a “open mind”.

So sometimes I still think that “open mind” is the correct term, but for now “open controlled mind” is the term that I mostly think is correct. I’ll keep this up and discuss with people about, but as I said if you’re “open controlled mind” you’ll just keep your own thoughts.

I really hope you keep it, because is this what makes human interactions alive and consequently this blog alive.

Thank you and welcome.

Follow

Get every new post delivered to your Inbox.