Tip of the day: Quickly finding commented out code in C-like languages

This is a quick rough-and-ready way of finding commented out code in languages like C#, C++, javaScript and Java. It won’t find all instances, but it will probably find most.

Essentially, you can use a regular expression to search through the source code looking for a specific pattern.

The following works for Visual Studio for searching C#. The same expression would also likely work in other similar languages where the comment line starts with a double slash and ends in a semi-colon.

So for Visual Studio, press Ctrl+F to bring up the search bar, set the search option to be be “Regular Expression” and set it to look in the “Entire Solution” (or what ever extent you are searching) then enter the regular expression into the search box:

^\s*//.*;\s*$
Visual Studio 2012 Search Bar

Visual Studio 2012 Search Bar

Then tell it to “Find all” and you’ll be presented with a list of lines of code that appear to be commented out.

Caveats

It is not fully proof. It does not find code commented out in the style:

/*
* Commented = out.code.here();
*/

It also does not find commented out code on lines that do not end in a semi-colon such as namespace, class, method declarations and the like, nor on lines that simple contain scope operators (The { and } braces to define the scope of a namespace, class, method, etc.)

And although I’ve said C-like languages in the title, I don’t recall that C ever had the double-slash comment style (although many languages based on it do, hence “C-like”)

Update: Updated the regular expression to ignore white space at the start and end of the line.

Tip of the day: Forcing a file in to your Git repository

Normally, you’ll have a .gitignore file that defines what should not go into a git repository. If you find that there is a file or two that is ignored that you really need in the repository (e.g. a DLL dependency in a BIN folder for some legacy application that doesn’t do NuGet, or other package management) then you need a way to force that into Git.

The command you need is this:

git add --force <filename>

Replace <filename> with the file that you need to force in. This will force stage the file ready for your next commit.

You can also use . in place of a file name to force in an entire directory, or you can use wild cards to determine what is staged.

Afterwards you can use

git status

to see that it has been staged.

Tip of the Day: Make Outlook 2013 display the Weather in Celsius

At work we’re upgrading to Outlook 2013. One of the new features is that the Calendar will display the weather for the next few days.

Outlook 2013 Displaying the Weather in Fahrenheit

If you like this, that’s great. However, most of the world uses Celcius, but out of the box Outlook displays Fahrenheit (regardless of the locale set up on your machine). In fact, it also defaults to New York as the city.

Changing the Weather Location

It is easy enough to change the weather location. Just click on the name of the city and you get a drop down box.

Weather Location Drop Down Box

If the city you want is not in the list, just select “Add Location” and you get a search box to type in the city that you want.

Search for Weather Location

Change the Temperature Scale to Celsius

Changing the temperature scale to Celsius is a little bit more involved. First click the “File” menu in the top right corner of the window.

File Menu Button in Outlook 2013

Then select the “Options” button from the new menu.

File Menu Details

This will display the options dialog. First, click the “Calendar” button in the left menu. Then scroll down to the bottom of the options that are presented. The Weather options are last. You’ll see you can then select the temperature scale (“Celsius” or “Fahrenheit”) that you want. Or you can even turn off the weather too.

Options Dialog

Then you can just “OK” the dialog and the weather will be updated into Celsius. Much more civilised.

Much more civilised, the temperature in Celsius

 

 

Tip of the Day: Getting TFS to remember you each time you open Visual Studio

Because the TFS Server where I work is not on the domain, it will prompt you for credentials each time you log in (unless you’ve previously used the web access and checked the “Remember Me” option). If you don’t want to use the web access portal, you can still get TFS to remember your credentials and not ask you each time you log in.

Go in to the control panel and select “User Accounts”

In the next screen click “Manage Windows Credentials”

In the next screen click “Add Windows Credential”

Then type your details into the form, and press “OK”

You’ll see your new set of credentials appear in the Credential Manager page:

Now when you open up Visual Studio it won’t prompt you for your credentials all the time.

Tip of the day: How to tell why your app couldn’t log on to SQL Server

When you get a log in failure on SQL Server the message you get back from SQL Server Management Studio, or in a .NET Exception is vague for security. They don’t want to give away too much information just in case.

For example, the exception message will be something like “Login failed for user ‘someUser’.” which doesn’t give you much of a clue as to what is actually happening. There could be a multitude of reasons that login failed.

If you want more information about why a log-in failed you can open up the event viewer on the machine that SQL Server is installed on and have a look. You’ll find a more detailed message there.

The wider messages may be things like:

  • “Login failed for user ‘someUser’. Reason: Could not find a login matching the name provided. [CLIENT: <local machine>]”
  • Login failed for user ‘someUser’. Reason: Password did not match that for the login provided. [CLIENT: <local machine>]
  • Login failed for user ‘someUser’. Reason: Failed to open the explicitly specified database. [CLIENT: <local machine>]
    Note: This could be because the database doesn’t exist, or because the user doesn’t have permissions to the database.

Tip of the Day: A DateOnly function in SQL Server

It occurs to me that I’ve probably written several versions of this function in various installations on SQL Sever over the last 10+ years (10 years 2 months, 26 days – to be exact) since I started using SQL Server. I should really put it somewhere that I can refer to it easily and not have to re-write it again. (So why not put it in my blog?)

It’s a simple little thing that takes a DATETIME and returns the same but without the time elements, basically, it returns the date only.

CREATE FUNCTION [dbo].[DateOnly]
(
  @DateTime DATETIME
)
RETURNS DATETIME
AS
BEGIN
  RETURN 
    DATEADD(MILLISECOND, -DATEPART(MILLISECOND, @DateTime),
      DATEADD(SECOND, -DATEPART(SECOND, @DateTime),
        DATEADD(MINUTE, -DATEPART(MINUTE, @DateTime), 
          DATEADD(HOUR, -DATEPART(HOUR, @DateTime), @DateTime))));
END

Tip of the day: Going quickly to an item in the Entity Model Diagram

After a conversation recently about how difficult it was to find stuff in the EDMX diagram because it can often be a right pigs breakfast, I stumbled across this today.

In Visual Studio there is a Model Browser that is available when viewing the diagram. It appears in the same space as the solution explorer. If you don’t see it in the tab list you can add it by going to View–>Other Windows–>Entity Data Model Browser. Like this:

Menu to open Entity Data Model Browser

Menu to open Entity Data Model Browser

Once there, you can open the tree to get the item you want much more easily that finding in on the diagram.  Open entity types to see a list:

The model browser window

The model browser window

Right-click the entity you want to move the diagram to and select “Show in Designer”

Show in Designer

Show in Designer

The designer will shift to the location of the table and put it in the centre of the window for you. It will also select the table.

It may be a really simple thing, but I wish I’d discovered it sooner.

Tip of the day: Setting a default stylesheet for Chrome

I often type up HTML documents in a text editor and view them directly in my browser to review then. However I don’t like the default style, I don’t find it is very readable. It is too plain and dull. What I really want is to provide a style sheet that make the document styled more to my taste, yet I don’t want to have to litter my HTML files with uncessesary mark up linking to stylesheets.

Luckily Chrome has a custom.css file, which is empty by default, which you can use to provide your own CSS that is used when rendering documents and no other style information is available.

On windows this Custom.css file is located at: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default\User StyleSheets\Custom.css

I’ve created my own style sheet, which can be found as a Gist on GitHub: Custom.css.

This is great. My plain HTML documents that I use for writing up blog posts and the like now look a lot better and without me having to put in references to CSS. It also means that the file can contain just the HTML body (no <head> or <html> tags) so I can lazily perform a Ctrl+A, Ctrl+C on the document and then drop it in to my blog.

There is one slight problem with all of this… Depending on how far you go with the styling, a little light play with fonts is okay, but some things like setting borders, margins, colours and widths can mess up existing sites because they don’t expect the base style sheet to contain those elements. But then again, maybe that’s a good thing if you are a front end web developer. It ensures that you don’t make assumptions that may not be valid.

Tip of the day: When other things populate elements on web pages

I work in a web team of 6 developers and we have a diverse range of skills. Some are more skilled dealing with the front end, the UI, and working with HTML, JavaScript and CSS. Others are better with the back end of things, the business logic and dealing with databases. However, there is an overlap.

One thing I noticed earlier this year when we took on our first front end specialist is that she did all sorts of things to optimise the HTML, CSS and JavaScript. It was wonderful, the application worked a lot better and looked a lot nicer. Unfortunately, some of the optimisations broke things because we’d been populating divs and spans from Ajax calls, third party code, or other bits of JavaScript. Because we knew where the data was coming from, having the full view as we did, we’d not bother commenting the HTML (and HTML comments waste bandwidth, right?)

It became obvious that something was going to need to be done if we were going to work with a specialist like this. It occurred to us that the ASP.NET rendering engine allows us to create server side comments that are not transmitted to the client. That helped keep the HTML clean on the client and allowed us to indicate to our specialist that there was a div or span that is needed (and shouldn’t be optimised away)

<div id="sizeSurchargesInfo"><%-- Populated by an AJAX Callback --%></div>

In hindsight, it seems rather obvious to simply comment the code. However, in these enlightened times it does seem unnecessarily frowned upon.

Tip-of-the-day: Columns in CSS

The CSS Multi-column layout module is a Candidate Recommendation that allows CSS to specify various aspects of column layout for page flow. It has some implementations in Chrome and FireFox but it does not work in IE yet. (I’ve not tested it on other browsers). Because of this you have to specify the Webkit and Mozilla extensions in the CSS. e.g.

div.example
{
  column-width: 300px;
  -moz-column-width: 300px;
  -webkit-column-width: 300px;
}

To show you what it can do, I’ve created some small simple examples using a list of cities, a poem and some prose. (The links open in new windows. You are encouraged to look at the page source too)

Without Columns With Columns

Follow

Get every new post delivered to your Inbox.