Difference between revisions of "Teaching (Computing)"

From Jtkwiki
Jump to navigationJump to search
Line 11: Line 11:
  
 
'''Synopsis:''' Several attributes are stored in several arrays, such that attributes with the same index pertain to the same thing.
 
'''Synopsis:''' Several attributes are stored in several arrays, such that attributes with the same index pertain to the same thing.
 +
 +
'''Problem Example:'''
 +
  String[] name = new String[3];
 +
  String[] address = new String[3];
 +
  name[0] = "John Doe";
 +
  address[0] = "13 Roe Drive";
 +
  name[1] = "Jane Deer";
 +
  address[2] = "17 Caribou Close";
 +
  ...
  
 
'''Problem:''' If it's one (coherent) thing, then it should be represented as one instance.
 
'''Problem:''' If it's one (coherent) thing, then it should be represented as one instance.
  
 
'''Solution:''' Use ''one'' array consisting of instances that each contain ''several'' attributes.
 
'''Solution:''' Use ''one'' array consisting of instances that each contain ''several'' attributes.
 +
 +
'''Correct Example:'''
 +
 +
Write a class:
 +
 +
  class Person
 +
  {
 +
    private String name;
 +
    private String address;
 +
 
 +
    public Person(String name, String address)
 +
    {
 +
      this.name = name;
 +
      this.address = address;
 +
    }
 +
  }
 +
 +
and use that:
 +
 +
  Person[] person = new Person[3];
 +
  person[0] = new Person("John Doe", "13 Roe Drive");
 +
  person[1] = new Person("Jane Deer", "17 Caribou Close");
 +
 +
 +
==== Redundant Representation / De-Normalised Pattern ====
 +
 +
'''Synopsis:''' Multiple instances representing the same entity.
 +
 +
'''Problem:''' The state of the data held in the process can be inconsistent with respect to the problem domain. In database design, normalisation is used to eliminate such redundancy. The same principles can, and should, be applied to the design of data structures used by programs.
 +
 +
Perhaps it seems to some that the more transient nature of variables and instances in a process justifies a more relaxed and less rigorous approach. Normalised data structures may also sometimes appear to be difficult to work with. These arguments only apply in the development of simple software.
 +
 +
Normalised data structures may sometimes limit performance. This can be a good reason for deviating from normalisation. However, this should be based on an informed decision and it should be limited and documented. A generalised concern for performance is not a sufficient reason to justify generalised use of de-normalised data structures.
 +
 +
'''Solution:''' Design code so that each entity is represented by exactly one instance.

Revision as of 14:01, 16 February 2010


Common Programming Problems and Mistakes

Anti-Patterns

The anti-patterns described are quite basic and thus may be of little interest advanced programmers. I list them here mainly because I seem them repeatedly appearing in beginner's coding. Presumably some of these are widely known and have names other than those I use below. You're welcome to point out these in the discussion page.

Parallel Indexes and Arrays

Synopsis: Several attributes are stored in several arrays, such that attributes with the same index pertain to the same thing.

Problem Example:

 String[] name = new String[3];
 String[] address = new String[3];
 name[0] = "John Doe";
 address[0] = "13 Roe Drive";
 name[1] = "Jane Deer";
 address[2] = "17 Caribou Close";
 ...

Problem: If it's one (coherent) thing, then it should be represented as one instance.

Solution: Use one array consisting of instances that each contain several attributes.

Correct Example:

Write a class:

 class Person
 {
   private String name;
   private String address;
 
   public Person(String name, String address)
   {
     this.name = name;
     this.address = address;
   }
 }

and use that:

 Person[] person = new Person[3];
 person[0] = new Person("John Doe", "13 Roe Drive");
 person[1] = new Person("Jane Deer", "17 Caribou Close");


Redundant Representation / De-Normalised Pattern

Synopsis: Multiple instances representing the same entity.

Problem: The state of the data held in the process can be inconsistent with respect to the problem domain. In database design, normalisation is used to eliminate such redundancy. The same principles can, and should, be applied to the design of data structures used by programs.

Perhaps it seems to some that the more transient nature of variables and instances in a process justifies a more relaxed and less rigorous approach. Normalised data structures may also sometimes appear to be difficult to work with. These arguments only apply in the development of simple software.

Normalised data structures may sometimes limit performance. This can be a good reason for deviating from normalisation. However, this should be based on an informed decision and it should be limited and documented. A generalised concern for performance is not a sufficient reason to justify generalised use of de-normalised data structures.

Solution: Design code so that each entity is represented by exactly one instance.