Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Coding Style
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Jul 14, 2005 7:11 pm   Post subject: Coding Style

Introduction

I'm not going to speak from a podium, because I've certainly been guilty of bad practices both in the distant past and recently. That out of the way, let's look at some bad practices, and what we can do to improve them.

Variable names need meaning

All too often we use abbreviated variable names. Such names make it immensely difficult to discern the meaning of the variable, and why it was created.

This extends to variables as simple as loop counters.

I recently used the classic "get 10 grades" example in another tutorial so I'll use it here.

Bad:

Java:
Bufferedreader kb = new BufferedReader(
   new InputStreamReader(System.in));

int[] grades = new int[10];

for (int i = 0; i < 10; i++)
{
   grades[i] = Integer.parseInt(kb.readLine());
}


Good:

Java:
Bufferedreader keyboardInput = new BufferedReader(
   new InputStreamReader(System.in));

int[] grades = new int[10];

for (int gradeIndex = 0; gradeIndex < 10; gradeIndex++)
{
   grades[gradeIndex] = Integer.parseInt(keyboardInput.readLine());
}


Declare variables only when you need to

Bad:

Java:
Bufferedreader keyboardInput;
String name;

keyboardInput = new BufferedReader(
   new InputStreamReader(System.in));

System.out.print("Your name is? ");
name = kb.readLine();
System.out.println("Hello, " + name);


Good:

Java:
Bufferedreader keyboardInput = new BufferedReader(
   new InputStreamReader(System.in));

System.out.print("Your name is? ");
String name = kb.readLine();
System.out.println("Hello, " + name);


Don't reinvent the wheel if there's an existing library function to do it for you

Bad:

Ruby:
original_string = "hello"

capitalized_string = original_string.sub(/^[a-z]/) do |first_letter|
   first_letter.upcase
end


Good:

Ruby:
original_string = "hello"

capitalized_string = original_string.capitalize


Short code is good code

Bad:

Python:
initial_numbers = range(1, 10)
squared_numbers = []

for initial_number in initial_numbers:
   squared_numbers.append(initial_number ** 2)


Good:

Python:
initial_numbers = range(1, 10)
squared_numbers = map(lambda number: number ** 2, initial_numbers)


Or:

Python:
initial_numbers = range(1, 10)
squared_numbers = [number ** 2 for number in initial_numbers]


Contribute

Feel free to discuss these and suggest other good practices with regard to coding style.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Jul 14, 2005 7:28 pm   Post subject: (No subject)

Another big thing, no matter what your variable naming scheme; don't change it half way though a file! Chose something and stick to it.
Hikaru79




PostPosted: Thu Jul 14, 2005 7:36 pm   Post subject: (No subject)

How about sticking to your particular language's naming conventions? In Java, for example, it is customary to use camel case for fieldAndMethodNamesLikeThisOne, except with a small first letter, using capitalized first letters in class names, and ALL_CAPS_WITH_UNDERSCORES for final variables (constants). It just make it easier to detect on sight what everything is when reading code, and it drives me crazy when someone sends me a file like test.java with methods like Getname().
wtd




PostPosted: Thu Jul 14, 2005 7:40 pm   Post subject: (No subject)

Respect the teeming masses

Many languages have communities with accepted naming conventions. Embrace these naming conventions. For instance, the Java community has very specific style guidelines. Follow them.

Use whitespace

Bad:

ada:
with Ada.Text_IO,Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;

procedure Print_Range is
   Lower_Bound,Upper_Bound:Integer:=0;
begin
   Put("Lower bound: ");
   Get(Lower_Bound);
   Put("Upper bound: ");
   Get(Upper_Bound);
   
   for Number in Lower_Bound..Upper_Bound loop
      Put(Number);New_Line;
   end loop;
end Print_Range;


Good:

ada:
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure Print_Range is
   Lower_Bound, Upper_Bound : Integer := 0;
begin
   Put( "Lower bound: " );
   Get( Lower_Bound );
   Put( "Upper bound: " );
   Get( Upper_Bound );
   
   for Number in Lower_Bound .. Upper_Bound loop
      Put( Number );
      New_Line;
   end loop;
end Print_Range;


Bad:

Ruby:
(1..10).each{|number|puts number}


Good:

Ruby:
(1 .. 10).each { |number| puts number }
wtd




PostPosted: Sat Jul 30, 2005 10:23 pm   Post subject: (No subject)

The Biggest Tip I Can Offer

Don't look on these tips as a burden.

Look at them and see how they'll make your life easier.
[Gandalf]




PostPosted: Mon Aug 01, 2005 5:20 pm   Post subject: (No subject)

To me, having good looking, well syntaxed code makes coding easier and more fun. You don't look at something and say "omg I have to spend a day just to understand that persons coding".

Here's something which bothers me. Even if you don't follow standards, follow your own standard! I really don't like it when people do things like:
c++:
std::cout<<"\nHello,"<< name <<"have a good name today, "<< date <<"\n\n";

Keep the spacing the same!
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: