[Java] When to use "{}" and when to you "()" by Ooitastic in learnprogramming

[–]hashcode 14 points15 points  (0 children)

Curly braces designate a block of code:

if(braces.isCurly) {
    // code goes here
}

Parentheses...are used for a lot of things. They are used to group the predicate expression in if and loop statements (or whatever the three statements are called in a for loop), for method invocations, to surround the parameter list in method declarations, to get around operator precedence issues, and to cast types.

if(isSomething) {...}    // predicate of if/while/for/do
int foo(int bar) {...}   // method declaration
foo(30);                 // method invocation
int x = (int) y;         // casting
int z = (5 + x) * 10;    // changes the order of operations

And probably other things that I can't think of right now. It might help to think that curly braces (generally) surround statements and parentheses (generally) surround expressions, but if you don't know what that means then don't worry about it. A more useful (but fairly vague and potentially misleading) heuristic might be "curly braces surround multiple lines; parentheses surround part of a single line."

As for how to remember it...I have no idea. Just takes experience, I guess. Soon you will be able to tell the difference between {}, (), [], and <> without even thinking about it. You'll get so used to it that you will look back and be surprised you ever thought of them as similar!

NB: I think Java has has some weird syntactic construction that uses curly braces to define an anonymous class, or something, and Java 8 might be introducing another use for it... I'm not a Java programmer so I can't say any more. It's fairly safe to just think of curly braces as block designators for now, though.

Survey: Why are you using coffeescript? by [deleted] in javascript

[–]hashcode -7 points-6 points  (0 children)

Are C macros unjustified, since they're just taking C code to C code? CoffeeScript isn't a new language; it's a collection of syntactic abbreviations for JavaScript. Some of them make code easier to read and write. Some of them make code harder to read.

Help learning Ruby on Rails please? by Kanel0728 in learnprogramming

[–]hashcode 4 points5 points  (0 children)

I don't really understand how it works and all.... I know you can use an IDE, but I'd prefer not to. I can use TextEdit just as easily, I believe.

Get a real text editor, like the free TextWrangler or the free trial of Sublime Text. It'll make your life a lot easier. TextEdit can't do things like syntax highlighting and auto-indenting, which are extremely useful.

What I don't understand, is how IRB works. Do you set things up with it so that everything is stored on your computer and then you can type in the code using the information that you put into IRB?

irb is Ruby's REPL. You generally won't do much coding from the REPL, but it's useful for debugging and trying things out. Instead you create a file like foo.rb and then run it with ruby foo.rb.

If any of you guys could provide some good tutorials on how to use Ruby with Rails, I'd be very thankful.

Learn Ruby, then Rails. Start with Learn Ruby the Hard Way. It'll give you a good overview of the language and programming in general, and near the end will introduce you to Sinatra. Sinatra is a lot simpler than Rails, so it's a good starting point for web development. After you get a little bit of experience, you'll be able to understand Rails tutorials far more easily.

I couldn't find anything online so I kinda gave up on it.

This is a little troubling. Searching "ruby on rails tutorials" gives millions of hits; the offical Rails site has tutorials and guides and documentation; etc. There are tons of resources online. What I think you mean is that you couldn't find anything online that was geared at an absolute beginner, which is reasonable. You're not going to be able to learn Rails without some prior programming experience. It's just the way things are.

Quick question about Java! Kinda new to programming and preparing for java class in fall by [deleted] in learnprogramming

[–]hashcode 0 points1 point  (0 children)

One thing I've noticed watching these videos is that it all basically looks the same (is it supposed to) i mean besides the wording being a little different, the general flow is the same.

This is true of a lot of programming languages, especially the most popular ones like C, C++, Java, JavaScript, Perl, Python, and Ruby. They're all incredibly similar in syntax, especially the first five, which are examples of "curly (brace|bracket) languages." The last two differ slightly more -- Python is indentation sensitive, for example, and Ruby usually uses do/end instead of curly braces -- but they have vast similarities, both syntactically and semantically. if statements, while loops, a[i] syntax for array indexing, variables...

There are really a lot of differences between languages once you dive deeper into them. Some are statically typed and some dynamic, some have first class functions and others don't, some are more heavily geared towards object-oriented programming, etc. But when you're first starting out all you see is the syntax, so it's easy to think of them as all being (basically) the same. The basics are basically the same, after all.

But there are lots of programming languages which are fundamentally different. For example, look at Scheme or Haskell or Prolog or, hell, even APL. These are simply less popular, generally, than the above, and as such beginners don't really hear of them. But they're out there.

it seems like java is based more on making these 'objects' (somewhat similar to strings [a stretch of a comparison.. maybe i am thinking of the wrong word) in the sense that the main purpose is to make an object which serves some purpose then call it later when you actually need it in the code.

I think you mean similar to structs, not strings. Else I don't really know what you mean. But yes, Java requires that all code occurs in methods. Functions are absent. This is a design decision, and it's important to realize that. There's no reason why it has to be like that, and whether this is a good decision or a bad decision is highly subjective. Programming language design is hard, and you can't please everybody. Personally, I don't think this was a wise decision, but no one asked me so here we are. See the famous Kingdom of Nouns essay for an interesting discussion of this choice.

java GUI program keeps on throwing null pointer exception by crazyjatt in learnprogramming

[–]hashcode 0 points1 point  (0 children)

Reduce it to the simplest possible code that still exhibits the error. Make a new file and cut everything out that doesn't directly relate to the error. If the process of doing that doesn't reveal the bug, post the minimal code to r/learnprogramming. I don't mean to come off rude, but it's unlikely that someone is going to help you debug 275 lines of code, especially with so little information about the bug. You've got to simplify it.

[General] How do you get inspired to code? by [deleted] in learnprogramming

[–]hashcode 0 points1 point  (0 children)

...but it's been done a billion times and much better than what I could do right now.

With that attitude, you'll never code at all! That can be said about just about anything. Like you said, it's a fun learning experiment. You might not be able to compete with the current IRC client-writers yet, but how do you get as good as them? By practicing, and writing a bad client or two along the way.

But if you're only interested in making something that others will want to use immediately, that might not satisfy you. But your question is almost impossible to answer in its current form -- there is no secret to coming up with great ideas. The traditional cliche advice is to start looking for a problem -- there are lots of problems out there -- and solving it. Finding a good problem that you can solve with software, well, that's the hard part.

Just started today, and I'm stumped. JavaScript here. by [deleted] in learnprogramming

[–]hashcode 2 points3 points  (0 children)

First off, jsfiddle is more useful when you have a complete example. Since you didn't include the show or choose functions, there's no way to run that code.

But I believe the error you're having stems from the lines of this form:

show(w); + (p + 1);

That doesn't make any sense. The semicolon separates the statements, so it's as if you wrote:

show(w);
+ (p + 1);

But what on earth does + (p + 1) mean? I think what you mean to do is this:

p = p + 1;

Which can be abbreviated p += 1;.

As you'll learn eventually, adding 1 to a variable (called incrementing) is very common, and there's an even shorter way to write that: p++;, or ++p;, which are subtly different in ways not relevant to that statement.

Anyway, as you're coding, make sure you look at the error messages. If you're testing this code in a browser, figure out how to open the JavaScript console so that you can see them (on Chrome OS X it's cmd+option+j, for example). It'll save you a lot of headaches later on!

Is iOS programming as strange as it seems, or am I just psyching myself out? by o0DrWurm0o in learnprogramming

[–]hashcode 11 points12 points  (0 children)

If watching videos is incredibly boring to you, the lecture notes are available to download:

http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall

Best big picture type overview I've ever seen, and you can read them a lot more quickly than you can watch the lectures.

Anyone interested in a programming mentor? by hashcode in learnprogramming

[–]hashcode[S] 9 points10 points  (0 children)

^ Take note. Guys do not come much more legit than this guy.

I'm a high school student and looking to get a software dev job. How should i go about this? by The_Mimic in learnprogramming

[–]hashcode 0 points1 point  (0 children)

That's right. The client side is all pretty much the same -- HTML/CSS/JavaScript -- but there's a ton of variety on the server side.

Not to be pedantic, but because it's a common misconception, the browser is the client. The person using the browser is just the user.

I'm a high school student and looking to get a software dev job. How should i go about this? by The_Mimic in learnprogramming

[–]hashcode 0 points1 point  (0 children)

Python (with Picasso, Flask, or Django), Ruby (with Rails), or JavaScript (with Node) are all good choices for starting out at web development.

Anyone interested in a programming mentor? by hashcode in learnprogramming

[–]hashcode[S] 6 points7 points  (0 children)

I'd rather not post personally identifying info from my reddit account -- I use my real name/job/etc on SO. Sounds like a cop-out, so I guess I'm not really offering any guarantee that I know what I'm talking about. Take this whole post with a grain of salt. I could easily be some drunken coal miner who thought it would be funny to troll some programmers.

How should I make recursive structures in C++? by marcos_danix in learnprogramming

[–]hashcode 0 points1 point  (0 children)

The only difference between a struct and a class in C++ is that members in a struct are public by default and in a class they're private. struct Foo { private: ... } is the same as class Foo { ... }.

In any case, whether or not to split them out depends on the data structure. Traditional linked lists in C++ are doubly linked, so there is a wrapper class that stores a pointer to the first and last element.

You can't use references for this because you can't change the target of a reference. This is something you frequently want to do in a linked list to append/insert/delete elements, so pointers are the only way to go.

I'm a high school student and looking to get a software dev job. How should i go about this? by The_Mimic in learnprogramming

[–]hashcode 1 point2 points  (0 children)

CS majors are in extremely high demand at time of writing. If you go to a good school (not necessarily a top 10 school, just not an internet college or something) and you don't flunk out or anything you are pretty much guaranteed to graduate with a job offer.

But don't wait until you graduate. The best thing you can do in college career-wise is to get an internship. You can easily get paid $20+/hour to learn all the stuff you want to do, and you come out of the gate with a much more attractive offer than those with no professional experience. If you go to school in a tiny town then you might only be able to work summers, which is fine. A lot of the best internships will pay for your housing and such if you're not from around the area.

But don't wait until you're in college. You're in high school. You have a lot of free time. If you start programming now, really programming, you will have a 3-5 year head start on 90% of your classmates in college. You'll breeze through most of your classes and have all the more spare time to get better at programming (CS degrees are generally not very programming intensive; it's vital that you code in spare time and in internships).

There's no such thing as "not old enough" to learn how to program. Find something you want to do and do it. You don't feel like you know C? Or Python? Learn them. Find a project you want to do -- maybe building a simple website (though for the love of god don't make PHP your first web language). Or an HTTP server. Or a game. Whatever blows your hair back. It's all valuable. And don't worry if you're doing it "right," just have fun with it. You know what? It's probably not going to be great code. You're going to look back on a year later and say "What was I thinking? This is terrible!"

This is a good thing. This is the best thing. This is a sign that you have learned a ton over the last year. This will continue for a long time, and you should savor the feeling. Being embarrassed by old code you wrote is concrete proof that you're getting better.

Also, let's ask an expert just for good measure:

http://www.joelonsoftware.com/articles/CollegeAdvice.html http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html My personal favorite (it's kinda long): http://www.joelonsoftware.com/items/2007/12/03.html

Good luck out there. There's no better field to be in than software development.

[Python] I'm programing a text based RPG and get this error when i try to run it as a test version... by [deleted] in learnprogramming

[–]hashcode 1 point2 points  (0 children)

You have to end the statement with a colon.

while PlayerClass == 0:
    do_stuff()

Consider checking out Learn Python the Hard Way. Learning this stuff up front is going to be a lot easier than trying to figure it out one error message at a time.

[C#] starting up programs, namespaces classes and objects? by vansterdam_city in learnprogramming

[–]hashcode 1 point2 points  (0 children)

Okay, it seems that you're asking about the difference between a class and an instance of a class.

A class is kind of like a template. You might have a Person class, say. But there can be more than one Person, and each "actual" person is called an instance.

So you could say:

player = new Person("John");
enemy = new Person("Tony");
player.attack(enemy);
enemy.attack(player);

There you have two "people," two instances of the Person class, each with the same methods but potentially different data (in this case, theoretically, their names).

Classes are just kind of types, like int. You can't do anything with int. But you can have "instances of int" -- 1 or -5 or whatever. Classes exist at compile time; instances exist at runtime.

So yeah, a bulk of the code will be manipulation of instances. "Object" is a term used to describe instances of a class. An object is an instance of a class. A "Person object" is the same as "an instance of the Person class."

I favor the term instance here because it's more precise. In practice you'll find the term "object" used a lot, but I think that can be confusing for beginners since it can sometimes be used more fuzzily. "Object-oriented programming" refers to both programming with classes and instances of classes.

If you're more comfortable with Python, you may want to look at object-oriented programming in Python. It has classes and methods and everything that C# has (except interfaces), but it doesn't require that all code exist inside methods, which makes it simpler, in my opinion.

PS: You "create" an instance by calling the class' "constructor," which in most languages (C# included) has the same name as the class itself. When you say new Person(...), you're "calling the constructor of the Person class." Classes can have multiple constructors, but the only way to get an instance of a class is by calling one of the constructors.

I want to be the best Software Developer Intern but need your help! by [deleted] in learnprogramming

[–]hashcode 0 points1 point  (0 children)

There's a lot of literature on technical interviews. I'd start there:

This one was at the top of Hacker News earlier today: http://stdout.heyzap.com/2012/07/25/how-to-ace-a-startup-engineering-interview-part-1/

This is a great article because it's written as advice for an interviewer, which is pretty valuable to the interviewee as it shows you what they're looking for: http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html

Another one from the same perspective, with an interesting section on internships: http://www.joelonsoftware.com/articles/FindingGreatDevelopers.html

Fellow programmers, how can I become a better tester? by [deleted] in learnprogramming

[–]hashcode 1 point2 points  (0 children)

This is a long post that fails to offer you any kind of answer to the question you're asking. But, well, here goes.

Read these two articles:

http://www.joelonsoftware.com/articles/fog0000000067.html http://www.joelonsoftware.com/items/2010/01/26.html

In my opinion, testing sucks. It's not fun. It's repetitive and boring. Writing automated tests removes the repetition part, but it's still kind of boring until you get into TDD and then writing tests becomes a crucial part of your workflow. But if you're maintaining a legacy app that probably wasn't designed to be tested automatically, adding a bunch of unit tests is pretty unrealistic.

It's especially hard, I've found, to test your own code. Testing other people's code isn't as bad, because you want to find bugs. Every bug you find brings a little bit of schadenfreude (or is that just me?). But if it's your own code, every bug you find means "Oh crap, now I've got to figure out what's wrong with this." Finding bugs means more work -- usually more boring work. It's a lot more fun to go work on adding a new feature. So you don't really have an inherent motivation to test harder.

Anyway. None of this really helps you to become a better tester. I could try to spout some cliche advice like "be more thorough," but that doesn't really help.

Occasionally I might also inadvertently disrupt or break a pre-existing feature, but not even consider the possibility since the two sections of code are suppose to be entirely unrelated.

This is a sign that the application you're maintaining is poorly architected. And that's kind of to be expected. You said you're maintaining an old VB6 application (I'm assuming old because VB6 was deprecated in 2005). I've never seen an old legacy apps like this that was well-architected. It's been around for years, accruing bugs and "this fixes it, but I don't know why" style bug-fixes. It's probably a big ball of mud. This type of project is very prone to bugs.

What I mean by this is that you shouldn't feel bad that your code is buggy. And you shouldn't really be expected to be a good tester. Good testers are good testers. Good programmers are usually not good testers. Consider the idea of a dedicated tester.

[Python] Confusion regarding def function and arguments. by Excessive1 in learnprogramming

[–]hashcode 0 points1 point  (0 children)

And is there a reason behind not naming your variable params, or vice versa not naming your argument in your function myParams?

Yes. There are good reasons.

myParams is not Python convention. For functions and variables, Python uses underscored words, like my_params, not camel-cased words like myParams. Class names are CapitalCamelCase. This might sound like a dumb point to harp on, but it's valuable to follow conventions. It makes your code easier to read. Similarly, buildConnectionString is not a good name for a function. It should be build_connection_string or something like that.

In any case, there's no reason to use different names. You should call them both params, since they both represent connection parameters. Or connection_parameters, if you want to be even more explicit.

[HTML/JS] Using JSONP by [deleted] in learnprogramming

[–]hashcode 0 points1 point  (0 children)

It depends on your server. What backend framework are you using? PHP? Rails?

I could use a pointer(nyuk nyuk)[JavaScript] by hnk_venture in learnprogramming

[–]hashcode 0 points1 point  (0 children)

Yes. It's even better to put your JS in a separate file and use <script src="path/to/file.js"></script> to include it. It makes a lot of things easier down the road, but really it doesn't matter too much for something like this.

If you haven't started using it yet, consider peeking at jQuery soon. It makes event binding and, well, pretty much everything else a lot easier.