
-----------------------------------
wtd
Thu Jul 14, 2005 7:11 pm

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:

Bufferedreader kb = new BufferedReader(
   new InputStreamReader(System.in));

int

Good:

Bufferedreader keyboardInput = new BufferedReader(
   new InputStreamReader(System.in));

int

Declare variables only when you need to

Bad:

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:

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:

original_string = "hello"

capitalized_string = original_string.sub(/^

Good:

original_string = "hello"

capitalized_string = original_string.capitalize

Short code is good code

Bad:

initial_numbers = range(1, 10)
squared_numbers = 

Good:

initial_numbers = range(1, 10)
squared_numbers = map(lambda number: number ** 2, initial_numbers)

Or:

initial_numbers = range(1, 10)
squared_numbers = 

Contribute

Feel free to discuss these and suggest other good practices with regard to coding style.

-----------------------------------
md
Thu Jul 14, 2005 7:28 pm


-----------------------------------
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
Thu Jul 14, 2005 7:36 pm


-----------------------------------
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
Thu Jul 14, 2005 7:40 pm


-----------------------------------
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:

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:

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:

(1..10).each{|number|puts number}

Good:

(1 .. 10).each { |number| puts number }

-----------------------------------
wtd
Sat Jul 30, 2005 10:23 pm


-----------------------------------
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]
Mon Aug 01, 2005 5:20 pm


-----------------------------------
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:
std::cout