Programming: OOP help! :(
Hi everyone!
TL;DR: I know two people who are very good at programming. I have been programming for years and have olny just gotten the concept of using OOP, I have made a class with user management functions in, and I have shared it with these two people.
I have been learning programming since 2011 (although I have been doing visual basic since the age of 8 but I never really understood it properly until later by which point I had a few life issues and I stopped learning for a small while until 2010) . I started with Objective C and PHP back when I was 13 and I managed to make apps and websites (badly written mind you). I was given a book on learning Obj C but because I was so excited about learning and doing, I didnt get through all of it at the start, I got to page 150 and stopped right in my tracks and ever since I missed out on things like not knowing why and where I should use return (absolutely MORTIFIED about this) and OOP (although I have always had a basic understanding of OOP but not actually unleashing of what I think its potential is) up until the past year. I feel like I have done a bad job of learning because of this but im not really sure if that rings true or not. Ever since I started I have never really stuck with one thing for too long, ever. I have always been switching from one thing to another. I started with Obj C and PHP and then I moved right along to Java, C#, Lua and Javascript not really giving myself enough time to learn more of the basics of programming. So recently (in the past month) I decided this had to stop. I signed up for Codecadamy and took the full Javascirpt course to 100% with no problems at all and I did learn much more about how to use OOP (which I didnt know before!) and im now half way through a PHP course which Im using just to reinforce OOP.
Now heres the part where Im really confused and worried. Im 17 now, and I really dont wanna feel like I will be stuck in a state where my code will be bad because im a bad programmer. I know one kid a similar age to me and he has so much talent. I know its not a race, I just want to make sure im not heading to a dead end and trying to fix anything Im doing wrong as soon as possible.
Anyway, thats not what Im confused about. What Im confused about is I have asked this kid about my implementation of OOP and he said it was incorrect and that I needed to do it another way (AKA his way, hes very uhh... Over confident?) and so I do. I then go on an IRC and ask somone their opinion on the changes I made and then they said to change it to exactly what I did before as it was a more correct way of working it.
When I ask peoples opinion, Im trying to ask whether or not my core understanding/usage of OOP is correct. Not whether or not Im using their way of implementation as surely, no 2 programmers use the same way of doing OOP... Right?
Im so confused!
Heres a bit of code of my usage of OOP AFTER what that kid a similar age to me said to do:
http://pastebin.com/JxA0xmHG (PS: I know theres a few mistakes in this code, IM in the middle of fixing it up in areas so excuse the errors.)
Now what I started out doing before changing my code is making a class constructer which took a username argument and a true or false argument for whether or not the username provided was a UID or a username (because I have other databases which work from users content by UID and not username) which would then require you to call a getUserInfo function() from your object instance which would in turn, initialise all the variables in the class for user info. He said that the User class should have functions like remove user, add user, etc. I would have made another class for it where I dont use it for fetching other users information. Now that I write that down, I think that Im probably in the right, but I never really think my code is any good particularly, so its hard for me to think whats correct...
Sorry for the very long post!
_________________
?Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.? -Steve Jobs.
[Caution: I'm not a professional programmer.]
I'd say if you have read so much you start becoming unable to understand it all once, just start programming. Write more and more complex programs. If you can write them and they work well, then fine. If it turns out to be an unmaintainable mess and you have to rewrite it, then you learned what not to do and what is good practice instead. Or even if you don't have to rewrite it entirely, you may see what kind of bad practice caused problems, and avoid it in your next program. I don't think there is really a better way to learn it.
There is a bunch of well-known rules in OOP and programming in general. In my opinion most of them are like "you may break it if you know what you are doing", though a few are more or less universal. You can obey them until you understand the reasons behind them, or you can break them and learn them when things go awry.
Specifically for OOP: have you written widget-based GUI applications (like your average KDE or Windows application)? For me that was when I first got an understanding of why OOP makes sense; it is very natural to treat widgets as objects. Since then I used OOP for other purposes as well. Also, some people and programming languages advocate using OOP for everything; I'd rather say use objects when they are the best tool in a situation but procedural programming shouldn't be a taboo either.
About your PHP code: There are several problems with it. Using objects make sense when there is a certain kind of data (such as the data belonging to a specific user), and there are a few operations that can be done on those data. In your case, the code that creates a User object would first call the getUser or getUserByUID functions to get the data of a specific user. From then on all function calls on that object would work on that user. (The class User specifies what kind of data belongs to a user and what operations can be done on a user; an instance of that class, aka an object, actually contains the data of a specific user. Actually it would be a good idea to put the getUser/getUserByUID code in the constructor because that way it is guaranteed to be called before any other function that needs the data members to be already populated with the data of an actual user.)
This means that deleteUser shouldn't have a $username parameter; it should delete the user that is represented by the object, whose name is $this->username. An alternative is not to put it in the object but as a global function that takes a username as a parameter. Yet another alternative is a static function (also called a class method), which does not work on a specific User object and is only put in the User class for grouping.
addUser shouldn't be a function in the User class because it is not an operation on an existing user. It should be a global function or a static function which either doesn't have a return value, or returns a newly created User object representing the new user.
One of the rules I talked about, called encapsulation, is that the data members (properties) of an object, such as $username, $date etc. should be private, and only accessed from outside the object through accessor methods.
I suspect there are also problems with the way you use the Database class, but it's hard to tell without seeing that class. I also can't evaluate the NewPost and Posts objects without knowing what exactly they are supposed to do. In general, I suspect that these things are simple enough that OOP is an overcomplication and they would be more simple to do using simple global functions (i. e. procedural programming). If you want, I may post a version of the User class with proper OOP and a procedural version.
_________________
Maths student. Somewhere between NT and ASD.
Similar Topics | |
---|---|
Grace Hopper - Pioneer of Computer Programming |
24 Oct 2024, 10:47 pm |