IRohitable | .NET, SQL Server, Ruby on Rails and everything related to Software

Ruby Notes : 1

by Rohit 24. August 2012 18:17

 

This is the first post of Ruby Notes series. This series and the later, Rails Notes, is a collection of my personal learning notes prepared while learning Ruby and Rails. These notes are the extract of the several books I referred while learning Ruby. These notes were lying on my shelf from quite some time when one bright sunny day I realised to publish them in the hope to help large number of beginners and enthusiasts needing quick reference to relevant topics only.

In this post:

  1. The Ruby style of Coding.
  2. Proper use of Comments.
  3. When to use Parentheses and when to ignore?
  4. How much code in one line? Single-line or multi-line.

THE RUBY STYLE OF CODING

The Ruby style of coding is built on few simple ideas like:

  • The code should be clear and itself tell the reader what exactly it is trying to do.
  • The code should be concise.

To adapt these simple ideas into practice, Ruby stresses on Convention over Configuration from the very start. In Ruby, you indent your code with two spaces per level. The rule is not to use Tabs for indentation.

The Ruby naming convention in short says: Camels for Classes, Snakes everywhere.

This means the Class names should use Camel casing while the rest of the things like methods, arguments and variables should use lower case. For example:

class Customer   
   attr_accessor  :name,  :address,  :place

 
   def initialize(name, address, place)
       @name = name
       @address = address
       @place = place
   end


   def name_length
       puts name.size
   end
end

For constants there is a mixed belief. However, the Ruby community recommends all uppercase with words separated by underscore. For example:

ANNUAL_DISCOUNT = 20

 

PROPER USE OF COMMENTS

Ruby's philosophy says: "Good code speaks for itself". Therefore, use comments only when necessary not just everywhere.

(1) Keep background information separate from instructions:

# Author: 
# Copyright 2012:
#
# An example of Ruby class

 

(2) Not every line requires comment:

number += 1  # Add one to number

 

(3) An example of proper use of comment:

return 0 if divisor == 0  # Avoid division by zero

 

WHEN TO USE PARENTHESES AND WHEN TO IGNORE?

There is no rigid rule but this is what the Ruby community prefers:

(1) Use Parentheses with the arguments. For example:

def increment_number (number)
   number += 1
   puts number
end

(2) Ignore Parentheses in this type of situation:

def name_length()
   puts name.size
end

 Use instead

def name_length
   puts name.size
end

 

HOW MUCH CODE IN ONE LINE? SINGLE-LINE OR MULTI-LINE?

Preferably one code statement per line is recommended for good readability. However, in certain situations you can fold cold block into one if it doesn't compromise readability. Note how this code can be written with folded block of code without compromising readability:

 

Style 1:

5.times do |num|
  puts "The number is #{num}"
end

Style 2:

5.times { |num|  puts "The number is #{num}"  }


Technorati : ,
Del.icio.us : ,

Tags: ,

Ruby on Rails

Exploring the new Visual Studio Express 2012 RC for Web

by Rohit 31. July 2012 00:06

Visual Studio 2012 RC packs many new improvements from the Setup to the looks and the underlying core features. The new Setup is said to be 20% faster based on the customization you choose.

Lot of work has been done on the overall UX to give even more friendlier experience. Let's start exploring the new IDE to see few areas where we frequently spend time:

The New Project screen. An empty ASP.NET Web Application now really means empty. You won't get basic start-up code for your application.

NewProject1.png

NewProject2.png

An Empty Web Application comes with no default Web Page but only Web.Config file. If you want few lines of start-up code, you can always choose a Basic Template:

MvcProjectTemplate.png

The Solution Explorer is one such area where tweaks have been done to make it light and clutter free. Things that don't directly affect the Solution View or the Project Hierarchy have been removed from the Solution Explorer. Enhanced Search has been added to search for any text or item in the Solution Explorer.

SolutionExplorerWebForms.png

The Solution Explorer also contains Active Sync option to easily navigate to the location of the opened document. Click on file in the Solution Explorer and select the Preview option on top of the Solution Explorer. It opens the code of the file for you to preview. Once the Preview icon is selected and marked, you can just keep clicking other files in the Solution Explorer to instantly look the code of the file.

SolutionExplorerButtons.png

Now lets take a look at the default page of a basic Web Froms Application. You will notice a fresh new template to start with:

 

Now open the Mark-Up, i.e., the source of Default.aspx. You will notice improvements in code manageability and readability. Just click on any line of the source to notice a gray border around that line:

 

The new HTML editor offers full support for HTML5 elements and snippets. The CSS editor offers full support for CSS3, including support for CSS hacks and snippets for vendor-specific extensions to CSS.

Html5Support.png

Technorati : ,
Del.icio.us : ,

Tags: ,

Technology | Visual Studio

Enhancements in SQL Server 2012 Management Studio

by Rohit 15. July 2012 12:43

SQL Server 2012 brings some of the most desired enhancements in the Management Studio to simplify manageability.

(1) The new SQL Server Management Studio is built on Visual Studio 2012. As it opens, you soon recognize the familiar dark blue theme of Visual Studio 2010.

ssms1.png

ssms2.png

(2) You can drag and move out Query Window from the main work area:

ssms3.png

It also has dual monitor support. You can move out a Query Window to another monitor or outside the main IDE.

(3) You can even drag out the ToolBox from the main IDE and place it elsewhere on the desktop or on another monitor.

ssms4.png

(4) Being based on Visual Studio, SSMS brings the same Clipboard Buffer feature of Visual Studio 2010. You can cycle through buffer text by holding down the SHIFT key and pressing CTRL+V.

(5) Another interesting feature is Insert Snippet which you can find on menu via Edit -> IntelliSense. It allows you to inject variety of code snippets onto your Query Window:

ssms5.png

ssms6.png

ssms7.png

Above I selected Create Procedure Basic Template to inject stored procedure code. It even provides you with two parameters to start with. You can always remove or add parameters according to your need.

Similarly, you can inject many other type of code snippets like: Function, Index, Table, View etc. There are even more enhancements which are outside the scope of this post.

Technorati : , ,
Del.icio.us : , ,

Tags: , ,

Database | SQL Server | Technology

About Rohit Prakash

Software Craftsman and Technology Enthusiast (not a Guru).

Technical Reviewer of a book on open-source programming IDE.

My day job keeps me engaged with Microsoft Technologies (ASP.NET, C# and SQL Server) hence most of the posts are related to these technologies. I, however, love to play with other open-source technologies. I also have interest in IT Security and therefore you will find posts on Malware Analysis, Cryptography and Anti-Virus programs.

Few people have contacted me for Guest Posts. You will find these posts as well.

 

You can reach me at:

rohit [at] irohitable.com

-----------------------------------------

LinkedIn:

in.linkedin.com/in/rohitpkhare

Twitter:

@BuzzRohit

-----------------------------------------
Google+

Search with this name: Rohit Prakash

Month List

Protected by Copyscape Web Plagiarism Software