Similarly, in C/C++, int* i makes much more sense than int *i, as you would read it as an integer pointer. If in doubt, refer to your style guide (most companies or managed code bases typically have one somewhere).
Ahh, that one. On our first lecture on pointers in our C class, our lecturer completely failed to grasp how putting the pointer in front of the name could be confusing, even when I pointed out to him that...
Code:
int *x = y;
...and...
Code:
int *x;
*x = y;
...do two completely different things.
Myself, I used to write as you do before I (privately) found out how...
Code:
int* x, y;
...will declare y as an int, and not a pointer to an int. Now I just refuse to write the star together with anything in variable declarations. After all, I wouldn't do it with const, so why do it with *?
Quote:
Originally Posted by Neftren
Also, for the last code example, Bubble Sort (and similar O(n2) algorithms)...? Why do you consider this useful?
Yeah, and especially when a better sorting algorithm (QuickSort) already can be found in the Arrays library. I mean, you could just write Arrays.sort(array), where array is the array you wish to sort, and you'd be done.
__________________
Engineer Teddy by me. We're refitting this steam locomotive in the spirit of Steampunk, but we need your help designing it: Enter_the_design_competition_today!
(And yes, my avatar is according to scale.)
[*]Where are you and what are you doing in terms of programming? Employment, education or in your spare time?
Early on in learning my programming. Looking for employment, but when I'm not, I'm trying to learn programming. At the end of the day, I might be able to make some money out of it, so why not learn it, right?
Quote:
[*]What language or languages are you using primarily at the moment? What's your favourite, and why?
Well, only learning one right now. That's Python. Technically fiddling with the sub-module of Pygame (as a crutch). Overall, the language is pretty straightforward... I've looked at C++ (or C+) and it gave me a headache really quick.
Quote:
[*]Where do you hope to go with your programming? Any plans for the future, or just waiting to see if anything of interest comes along?
Ideally? Full-time career as a programmer. My dad did it before me, so did my brother. And while some forms give me a headache, it's pretty cool what you can do with programming.
Quote:
[*]Any projects, personal or otherwise, you're in the middle of? Anything you're really focused on?
Umm... mostly just learning the language. Within the pygame module, I'm trying to learn to make a physics engine of some kind, but boy is that getting me stuck.
Quote:
[*]What environment do you work/prefer to work in? Netbeans/Eclipse/Command line and a text editor?
Standard IDLE for Python (if I'm right in what you mean). Debated trying others, but I don't want to get into complicated things honestly.
Quote:
[*]Any problems that are driving you mad? A piece of code not compiling right, or being off by one, or one little syntax error in a sea of code? Not quite grasping the logic of a particular piece of code?
Just that I have absolutely no clue where to start with a Physics engine. I've grasped basic python, and basic Pygame, but there are like... 0 Tutorials for Python 3.2 on it. So I'm stumbling blindly, and it's VERY time consuming.
__________________ Click above to watch my Let's Play of Dead Space! Episode 4 is out!(UPDATED ON: May 14, 2013)
NOTE: I am not interested in watching MLP: FiM. I would appreciate it if others would not try to convert me. I've had enough RL friends try to do that. Thank you. Avatar: Thanks to asdflove for my avatar. ^_^
Myself, I used to write as you do before I (privately) found out how...
Code:
int* x, y;
...will declare y as an int, and not a pointer to an int. Now I just refuse to write the star together with anything in variable declarations. After all, I wouldn't do it with const, so why use it with *?
Ahh, yes, the precedence bit. I typically avoid declaring multiple variables on one line, so I don't really have that problem. To each their own I suppose.
Const is just one of those annoying keywords where I don't think anyone can really agree on what to do. I think most people prefer placing const before the type declaration, though off the top of my head, const should always be placed in such a way so as to modify the type to its immediate left (int const). I think this breaks on older compilers though. I haven't really tested it rigorously.
Quote:
Umm... mostly just learning the language. Within the pygame module, I'm trying to learn to make a physics engine of some kind, but boy is that getting me stuck.
Pygame isn't really designed to handle complex physics. The most I've ever gotten it to do is simple mechanics (e.g. handling kinematic equations, force application, etc.). You're not going to be able to get it to do anything in 3D or handle shadows/shaders/cloth simulation, at least, not with any sort of reasonable execution speed. Maybe if you hooked it into PyOpenCL or something to handle computations, but...
Quote:
Just that I have absolutely no clue where to start with a Physics engine. I've grasped basic python, and basic Pygame, but there are like... 0 Tutorials for Python 3.2 on it. So I'm stumbling blindly, and it's VERY time consuming.
I don't think the Pygame implementation for Python 3 is finished yet. At least, it wasn't last I checked. Most of the syntax between Pygame on 2.7 vs. 3.2 should be the same. If you're dead set on starting with a physics engine... you'll probably want to put together a calculations package containing the basic kinematic equations. As an aside, you'll have to tweak the numbers a bit to get the right feel (e.g. increasing gravity while falling, decreasing while jumping, to get that Mario feel).
Do you have some sort of goal, other than "I want to build a physics engine"?
Ahh, yes, the precedence bit. I typically avoid declaring multiple variables on one line, so I don't really have that problem. To each their own I suppose.
Yeah, when it comes to pointers, I usually do as well, but I felt like being prepared for when I want to do it one line.
Quote:
Originally Posted by Neftren
Const is just one of those annoying keywords where I don't think anyone can really agree on what to do. I think most people prefer placing const before the type declaration, though off the top of my head, const should always be placed in such a way so as to modify the type to its immediate left (int const). I think this breaks on older compilers though. I haven't really tested it rigorously.
Yes, that seems about right, even though const int works as well. The unholy combination of const and pointers really messed up one of our lectures for a while. Our lecturer (a replacement that day, so it's kind of understandable) wrote the pointer stars together with the variable name rather than with the type (or stand-alone, for that matter), which meant that he couldn't figure out where to put the const keyword when trying to declare a constant pointer to a variable value. Since I by then had learned about how pointer stars acted in multiple declarations on a single line, I could figure out how to do it (by placing the const after the star) and make the example work.
It also annoys me slightly how both const and (especially) * are read from right to left. the utterly brilliant declaration char * * const * const x (auto-generated example taken from this site) gives you a constant pointer to a constant pointer to a pointer to a char. When I first saw it myself earlier today, I failed to grasp why the const keywords affected the pointers in that order, before I realised how it reads them right to left. Simple when you know it, but hard to realise on your own...
__________________
Engineer Teddy by me. We're refitting this steam locomotive in the spirit of Steampunk, but we need your help designing it: Enter_the_design_competition_today!
(And yes, my avatar is according to scale.)
Okay, I wasn't mixing up two things at once, I was mixing up three.
Having a database of my own useful methods/classes/etc for my personal use.
Having something to show other people(that have started the course we're doing from a complete beginners viewpoint and thus have difficulty understanding a lot of the logic and syntax, whereas I have a year of experience in computer applications) in my course if they're having a problem.
A collection of slippery/tricky little aspects of coding in java that would be on the OCJA exam, such as what happens when you use ++ in an if statement, or a if(boolean = true)(this is assigning) as opposed to if(boolean == true)(this is checking), or a comment with \\(wrong) as opposed to //(right)
Unfortunately, while that may be what I'm asking for, it's completely beyond my level of comprehension at the moment; I'm barely understand any of it or how to utilize it.
Quote:
Originally Posted by Neftren
What I'm saying is that you shouldn't copy/paste at all. If it's a code snippet, it's short enough to retype, or regex/text-expand it. Typing it out will help you avoid mistakes (enter the logical fallacy of code found on the internet). If it's some sort of utility method (e.g. templated matrix multiply) that you use on a frequent basis, it should be documented and then compiled into a package.
Well, the thing is, I have found that there occasionally methods that I would want to re-use, that I know they would fit perfectly in this new situation. It could be quite a large method, or several methods, but I would know that what I expect to put in and get out is the exact same as the previous code. Not frequent enough to warrant making a package(though that is quite a good idea; that shall be a task for another day), but large enough that re-typing it out when I know what it does and that it works in the circumstance is just redundant work.
Quote:
Originally Posted by Neftren
Code that you use personally and code that you show other people shouldn't be any different. If you maintain a rigorous and neat programming style, your code should be easily understandable to anyone reading it. At that point, it's just a matter of "oh, here's my file, look at it and understand!"
I clarified this up at the top a bit; this isn't code for people at my skill level or above, but rather to help explain the code, how it works, why it does what it does and such for other people learning java for the first time in the course who might be struggling to grasp it.
I'd like to think of my code as quite neat and readable, and I already threaten apocalypse via armoured attack-weasels on people who don't indent/comment/do general neaty-ness things.
Quote:
Originally Posted by Neftren
As for some comments on your code... I know a lot of instructors provide cheat sheets on exams. I don't know if yours does the same though. Anyways ...
Well, our exams so far are snippets of code that are designed to be as sneaky, evil, and underhanded as possible, i.e. no-where near what good practical coding should ever look like.
Quote:
Originally Posted by Neftren
You should probably avoid performing mathematical operations within conditional statements. Your first code sample will fail if the given number isn't 5. Granted you probably already knew that part.
This is more to prove the kind of questions we get, i.e. what will this snippet of code output? In this case, just "Hello.", because of pre and post incrementing, and the fact that using ++ or -- in the if parameter still increments/decrements for outside the if statement, regardless of whether or not the if statement proves true or not. Unless I'm mistaking something here, and am just ending up with egg on my face.
Quote:
Originally Posted by Neftren
I'm surprised you (or other people you're helping) found int []y[]; tricky. Well, maybe it's the format you wrote it in. This is more of a pet peeve of mine more than anything else (going back to the Star (Pointer) Wars. It makes much more sense to rewrite that fragment as int[] y[]; (note the spacing). That way, you keep the data type with the container. An integer[array] (of) y[array_size]. Similarly, in C/C++, int* i makes much more sense than int *i, as you would read it as an integer pointer. If in doubt, refer to your style guide (most companies or managed code bases typically have one somewhere).
It's more that I (and as far as I know, everyone else in the course) have always used int x[][];, and we would be asked questions like "Will int []x[]; compile?" in exams, when we've never seen an array intialised like that before.
Quote:
Originally Posted by Neftren
Also, for the last code example, Bubble Sort (and similar O(n2) algorithms)...? Why do you consider this useful?
In of itself, it's not that useful, apart from the fact that I worked out the logic of the code myself(and I am damn proud of that! Stupid thing wrecked my brain for a while.). However, I can show this to other people to explain how sorting an array works.
Another benefit is that I can alter this. For example, say I have a Card object that has a int value(is it a 5, 6, Ace, King, etc). I can copy this code, and change it to take in an Array of Cards, and use getValue to compare the values. In fairness, I could also have a method to take the values from an Array of Cards, and write that to an Array of ints called values, but either way is valid from what I can see, and having an extra alternative is never a a bad thing.
Quote:
Originally Posted by Neftren
A tagging system in what? All modern operating systems can sort by file name and date. Or are you using some IDE?
Netbeans. I have my projects named YYYY/MM/DD/NAME so I can at least search by date. Also, is it possible to add search tags(similar to those for images) to folders/files, for things like, say Has2DArrays, ExampleOnMethods, Assignment, etc? I know what you mean by searching by date created/size/name/etc, but I mean to search for programs that come back with the following tags, and such.
Quote:
Originally Posted by Neftren
Compiling via command line is pretty easy. You'll probably learn to use the GNU Compiler (gcc/g++). In Terminal (or shell of choice), you'll enter something along the lines of...
Funny enough, I started learning using java, so I'm quite happy with that environment. The only issue I have is that I don't know how to access methods from another class in one class, and I used a class that handled all input from the user that a lecturer from college gave us. I'm happy enough using netbeans for the moment though, and our next teacher is going to go through compiling via command line further down the line, so I'll be happy to deal with that issue then.
So, coming a little late to the party, but here is a little about me:
I work in the Engineering group at my job, but my duties are mostly related to release management, so as my direct duties I deal mainly with things like Ant and Ivy. For my background I have a BS in Computer Science, and I did a little bit of graduate work(of which I'd like to do more). Most of programming is currently of the hobby variety, though I'd love to do more of it as my actual job. I've been employed for the same company for 10 years, but only worked in the engineering group for 6 of them. Did a little QA, then a little bit of bug fixing, then moved into more of a support role, leading to my being part of release management during a merger.
I've used a lot of languages for different purposes. C/C++ was the language my school used for teaching, and it's in place in a fair amount of our projects at work. I've done Java and JSP. I've scripted in Perl, PHP, awk, and Python. For school, I've done Prolog and Lisp(Lisp is fun). I mucked around in C# a little as well for an opensource project. My current favorite and what I use for home projects is Python. It's just kind of fun. It's fairly straight forward, but has a lot of power and versatility.
I have a couple of goals. I'd either like to focus on programming more as a profession than I currently do, or I'd like to become a professor and teach. I'd also like to continue my graduate studies. My interest lay in the realm of AI and specifically Natural Language Processing. Given the opportunity, I'd probably do some studies in Linguistics as well.
I have a couple of projects that I work on from time to time, but nothing serious.
I usually use eclipse when I'm working with Python, at work, I'll either use vi or jedit for quick changes/merge conflict resolution.
__________________
CEO of Evil Incorporated: "Subjugating humanity for a better tomorrow."
Unfortunately, while that may be what I'm asking for, it's completely beyond my level of comprehension at the moment; I'm barely understand any of it or how to utilize it.
It`s not easy to learn from it, but it`s a very good refrence tool if you already know about the things you want to use, just not remmember how exactly ("This class has a method to preform that thing I want to be done... but how is it called?").
Quote:
Originally Posted by Miscast_Mage
Well, the thing is, I have found that there occasionally methods that I would want to re-use, that I know they would fit perfectly in this new situation. It could be quite a large method, or several methods, but I would know that what I expect to put in and get out is the exact same as the previous code. Not frequent enough to warrant making a package(though that is quite a good idea; that shall be a task for another day), but large enough that re-typing it out when I know what it does and that it works in the circumstance is just redundant work.
A package is just a bunch of classes bundled togather. One of the core packages comes with two classes and one enum (java.lang.math), it doesn`t have to be big to be part of a package.
Quote:
Originally Posted by Miscast_Mage
This is more to prove the kind of questions we get, i.e. what will this snippet of code output? In this case, just "Hello.", because of pre and post incrementing, and the fact that using ++ or -- in the if parameter still increments/decrements for outside the if statement, regardless of whether or not the if statement proves true or not. Unless I'm mistaking something here, and am just ending up with egg on my face.
I have a confession: I don`t know the diffrence between using ++ before a number and after it.
I looked at it, I got confused, I decided to never use it. I could get the same results without dealing with this.
Quote:
Originally Posted by Miscast_Mage
Netbeans. I have my projects named YYYY/MM/DD/NAME so I can at least search by date. Also, is it possible to add search tags(similar to those for images) to folders/files, for things like, say Has2DArrays, ExampleOnMethods, Assignment, etc? I know what you mean by searching by date created/size/name/etc, but I mean to search for programs that come back with the following tags, and such.
You can have a file with a realy long name, that in it`s title it would contain all this date, inside the folder.
Or another, single file that contains data on all the projects, but you would have to update it frequently.
Quote:
Originally Posted by Miscast_Mage
Funny enough, I started learning using java, so I'm quite happy with that environment. The only issue I have is that I don't know how to access methods from another class in one class
I have a confession: I don`t know the diffrence between using ++ before a number and after it.
I looked at it, I got confused, I decided to never use it. I could get the same results without dealing with this.
++<num> increments it before it evaluates the value, <num>++ increments after.
Code:
val = 2
print ++val // 3
print val++ // 3
print val // 4
__________________
CEO of Evil Incorporated: "Subjugating humanity for a better tomorrow."
I have a function to read the file a line at a time and split each line into a list.
Quote:
def main_list(fname="grades.txt"):
import locale
fin = open(fname,mode='rt' buffering=1, newline='\n', encoding=locale.getpreferrencoding())
lst=[]
for line in fin:
lst.append(fin.readline().split(','))
fin.close()
return lst
Earlier attempts didn't have the buffering and newline character and encoding for the open call.
it appears to be creating the list I want, a list with each entry is a list of strings. However it seems to only be reading every other line of the file.
it appears to be creating the list I want, a list with each entry is a list of strings. However it seems to only be reading every other line of the file.
The problem here is the for loop syntax. When you say "for line in fin," you're iterating through fin's file and returning the lines one at a time as line...and then in the body of the loop you're telling it to readline() to get a line again, so you're throwing away every other line.
Just change the body of the loop to "lst.append(line.split(','))" and that should fix it.
__________________
Better to DM in Baator than play in Celestia
You can just call me Dice; that's how I roll.
Sig of Holding
Spoiler
Quote:
Originally Posted by abadguy
Darn you PoDL for making me care about a bunch of NPC Commoners!
So I started on a re-code of my previous attempt of a blogging platform on Google App Engine. So far so good. I've managed to get adding/editing posts working properly, and I've integrated Twitter Bootstrap, the Bootstrap-WYSIHTML5 plugin, and the Google-Code-Prettify plugin. Everything is saved/retrieved dynamically from the datastore and rendered using the Jinja2 templating engine.
I think my next step is to add RSS/Atom support, but I'm finding it rather difficult to actually put that together. Does anyone here have experience with writing xml feeds?
So, working in Java, and trying to figure out combining inner/outer classes with using an abstract class.
Say I've got an abstract class, Person, which extends to two classes, Teacher and Student.
I want each to have an inner class, Stats, but stats is slightly different for either of them(say their difference is Student has marks, Teacher has wages);
Would having Stats as an abstract class, and then having a Stats for Teacher and a Stats for Student work here? Would I have the abstract class Stats inside the abstract class Person as well?
Would it be a case of Teacher extends Person, then inside Teacher, TeachStats extends Stats?
Edit: I should probably clarify, when I say "Stats for Student", I mean "Stats for StudentStats extends Stats". Sorry if that caused any confusion.
Spoiler
Note that variables could mean methods as well; I was just lazy drawing up this diagram.
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
Last edited by Miscast_Mage : 11-01-2012 at 08:38 AM.
I don`t see why you want to use an inner class at all. You could create a regular class/interface named Stats, and just have a reference to it. In Student and Teacher it`s use is completly diffrent.
Anyways, you could have the Stats object contain an int value.
You could have a getMarks() method in Student, and getWage() method in Teacher, both returning that int value.
Is it completly neccesery for you to use inner classes for some reason? (if so, remmember not to make the inner class private)
Well okay, if I don't have to have it as an inner class, how would I implement it? Would I just have Stats, StudentStats and TeacherStats as seperate classes, and just have in, say, Student:
Spoiler
Code:
public class Student extends Person
{
private String whatever;
private StudentStats studentStats;
}
where StudentStats is a completely seperate class? This is what I was thinking originally, but then figured that if I want to add a few more classes(which is what I'm planning on doing; this is just testing for another project that have a lot more classes extending Person), it might be more organised if I had the Stats(which also changes depending on what class it's going with) inside the Student/Teacher classes.
Having the stats object just as it is, and having the getMarks/getWages be the only difference might work; I'll have to look into that. The only problem is I'm only going through this project so I can understand the logic of it and apply it to a larger project. I'll see about about getting precise details on what I want this project to do later; I've been studying the last two and half hours and my brain is starting to melt, so apologies if I'm mixing things up or being incomprehensible.
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
I`m thinking more of:
public class Person { protected Stats stats; // so it would be inherited
}
public class Student {
stats = new StudentStats();
}
or otherwise make sure it would be StudentStats.
The field that will be returned by getWage()/getMarks() doesn`t have to be inherited, but I do strongly suggest that all values would be returned from a method inherited from the class Person.
I'm not sure why you need a separate class for those stats at all. I thought the whole point of abstract classes were that you put the basic details of the type in there, and then got more detailed with the derived classes? So, Wages is a particular way in which the Teacher class extends the basic Person one. I might be thinking of this too much from a C++ viewpoint, mind you, since I'm not overly familiar with Java...
I'm not sure why you need a separate class for those stats at all. I thought the whole point of abstract classes were that you put the basic details of the type in there, and then got more detailed with the derived classes? So, Wages is a particular way in which the Teacher class extends the basic Person one. I might be thinking of this too much from a C++ viewpoint, mind you, since I'm not overly familiar with Java...
It probably isn't needed for this example, but I just want to see the logic of how they work and how to use them properly; I'm trying to learn this for another project where the Person class, if I didn't use an inner class, would have quite a lot of variables. I can imagine it would work the same, the problem would be that I would have is that it would be quite messy.
However, I think there's been a bit of a misunderstanding here, to my fault. I don't think inner classes are what I was planning to do, but I got muddled up with terminology there. My code probably makes more sense like this:
Spoiler
Code:
abstract public Person()
{
private String whateverString;
private int whateverInt;
private Stats stats;
abstract void greetings();
}
public TeachStats extends Stats
{
private String somethingForTeacher;
}
Code:
public StudentStats extends Stats
{
private int somethingForStudent;
}
Code:
public Student extends Person
{
private StudentStats studentStats;
public void greetings()
{
System.out.println("Hi, I'm a student.");
}
}
Code:
public Teacher extends Person
{
private TeachStats teachStats;
public void greetings()
{
System.out.println("Hello, I am a teacher.");
}
}
That is, of course, assuming that this even works; I'm starting to think I may be out of my depth here(Yet another project that'll have to go on the backburner untill I get the proper skills and experience for it. Sigh.), but I do still want to try and understand the logic of this.
Also, just as an aside; does anyone have any handy UML tutorials/notation pages that they'd recommend? Just googling for some now, but it's always nice to have one that's recommended.
Edit:
Quote:
Originally Posted by akma
*snip*
Just out of curiousity, why would I use protected as opposed to private? Would using private cause issues with inheritance? I've only ever used private/public before, but then, I've never worked with classes like this before, so c'est la vie.
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
Last edited by Miscast_Mage : 11-02-2012 at 04:23 AM.
Just out of curiousity, why would I use protected as opposed to private? Would using private cause issues with inheritance? I've only ever used private/public before, but then, I've never worked with classes like this before, so c'est la vie.
Yes, private causes issues with inheritance. Check here for a decent explanation of the difference between private, protected, and public. A protected method gives subclasses an opportunity to take advantage of the method, or to allow its children to overwrite the method, which cannot be done with a private method. Public/private only coding is OK as long as you A)don't have a lot of inheritance, and B)follow good coding style and have get/set methods for your variables.
__________________
SUBMIT your searches
TO
GOOGLE.
Yes, private causes issues with inheritance. Check here for a decent explanation of the difference between private, protected, and public. A protected method gives subclasses an opportunity to take advantage of the method, or to allow its children to overwrite the method, which cannot be done with a private method. Public/private only coding is OK as long as you A)don't have a lot of inheritance, and B)follow good coding style and have get/set methods for your variables.
Ah, I see now. I would have the variables marked as private and use getters/setters to access them. That can get quite fiddly though; having the variables as protected seems like it'll make things much easier in that regards, thanks for that.
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
Just out of curiousity, why would I use protected as opposed to private? Would using private cause issues with inheritance? I've only ever used private/public before, but then, I've never worked with classes like this before, so c'est la vie.
private variables, private methods and private inner classes cannot be accsessed by subclasses.
You could have a protected/public method named get, which returns a Stats object, which could then be used to get the wanted data.
My philosephy on accsess modifiers
(Note: This is the opposite of the approach encouraged by the tutorials I have read) Generally speaking, I believe variables should be as public as possible. If the variable can contain any value, limiting the access to it only make you write unnessecery get and set methods. If you have a reason to limit the access to a variable (if it must not contain certain values, for exemple), then limit it as neccessery (probably making it private).
With methods, I write all of them as public or private. The private ones are utility methods used by the public methods, and are not usefull outside of the class.
private variables, private methods and private inner classes cannot be accsessed by subclasses.
You could have a protected/public method named get, which returns a Stats object, which could then be used to get the wanted data.
Yeah, that's what I was doing before; using getters/setters to access private variables and methods(haven't really gotten into private inner methods yet, which I've realised aren't even what I want to do here.). Having things as protected though just seems like a lot less hassle and headaches than having to use getters to access them.
Quote:
Originally Posted by akma
My philosephy on accsess modifiers
(Note: This is the opposite of the approach encouraged by the tutorials I have read) Generally speaking, I believe variables should be as public as possible. If the variable can contain any value, limiting the access to it only make you write unnessecery get and set methods. If you have a reason to limit the access to a variable (if it must not contain certain values, for exemple), then limit it as neccessery (probably making it private).
With methods, I write all of them as public or private. The private ones are utility methods used by the public methods, and are not usefull outside of the class.
The only problem with having access modifiers as public as possible, is that does it not get inefficient the larger the code gets due to local/global scope? I got the impression that having a variable on a scope larger that it has to be inefficient and bad practice; does it actually cost more processing power to run that code, or is it just a case of different practices?
Also, I've encountered this problem three times in the last three days and I keep forgetting it:
Spoiler
Code:
public Person()
{
protected Stats stat;
protected String whatever;
public Person(String whatever)
{
this.whatever = whatever;
stat.setStats(); //a method in the Stats to class to assign data to the variables
}
}
And I was getting a nullPointerException. What I need to do was intiliaze the stat object first:
Code:
public Person()
{
protected Stats stat = new Stats();
protected String whatever;
public Person(String whatever)
{
this.whatever = whatever;
stat.setStats(); //a method in the Stats to class to assign data to the variables
}
}
I feel silly now.
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
The only problem with having access modifiers as public as possible, is that does it not get inefficient the larger the code gets due to local/global scope? I got the impression that having a variable on a scope larger that it has to be inefficient and bad practice; does it actually cost more processing power to run that code, or is it just a case of different practices?
I have never seen that claim.
I don`t think this is true. The variable would exist anyways, even if you couldn`t access it - int would "cost" 32 bits even if you could access it anywhere.
I also think you are mixing concepts, and static variables are like global variables in other leanguages, and instance variables are like local variables in other leanguages.
Maybe I`m wrong.
The arguments I remmember to make things as private as possible is to make it less clear how operations are preformed and to make it less error prone by wrong values being inserted (which is not always relevent).
I guess it`s more of a matter of personal taste - the thing that matters the most is if the code you write does the things it`s supposed to do.
I guess it`s more of a matter of personal taste - the thing that matters the most is if the code you write does the things it`s supposed to do.
Readability and maintainability are at least as important as that, IMHO. It would be perfectly possible to write code that does what it's supposed to, but is so tangled and labyrinthine that *you* can't even remember what it does when you look at it a few months later, much less someone who's been hired to maintain the program after you've left!
Readability and maintainability are at least as important as that, IMHO. It would be perfectly possible to write code that does what it's supposed to, but is so tangled and labyrinthine that *you* can't even remember what it does when you look at it a few months later, much less someone who's been hired to maintain the program after you've left!
Heh, I actually thought about that contest a week or so ago, but couldn't come up with a good way of posting it without it seem all too much out of context. It's a neat concept, isn't?
Also, on the topic of writing good code, as one of our lecturers use to say: "It's ten times as hard to debug code than it is to write it, so if you write the most complicated code you could ever make, it consequently will be impossible for you to debug".
__________________
Engineer Teddy by me. We're refitting this steam locomotive in the spirit of Steampunk, but we need your help designing it: Enter_the_design_competition_today!
(And yes, my avatar is according to scale.)
I have never seen that claim.
I don`t think this is true. The variable would exist anyways, even if you couldn`t access it - int would "cost" 32 bits even if you could access it anywhere.
I also think you are mixing concepts, and static variables are like global variables in other leanguages, and instance variables are like local variables in other leanguages.
Maybe I`m wrong.
The arguments I remmember to make things as private as possible is to make it less clear how operations are preformed and to make it less error prone by wrong values being inserted (which is not always relevent).
I guess it`s more of a matter of personal taste - the thing that matters the most is if the code you write does the things it`s supposed to do.
Ah, I must have been mistaken then; I thought I remembered my lecturer saying that it was inefficient before, but I must have been mis-remembering. My apologies.
Quote:
Originally Posted by factotum
Readability and maintainability are at least as important as that, IMHO. It would be perfectly possible to write code that does what it's supposed to, but is so tangled and labyrinthine that *you* can't even remember what it does when you look at it a few months later, much less someone who's been hired to maintain the program after you've left!
Funny thing, a friend of mine in college created a simple OS, and when he went back to it a few months later, he went "I can't remember what any of this does. " "Well that's because you don't have any comments.". So it just goes to show, comments are definitely something you want to keep in mind.
Also, I think I've got the diagram I had above sorted out now; hopefully this makes sense as a graphical representation:
Spoiler
__________________
If I'm talking about coding or programming, assume I'm talking about Java unless I say otherwise.
(◕ ‿‿ ◕)
Readability and maintainability are at least as important as that, IMHO.
I personally disaggree, but can`t seem to be able to detail any further (I`m usually better at debating).
But don`t worry, I don`t neglect this things while proggramming.
Quote:
Originally Posted by Miscast_Mage
So it just goes to show, comments are definitely something you want to keep in mind.
Generally my comments tend to be in the style of "Not debugged" and lists of methods to do (which I tend to neglect after I do the hardest, most intresting and most important methods of a class).
Reading on posts about comments, in my opinion style(comments,readable, etc) is very important. This should be taken with a grain of salt cause most of my programming is personal, although I did write a little code here and there at work to make some of my jobs easier when I was working.
When I was younger the order was more than likely 1. Program works, 2. Program is efficient, 3. style
Then I started working and the order was more like. 1.program works, 2. program is done on time, 3. program is efficient, 4. style
as I worked more in oil and gas industry and now that I'm back in school it's more like 1. program is finished on time, 2. program works and style, 4. program is efficient. It seems to work for me cause I get 100% on my assignments....Except one I got 98%
If you ever intend to work in the software industry, get in the habit of commenting well, commenting thoroughly, and commenting often.
I'm currently working on a project involving an unholy synthesis of Java, C++, and third party C libraries comprised of over 200,000 lines of code all written by one person, who probably shouldn't have been allowed near our codebase in the first place. The code is so unintuitive and over-complicated that he was the only one who understood it all, he retired two weeks ago, and he didn't leave a single comment in any of the files. And now the new guy (yours truly) gets to try to rewrite that cluster**** to make it usable when we have a new release in less than a month.
Even if you're just coding for fun, you will run into a situation where you see code without comments and hate the person who wrote the code, whether it's someone who wrote libraries or code samples on the internet or yourself-of-two-months-ago who thought yourself-right-now wouldn't need comments to remember how that one kludge worked. Comment, comment, comment.
__________________
Better to DM in Baator than play in Celestia
You can just call me Dice; that's how I roll.
Sig of Holding
Spoiler
Quote:
Originally Posted by abadguy
Darn you PoDL for making me care about a bunch of NPC Commoners!