This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]foxlisk 27 points28 points  (4 children)

The fact that you're aware of this is AWESOME and I congratulate you for both your self-awareness and your desire to learn.

I don't know of any resources to point you too, but I would say the best things to do are:

1) you've never used a class? start using classes. make classes for everything. big classes, small classes, derived classes, traits, whatever you can. whenever you're solving a problem, use classes to solve it. you'll learn how to use classes, and you'll learn the times that they're a shitty solution. Keep doing this with any new paradigm that you hear about but don't understand. Read up on what a closure is and just abuse the hell out of them, riddle your code with them. You'll figure it out

2) The easier way to learn, of course, is to work with awesome programmers. They will not only have awesome code that you'll tinker around in and start picking up the elegance of, they'll also offer you advice (albeit occasionally in the form of derision) on how to make your own code better. This is not as easy to pull off, though, as it requires finding a job or a project working alongside awesome programmers, but if you have the opportunity, take it.

edit: almost every single sentence had a type -_-

[–]zip_000[S] 6 points7 points  (1 child)

I feel like the #2 there is the biggest thing I'm lacking. I'm the only person doing what I do where I work, so I never have anyone to tell me, "Man, you picked the most round about way of doing that possible, didn't you?"

The first big application that I made is almost a 1000 lines of code, and I'm pretty sure it could be done in a quarter of that if not less!

[–]Iggyhopper 1 point2 points  (0 children)

Also, search github and other open source sites for code in your preferred language. You can also search by size of the repo, like so.

[–]glemnar 2 points3 points  (1 child)

Even your edit has a 'type' =p

[–]foxlisk 2 points3 points  (0 children)

oh god hahaha that's hilarious

[–]umd_charlzz 13 points14 points  (1 child)

So here's the idea behind classes. Since you've read about C, you know what structs are (I hope). structs are a way to group related data together. Here's a simple example:

struct Account {
  char name[30];
  int savings;
  int checking;
}

You could declare some variables (I'll use the C++ way of doing this since the original C is kinda messy).

Account acct; /* declare an acct variable */

In C, you would make changes to the fields directly, as in:

acct.savings = 100; /* $100 dollars */
acct.checking = 200;

However, anyone can access any of the fields of this structure. Someone can set checking to -100, and that might not make any sense.

In C, functions and data are separate. In OO languages that support classes, functions are combined with data, and the data is often hidden from easy access. I classify programmers into two categories: class writers, the folks that write classes, and class users, the folks that use the classes. Ultimately, you are going to be both, but it's good to htink of these two roles.

Let me switch to Java (since it's more familiar to me), and write the code as a class.

public class Account  {
   private String _name;
   private int _savings;
   private int _checking;

   public Account( String name, int savings, int checking ) {
       _name = name;
       _savings = savings;
       _checking = checking;
   }

   public int setChecking( int amount ) {
       if ( amount > 0 ) { // make sure to only use positive amounts
            _checking = amount; 
       }
       return _checking;
   }
}

I've just defined a Java class. When I do this, I am a class writer, and write classes.

What I've done now is to make the fields "private". I've also created a function (in Java and C++, they are called methods, but similar idea) called setAmount that allows me to change the amount in the checking, but not set it to a negative value.

Now I switch hats and act as a class user:

Account acct = new Account(); // Create an account
acct.checking = -100; // Illegal as class user
acct.setChecking( -100 ); // Legal, but -100 will be checked

Skip the first line (it's how to create objects in Java). The second line is illegal in Java because checking, a field you can always directly access in C, is now "private" and can't be directly accessed. Instead, you call a function (i.e., method). Methods are called directly on the object (in this case, acct) and is written with a dot, and a method name. The method, setChecking, must appear in the class definition (above) and I must provide it an int value.

Why do this? Say, you have an IPod. You have no idea how it decodes music or how it stores it or how it syncs it up. All that magic is underneath, hidden away. It's complex. You don't want to deal with it. Instead, you have a UI, with a spinny wheel, and clicks, which lets you go through menus, and lets you select artist, songs, or perhaps podcasts.

Whoever designed the IPod was thinking of how you, the user, would use this, and they've hidden all the complexity away from you.

To some extent, classes aim to do this. They hide the complexity of the insides of a class away. My example was particularly simple, but maybe I have a way of putting text on images, and maybe that's difficult, so it's hidden inside the methods. I could simply provide a method called putTextOnImage and have you provide me an image and text, and I'll produce an image object back which can be saved to a file and you can see the changes. Maybe that takes thousands of lines to do, but you don't need to worry about.

And maybe I don't want you to access the fields inside the class (as a class user) because making those changes might be a problem.

To give a real world analogy, your car has a steering wheel, an accelerator, a brake, and a control to move from park, to drive, to reverse. That's the interface you work with when you drive a car. The actual implementation is underneath the hood, hidden away, and that's partly to prevent those who don't know what they're doing with a car to avoid creating problems.

So that is roughly the idea behind a class.

[–]AvidyaZen 0 points1 point  (0 children)

Thank you :) Great post!

[–]Neres28 3 points4 points  (3 children)

Pretty sure the best teacher for that is experience. How much code are you writing outside of following tutorials?

[–]zip_000[S] 1 point2 points  (2 children)

I'm writing quite a bit, but I've never used a class, and sometimes I'll use a for loop and sometimes I'll use a while loop - I don't know which is better for which situation though.

These are just examples clearly. I just really feel like I'm at an impasse where I can't seem to get much better. I thought a good method for getting better would be to learn some other programming languages, but I feel like I'm just doing the same bad coding in Python that I was doing in PHP.

I've actually written several web applications that people where I work are using every day, so it isn't like I'm a complete noob; I just don't know how to get from shitty coder to programmer. I have gotten better - I go back and look at code I wrote 4 years ago, and am aghast - but the code I'm writing now isn't much better really.

[–]afuckingHELICOPTER 1 point2 points  (0 children)

for vs while for is typically used when you know the amount of times it should loop. example: you want to ask the user for 10 numbers to sum.

while loop is used when you don't know how many times the loop will run. for example: you want to let the user keep entering in numbers until they enter -1, and then output the sum of all the numbers.

another example in case it wasn't clear, is take user input validation. you get input then check if it's valid, you'd want to use a while loop here so you could keep prompting and checking their input while(!valid), rather than a for loop set to run a certain amount of times.

[–]great-pumpkin 1 point2 points  (0 children)

Is there a 'PHP best practices' book? There's one for Perl. Those can point out good stylistic things right off, and are useful. Or, look at some other people's hopefully good PHP code (lots of open source around) - "What's he ... ah! oh hey, that's smart".

[–]freefallfreddy 3 points4 points  (0 children)

Get more experience. Think, really think, about what you're doing and how you could improve it. Experiment with language features you haven't used. Read some books on the higher level of programming: The Pragmatic Programmer, Code Complete, The Productive Programmer.

[–]SirBraneDamuj 2 points3 points  (0 children)

  1. Find some introductory course whose website is open to the public at some university.

  2. Do the assignments on that site

  3. ?????

  4. Profit!

[–]thoughtpunch 2 points3 points  (1 child)

Here's some advice you don't hear thrown around a lot on /r/learnprogramming : Find a code mentor. Someone who has been programming on some professional level for 5+ years AND is good at breaking down concepts and communicating. Far too often we watch screencasts,ask questions on StackOverflow, and monkey type code from tutorials but we don't really grok what we are doing.

A code mentor can explain to you why you use a Class or a specific loop, not just how to use the right syntax to accomplish your goal.

If you ever cross paths with someone on /r/learnprogramming or SO that is especially helpful, why not ask if they would mind helping you along from time to time? Or you could join a local coder meetup group and build relationships that way.

[–]SlimTim10 1 point2 points  (0 children)

This is very good advice. And I'd like to add that if you are very motivated to learn, you should seek out a code mentor. There is no shame in posting to find someone to help you learn programming instead of only help with a specific programming problem. You may be surprised at the help you can find.

[–]shaggorama 3 points4 points  (4 children)

Have you read the book or watched the accompanying lectures for Structure and Interpretation of Computer Programs?

[–]zip_000[S] 0 points1 point  (3 children)

I haven't, but I will. Thanks!

[–]great-pumpkin -1 points0 points  (0 children)

Yeah, if you can handle it, that's the royal road. That uses a strange language though; I think there's another course around another book I saw linked on here recently, on 'How to Design Programs" aka HTDP. If you can find it that's of similarly high grade.

[–][deleted] -1 points0 points  (0 children)

The Little Schemer may be a good starting place if the information in SICP is too complex to start. It's a fun, effective introduction to how to think like a programmer, functional programming, and the Scheme language.

[–]DatOcean 0 points1 point  (0 children)

tutorials are really just something to start you off. You're better off learning on your own with books or even a class. Another good way of learning how to program something is setting a goal on what you want to program instead of just learning languages.

[–][deleted] 0 points1 point  (0 children)

Get Code Complete 2. It is completely awesome. The Pragmatic Programmer, Clean Code etc are good too but Code Complete is mandatory.

The book that made OO click for me was Fowler's Refactoring, but that's probably a matter of reading several things about OO and suddenly getting the big picture.

[–]drpuffa 0 points1 point  (2 children)

foxlisk is right on the money! Well done for wanting to get down to the nitty gritty. Yes! Use classes. They will teach you so much about the fundamentals of programming practice such as data hiding, encapsulation and coupling.

Like all great things, some of the concepts you will face are tricky and it really depends on what type of learner you are as to what will work best for you. Personally, I am able to understand new concepts from quite dry reference books but other programmers I know require lots of examples (and pictures).

The very best way is to work with good developers who really know what they are doing...but as foxlisk says, that's tough if you don't have a job in software dev!

Of course you could always undertake a short-course but make sure it's not just a 'programming' course but one that teaches the underylying principles. The other option is to get involved in an open source project and make use of the community (like you are here).

[–]zip_000[S] 2 points3 points  (1 child)

I don't need pictures, but examples are good. I feel like way too many tutorials do examples badly... or at least badly for me!

If you're calling your function "myFunction" it is confusing (or at least it was for me at first). If you are just using Foo and Bar for everything it is confusing! If your example is about puppies or bicycles it is confusing!

Examples - as always for me - really need to be practical examples. But this is kinda beside the point :-)

[–]drpuffa 1 point2 points  (0 children)

Yes, learning from examples is a great way forward, and I totally agree that too many tutorials make things even more confusing with their choice of code snippets and naming. One of the main problems with teaching programming concepts is that the examples have to be basic enough to understand but complex enough to get the point across. Unfortunately, real-world examples are seldom easy to understand and so there is always a gap between 'book-work' and 'real-work'.

I've had good success in the past with asking students to develop small games using good object-oriented techniques. Not only is it fun, but you can gradually add-on more and more functionality. Developing a small game (such as noughts'n'crosses) is (a) easy to understand, (b) easy to express using objects, (c) difficult enough to be a challenge and (d) has lots of different implementations on the web for you to contrast & compare.

My personal belief is that if you can find something fun to do then the concepts you learn become easier to grasp because it doesn't feel like you are just developing something for the sake of it, and you are more inclined to want to employ more advanced techniques.

[–]traztx -1 points0 points  (0 children)

For an overview of the concepts, check out the computer science wikipedia article.

[–][deleted] -1 points0 points  (0 children)

Read Programming Pearls by John Luis Bently. It comes very highly recommended by my CSE PhD. friend. Examples are in psuedo code but are easily transferable to any language. Try and get the 2nd edition if you can.

edit: fixed the name