Who Wants To Learn How To Program?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Who Wants To Learn How To Program?

Post by nathandelane on Wed Nov 24, 2010 5:03 pm
([msg=49385]see Who Wants To Learn How To Program?[/msg])

Hi all, it has been a while because my last company blocked me from getting into hackthissite and all of the proxies I could find were sub-par, so I didn't bother to really try. Anyway it looks like people have been busy reading this thread and I'm glad. I would love to help anybody at any time to learn how to program. Recently my coworker and supervisor indicated that he thought there was a bit of a shift happening in the programming industry on how to teach and learn programming - moving further away from "How to Program" and closer towards "How to Program Correct Programs." The objective in defining what a correct program is is to utilize proven methods of solving problems. Generally these proven problem-solving methods are called design patterns. Most commonly, design patterns are applied to object-oriented problems. However some design patterns are geared specifically toward functional programming (btw those are the two most common programming paradigms: functional and object-oriented...procedural could arguably be a synonym for functional, structured programming is a sub-paradigm of imperative programming - also a programming paradigm. Most programmers use multiple paradigms while programming, because one is not necessarily more effective than another at solving a problem and not all problems lend themselves well to a single programming paradigm.) Here are some examples of design patterns (taken from Wikipedia):

Some Popular Object-Oriented Design Patterns

  • Builder
  • Object Pool
  • Singleton (a special type of Object Pool)
  • Decorator

Some Popular Functional Design Patterns

  • Event
  • Recursion
  • Pipes and Filters
  • Bridge

Many of these design patterns are simply more popular in one paradigm over another. For example Pipes and Filters and Bridge can be used in OOP, while Decorator can be used in functional programming. These are just some examples. While many design patterns can be used in multiple paradigms it may be simpler to implement a design pattern a certain way that involves a specific paradigm over another. Recursion is however an example of a multi-paradigm design pattern that is generally implemented in the same fashion in both paradigms.

Since I have been studying design patterns, I have often asked myself why don't schools teach design patterns first. And I remember that I used to hate programming theory, not understanding the purpose behind learning about how a bunch of really old guys' programming patterns would help me to learn how to program. But now more than ever I understand that programming theory (like design patterns) is a foundation for computer science. It includes proofs, mathematics, discrete math, computer architecture and organization, O-notation, and design patterns. One very simple and basic design pattern, and yet one of the most important and highly used design patterns, is the Interface design pattern. If you are using C# or Java then interfaces are part of the programming language. Recently I dug a little deeper into what interfaces really were.

The simple definition of an interface (generically in computer science) is:
A contract defining that any class that implements an interface must implement all the method definitions given in the interface. (The Code Project - The Interface Construct in C#)


As I said C# and Java define the Interface Pattern explicitly. Here are some example interfaces in C# and Java (they should look very similar):

Given the enumerated type WeaponType:
(Language-independent)
Code: Select all
WeaponType = Firearm | Melee | Missile;


(Java)
Code: Select all
package com.nathandelane.interfaces;

public interface IWeapon
{
   String getName();
   double getWeight();
   WeaponType getWeaponType();
   long getDamageIncurance();
}


(C#)
Code: Select all
public namespace Nathandelane.Interfaces
{
   public interface IWeapon
   {
      string GetName();
      double GetWeight();
      WeaponType GetWeaponType();
      long GetDamageIncurance();
   }
}


(Note that methods of an interface in C# and Java are inherently public and it is a syntax error to include an access modifier in a method declaration in an interface.)

In these languages and some modern dynamic languages like Ruby interfaces are trivial, and simple to implement. Once I realized that, I wondered how one does it in C++. I read a lot of information and learned that at some point Microsoft added a __interface keyword that can be used similarly to the C# and Java variants, but that wasn't exactly what I wanted, so I consulted with Bjarne Stroustrup, the inventor of C++ to see what he said, and I learned that an interface is really just a completely abstract or (in C++) virtual class. The class itself is not what is virtual but all of its members are. They must also be public. Anyway this is the basic result:

Code: Select all
#include <string>

class IWeapon
{
public:
   virtual std::string GetName() = 0; // The '= 0' makes this a pure virtual function.
   virtual double GetWeight() = 0;
   virtual WeaponType GetWeaponType() = 0;
   virtual long GetDamageIncurance() = 0;
};


This pattern is more similar to a abstract class in high-level strongly-typed languages like C# and Java. An abstract class is also, by definition, and interface, however an abstract class may define some methods and leave other methods virtual or abstract. Abstract classes may also define a constructor and fields, though typically the constructor or constructors are protected a little more by giving it protected access. Here are some examples in Java and C#:

(Java)
Code: Select all
package com.nathandelane.interfaces;

abstract class AbstractWeapon
{
   private String name;
   private double weight;
   private WeaponType type;
   private long damageIncurance;

   protected AbstractWeapon(String name, double weight, WeaponType type, long damageIncurance)
   {
      this.name = name;
      this.weight = weight;
      this.type = type;
      this.damageIncurance = damageIncurance;
   }

   abstract String getName(); // Note that these methods can also have a body.
   abstract double getWeight();
   abstract WeaponType getWeaponType();
   abstract long getDamageIncurance();
}


(C#)
Code: Select all
public namespace Nathandelane.Interfaces
{
   public abstract class AbstractWeapon
   {
      private String _name;
      private double _weight;
      private WeaponType _type;
      private long _damageIncurance;

      protected AbstractWeapon(String name, double weight, WeaponType type, long damageIncurance)
      {
         _name = name;
         _weight = weight;
         _type = type;
         _damageIncurance = damageIncurance;
      }

      public virtual String getName(); // Now this method doesn't need to be overridden because it is virtual.
      public abstract double getWeight(); // Note that these methods can also have a body.
      public abstract WeaponType getWeaponType();
      public abstract long getDamageIncurance();
}


So there are a couple of different rules in C#. It has both virtual and abstract keywords. A virtual function and an abstract function are both by definition virtual or abstract, but the virtual keyword makes it so that the implementing class is not required to override the method. An abstract method must be overridden by an implementing class. Also both abstract (and virtual methods in C#) may have a body in an abstract class in C# or Java. Sometimes you will see that the default body throws an exception, such as a new NotImplementedException, in case somebody tries to use the method before it is implemented.

Anyway, besides practicing programming now, I am suggesting more and more understanding why, when, and how to correctly implement design patterns. If anybody wants to pick my brain a little more on design patterns or any of the other ideas I mentioned in this post, feel free to contact me. I should be more available these days.

Thanks.

Nathandelane
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: Who Wants To Learn How To Program?

Post by fabianhjr on Wed Nov 24, 2010 8:48 pm
([msg=49392]see Re: Who Wants To Learn How To Program?[/msg])

Wow, nice post and welcome back. Hadn't seen you since January.
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Who Wants To Learn How To Program?

Post by nathandelane on Mon Nov 29, 2010 11:35 am
([msg=49589]see Re: Who Wants To Learn How To Program?[/msg])

Thanks. That's about right - January was when I started my last job.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests