• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Water

Member
For someone who moves around code using arrow keys you space coders are the worst. Tabs means I get around quickly
Using a less bad editor is the solution. Not necessarily vim
jk of course vim
where movement commands allow you to ignore whitespace, but a good editor should give you settings that pretend every X spaces are a tab, so you can freely select your personal editing preference while the editor takes care of loading/saving files to automatically conform to the project standard.
only editor I've used where I'm sure that feature exists is vim, though :-D

If your editor is ass, you could still script your version control client and / or repository hooks so they autoconvert to/from tabs.
 

r1chard

Member
Any decent editor worth its salt makes movement a breeze. And treats N spaces as tab marks. Seriously, if your editor doesn't do this, it's time to get a new editor.
 

Massa

Member
So are there any pros to using spaces instead of tabs?

Yes. The code looks consistent no matter what editor is being used to work on it.

Just this weekend I actually worked on some code where the guy was using tabs and his editor clearly is set to tabs with 8 spaces width, while mine is set to 4. That would've been fine of course but the code had some places where it used 8 spaces instead of tab, so what looked perfect to the other guy looked like a complete mess on my editor.
 
I wonder how many times tabs v. spaces have been discussed in this thread...I know this is the second time within just the past couple of months.
 

usea

Member
Yes. The code looks consistent no matter what editor is being used to work on it.

Just this weekend I actually worked on some code where the guy was using tabs and his editor clearly is set to tabs with 8 spaces width, while mine is set to 4. That would've been fine of course but the code had some places where it used 8 spaces instead of tab, so what looked perfect to the other guy looked like a complete mess on my editor.
This isn't a problem with tabs, it's a problem with spaces. If the code actually used tabs, it'd be correct and flexible. But because some spaces got thrown in there, it was all messed up. The problem isn't the tabs. The problem was some lines had 8 spaces before it.
 

Slavik81

Member
80 column rule is dumb anyway. Also many more characters than tabs are variable width. What happens when there's unicode symbols in your code?

The default isn't always wrong. Each editor has its own default. So it goes both ways. Also the adjustment only happens once. You're talking a few seconds, one time, maybe. The smallest of concerns.
I say the default is wrong facetiously because it often feels like I spend a lot of time fixing it. If it only did need to be done once, that would be one thing, but it needs to be changed in every editor and viewer I use on every computer, and it gets worse when the code is integrated into a website. Fixing the ridiculous 8-space tabs on GitHub requires installing browser plug-ins and injecting CSS.

I'm not doing that across my desktop, netbook, laptop, phone and multitude of operating systems and browsers I have on each, plus whatever other computers I may use in the future. It's much simpler to use spaces and have my code always read nicely without any fuss.

If you like using tabs that's fine. I like their conceptual integrity, but when I used them I found I was spending too much time configuring editors and viewers.
 

usea

Member
I say the default is wrong facetiously because it often feels like I spend a lot of time fixing it. If it only did need to be done once, that would be one thing, but it needs to be changed in every editor and viewer I use on every computer, and it gets worse when the code is integrated into a website. Fixing the ridiculous 8-space tabs on GitHub requires installing browser plug-ins and injecting CSS.

I'm not doing that across my desktop, netbook, laptop, phone and multitude of operating systems and browsers I have on each, plus whatever other computers I may use in the future. It's much simpler to use spaces and have my code always read nicely without any fuss.

If you like using tabs that's fine. I like their conceptual integrity, but when I used them I found I was spending too much time configuring editors and viewers.
You're right about viewing code in the browser, that's a real problem I neglected to mention. In general when putting code up for a gist or pastebin I convert to spaces. I don't do it when I push my code to a repository hosted on github, and the cost of that is that it's annoying to view on their website. And by annoying I mean that indents are like 8 spaces, not that the code looks wrong or anything. It looks fine, just with huge indents.

Also, this is c#, where tabs is more acceptable. When I'm writing javascript, go, or rust, I use spaces because that's what the people who live in those domains mostly use.
 

PlayDat

Member
Pretty basic question here. I learning programming with a Java course in school. We used DrJava as our IDE. I've been trying to get into the habit of using Eclipse instead since it seems to be much more common.

All the extra functionality is kind of overwhelming. Right now I'm not even sure how to run and compile code that's already been written. I started by creating a new project (something I never even had to think about in DrJava). The first file I started with ran just fine, but I downloaded some starter code from a course website and have no idea how to compile and run it. In DrJava I would just click the file i felt like editing over in the box to the left, hit compile, then run. Eclipse is asking me about run configurations, projects, and which main class I want. I tried copying the .java file I downloaded to the same src and bin folders I was working with before, but it won't show up as an option when I try to choose a main class to run.

I'm assuming I need to make a class file out of the one I'm trying to add but the only way I know to get a class file from a java file is through using DrJava. Since I'm trying not to be so reliant on it anymore I'm looking for another way.
 

upandaway

Member
Pretty basic question here. I learning programming with a Java course in school. We used DrJava as our IDE. I've been trying to get into the habit of using Eclipse instead since it seems to be much more common.

All the extra functionality is kind of overwhelming. Right now I'm not even sure how to run and compile code that's already been written. I started by creating a new project (something I never even had to think about in DrJava). The first file I started with ran just fine, but I downloaded some starter code from a course website and have no idea how to compile and run it. In DrJava I would just click the file i felt like editing over in the box to the left, hit compile, then run. Eclipse is asking me about run configurations, projects, and which main class I want. I tried copying the .java file I downloaded to the same src and bin folders I was working with before, but it won't show up as an option when I try to choose a main class to run.

I'm assuming I need to make a class file out of the one I'm trying to add but the only way I know to get a class file from a java file is through using DrJava. Since I'm trying not to be so reliant on it anymore I'm looking for another way.
I'd actually recommend to try and see how Java code is organized and compiled as it is without any IDEs, like try writing and running a Hello World without any IDE using some tutorial, from the command line. Knowing how it works under the surface will make it clearer what steps the IDEs do automatically for you (and make you less dependent on a specific IDE like you were with DrJava).
 

oxrock

Gravity is a myth, the Earth SUCKS!
I'd actually recommend to try and see how Java code is organized and compiled as it is without any IDEs, like try writing and running a Hello World without any IDE using some tutorial, from the command line. Knowing how it works under the surface will make it clearer what steps the IDEs do automatically for you (and make you less dependent on a specific IDE like you were with DrJava).

And then thank the coding lord that IDE's exist.
 

Kansoku

Member
Pretty basic question here. I learning programming with a Java course in school. We used DrJava as our IDE. I've been trying to get into the habit of using Eclipse instead since it seems to be much more common.

All the extra functionality is kind of overwhelming. Right now I'm not even sure how to run and compile code that's already been written. I started by creating a new project (something I never even had to think about in DrJava). The first file I started with ran just fine, but I downloaded some starter code from a course website and have no idea how to compile and run it. In DrJava I would just click the file i felt like editing over in the box to the left, hit compile, then run. Eclipse is asking me about run configurations, projects, and which main class I want. I tried copying the .java file I downloaded to the same src and bin folders I was working with before, but it won't show up as an option when I try to choose a main class to run.

I'm assuming I need to make a class file out of the one I'm trying to add but the only way I know to get a class file from a java file is through using DrJava. Since I'm trying not to be so reliant on it anymore I'm looking for another way.

That's the reason I don't use Eclipse, (and I assume that Net Beans is the same thing). I'm so used to write the code and compile it directly, that all these project stuff is too much. Oddly enough, I was okay with it when I was trying to make an android app.
 
Pretty basic question here. I learning programming with a Java course in school. We used DrJava as our IDE. I've been trying to get into the habit of using Eclipse instead since it seems to be much more common.

All the extra functionality is kind of overwhelming. Right now I'm not even sure how to run and compile code that's already been written. I started by creating a new project (something I never even had to think about in DrJava). The first file I started with ran just fine, but I downloaded some starter code from a course website and have no idea how to compile and run it. In DrJava I would just click the file i felt like editing over in the box to the left, hit compile, then run. Eclipse is asking me about run configurations, projects, and which main class I want. I tried copying the .java file I downloaded to the same src and bin folders I was working with before, but it won't show up as an option when I try to choose a main class to run.

I'm assuming I need to make a class file out of the one I'm trying to add but the only way I know to get a class file from a java file is through using DrJava. Since I'm trying not to be so reliant on it anymore I'm looking for another way.

Best thing to do is learn how to use maven, ant, or gradle. Build tools make your life easier since most IDEs have either builtin support for them or plugins are available. As well, when compiling from the command line, build tools make it easier since you don't have to worry calling the compiler on every file. Instead, the build tool will compile, package, call tests, etc as part of the build process.
 

Exuro

Member
I have a file system question. I'm making a fake os for a school assignment and need some help/guidance on the file system portion. It uses nfs and it's asking to suggest an optimization for read requests of large contiguous chunks as well as write requests of large non-contiguous chunks. Does anyone know of any simple ones or something I could read that would help me determine this?

I would guess for large read requests I could have a client cache that stores large contiguous data to the client and the user would have quick access to it instead of having to send multiple requests.

I'm not quite sure how I could optimize non contiguous data for writes though.
Thanks.
 

BreakyBoy

o_O @_@ O_o
Here are some excellent talks from Monitorama. These aren't actually specifically about monitoring, so I recommend them both to everyone in this thread. Seasoned pro or new student. They're fantastic, and are fun ways to illustrate some of the common pitfalls in the field.

Computers are a Sadness, I am the Cure

17th Century Shipbuilding and Your Failed Software Project

Edit: And because I was too busy to join in the conversation while it was being bantered back and forth, I will say that I'm more interested in the spirit of the 80 Column Rule, than the actual application of it. There's definitely some (overly IMO) verbose languages that make it near-impossible to adhere to. Not to mention sometimes you're working with a group that has style guidelines that require variables like storefrontShoppingCartItemDetailedDescription. Also, it's worth mentioning that sometimes blind adherence to the rule makes things worse than if you just wrote it on one line to begin with.

In general though, I like people to keep it in mind, because in general it helps with code legibility by discouraging monstrous one-liners that are a pain to debug.
 

Linkhero1

Member
Question regarding ArrayLists. If I have the following


Code:
ArrayList <String> list1 = new ArrayList<String>();
ArrayList <String> list2 = new ArrayList<String>();

list1.add("one");
list1.add("two");
list1.add("three");

What's the difference between:

Code:
list2.addAll(list1);

and

Code:
for (int i = 0; i < list1.size(); i++) {
     list2.add(list1.get(i));
}
 

Rapstah

Member
I don't think there is a functional difference. addAll is supposed to add all elements in a collection to the collection you're calling it from in the order the collection's iterator returns, and an array list's iterator just goes numerically from 0 to one short of the length of the array list.
 

poweld

Member
Question regarding ArrayLists. If I have the following


Code:
ArrayList <String> list1 = new ArrayList<String>();
ArrayList <String> list2 = new ArrayList<String>();

list1.add("one");
list1.add("two");
list1.add("three");

What's the difference between:

Code:
list2.addAll(list1);

and

Code:
for (int i = 0; i < list1.size(); i++) {
     list2.add(list1.get(i));
}

Seems to me like it's just a convenience method. Both of your examples should be fine, though of course addAll is cleaner looking.
 

BreakyBoy

o_O @_@ O_o
Question regarding ArrayLists

My Java is probably rusty, but for this specific example, it looks like it should do the same thing to me. Reference: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)

It mentions that it appends the given collection to the end of the ArrayList in the order provided by the specified collection's iterator. In the case of a simple ArrayList, I would assume that it will just return in the same order shown in your for loop.

I believe a HashMap is also considered a Collection though, so in that case, the order it appends the HashMap's items to the ArrayList may vary.


Again, my Java is really rusty, so this is more from a quick reading of the docs and your code snippet. Some one else who has more experience might correct me.

Edit: Y'all are quick!
 

iapetus

Scary Euro Man
What's the difference between:

Code:
list2.addAll(list1);

and

Code:
for (int i = 0; i < list1.size(); i++) {
     list2.add(list1.get(i));
}

Functionally? Pretty much nothing. In terms of implementation? You can take a look at the source for ArrayList#addAll and see for yourself. It's not remotely similar. :p
 
Depends on your background. Are you new to programming, have you been studying for a while, actually done some pet projects in another language, etc?

Up to this point I've only used Javascript. C looks somewhat similar so far, but I've heard the language is a lot less forgiving.
 
Learn about pointers. Make cheat sheets for them. Study until you feel you understand them. Then study some more.

Pointers are great and useful but cause lots of problems for those new to the language.
 
Up to this point I've only used Javascript. C looks somewhat similar so far, but I've heard the language is a lot less forgiving.

Yeah, the various aspects of memory management will most likely be the trickiest. Getting a head start on pointers and memory allocation should help a lot.
 

Linkhero1

Member
This is homework related so I don't want to post the entire code, but I'm having trouble with scanners.

I'm reading in an input of ints from the user e.g.

Code:
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter some integers: ")

while (scanner.hasNext()) {
        System.out.println(scanner.nextInt());
}


scanner.close();

I'm also reading for yes/no answers as well later on in the same code. Something like:

Code:
System.out.print("Enter more integers (yes/no)? ");
                 
                 if (sc.next().equals("yes")) {
                     displayPayout = true;
                 }

So, I'm using two scanners. The first within a method and the second within the main. Then I get this error.

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at PJ4.MyPokerGame.play(MyPokerGame.java:359)
at TestMyPokerGame.main(TestMyPokerGame.java:26)

If you need to see the actual code I'll PM you.
 

poweld

Member
This is homework related so I don't want to post the entire code, but I'm having trouble with scanners.

I'm reading in an input of ints from the user

Check out the docs: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()

Java methods usually have well-defined reasons why a particular exception will be thrown. In this case, I believe you are attempting to pull data from the scanner when there is none; you must check if it has data first (see your first snippet).
 

Linkhero1

Member
Check out the docs: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()

Java methods usually have well-defined reasons why a particular exception will be thrown. In this case, I believe you are attempting to pull data from the scanner when there is none; you must check if it has data first (see your first snippet).

Makes sense. Actually, I should have done that first. I created a new project and began messing around with scanner. I just have to sift through the code I currently have and make sure to check if there's data first.

Thank you :)
 

Linkhero1

Member
Out of curiosity, where are you defining your second Scanner, 'sc' ?

The first code snippet is from within a function that asks user to input some integer. The second is within the main function. It's asking the user if they want to enter more integers. The code I posted isn't from the homework but it's close to what I'm trying to achieve.

If you're still having issues, it looks like this would be helpful:
http://stackoverflow.com/questions/15443383/scanner-nosuchelementexception

Thank you :)

Edit: This is exactly what my problem is. I was trying to use two scanners with System.in. The only reason I was using two is because my function wasn't working properly with just one but I'll spend some time debugging it and see if I'm able to fix it.
 
Ok so this probably shouldn't be causing me so much trouble but I'm running on no sleep for over 24 hours now and this is the last little piece of work I need to get done.

All I need is an algorithm for creating a frequency table (in order to draw a histogram or something) it can be in pseudocode or just structured English but for some reason by brain won't kick into gear. The algorithm assumes I have data in an array and should result in an array that simply has the frequency counts.

Someone please give me a hand. Please.
 
All I need is an algorithm for creating a frequency table (in order to draw a histogram or something) it can be in pseudocode or just structured English but for some reason by brain won't kick into gear. The algorithm assumes I have data in an array and should result in an array that simply has the frequency counts.

Ok, I don't know how much precision is needed, so you get to write the 'determine_bucket' function. Also, you didn't specify if you know the amount of buckets you'll need, so this determines it.

Also, the theoretical maximum and minimum sizes of whatever you're analyzing.

Code:
//first pass
low_value = Theoretical_max
high_value = theoretical_min

Loop over input {
    if element < low_value 
        low_value = element
    else if element > high_value
        high_value = element
}
num_buckets = (determine_bucket(high_value) – determine_bucket(low_value))/bucket_width

buckets = array of size num_buckets


zero/initialize each element of buckets
//exercise to reader

//count them
loop over input {
    bucket = determine_bucket(element)
    buckets[bucket]++
}

In a dynamic language you can expand buckets on the fly and sort it afterwards, so no need for a pass; can do the same thing with a linked list in a C like language; all depends on what you are optimizing for, if anything.


If I am beaten it is because I wrote this on my phone.
 

poweld

Member
Ok so this probably shouldn't be causing me so much trouble but I'm running on no sleep for over 24 hours now and this is the last little piece of work I need to get done.

All I need is an algorithm for creating a frequency table (in order to draw a histogram or something) it can be in pseudocode or just structured English but for some reason by brain won't kick into gear. The algorithm assumes I have data in an array and should result in an array that simply has the frequency counts.

Someone please give me a hand. Please.

I don't think anyone should do your homework for you. If you want, you can post what you've got thus far and we can help.

edit: I guess people will do your homework for you here :(
 
I don't think anyone should do your homework for you. If you want, you can post what you've got thus far and we can help.

edit: I guess people will do your homework for you here :(

I only read the quoted portion in the post above mine.. didn't realize it was hw until just now :(
 
It's not really a case of anyone doing it for me, I've just finished writing a few others and getting my other tasks all done. I'm just so so tired at this point, can't think straight but I need to get this last thing done for a couple of hours time!

Once I have something I'm gunna properly go through it myself tomorrow when I've had a bit of sleep!
 

mltplkxr

Member
Pretty basic question here. I learning programming with a Java course in school. We used DrJava as our IDE. I've been trying to get into the habit of using Eclipse instead since it seems to be much more common.

All the extra functionality is kind of overwhelming. Right now I'm not even sure how to run and compile code that's already been written. I started by creating a new project (something I never even had to think about in DrJava). The first file I started with ran just fine, but I downloaded some starter code from a course website and have no idea how to compile and run it. In DrJava I would just click the file i felt like editing over in the box to the left, hit compile, then run. Eclipse is asking me about run configurations, projects, and which main class I want. I tried copying the .java file I downloaded to the same src and bin folders I was working with before, but it won't show up as an option when I try to choose a main class to run.

I'm assuming I need to make a class file out of the one I'm trying to add but the only way I know to get a class file from a java file is through using DrJava. Since I'm trying not to be so reliant on it anymore I'm looking for another way.

Yeah, Eclipse is horrible. It's the equivalent of your government in an IDE: built by committee, does everything for everyone in 10 different ways, bloated, hard to find your way around and the documentation is incomplete, misleading and inscrutable, when it's not flat-out outdated. : ) Once you know your way around for what you want to do though, it gets the job done. And there are tons of cool plugins.

Sadly, your assessment is right when you say it's pretty common. Its main advantage is the huge community around it. So, it's a good idea to learn it. If you're a student, I recommend getting your hands on IntelliJ, it's the industry favorite. Never heard of DrJava.

Now for the matter at hand. What I do when I want to run a "quick test in Eclipse":
1. I start a Java project : File -> New -> Java Project
2. Type the name, etc.
3. Finish
3. Once the project is created, expand the project folder
4. Right-click on the src folder
5. Choose New... -> Class in the contextual menu
6. Give it a name.
7. Tick the box that says "Generate main method" or something like that.
8. Finish
9 Type your code in the main method of the class that has been generated.
10. When you're ready to run it, right-click in the editor window.
11. From the contextual menu, choose Run As... -> Java Application
12. If you have print outs (or exceptions!), they appear in the Console window below.
 
After trying to recreate some SDL projects in Windows, I've been getting the following warning in Visual Studio:
"warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library"

So far though, most of my projects and programs are running fine. Will this warning come to bite me in the back in the future?
 

mltplkxr

Member
Question regarding ArrayLists. If I have the following


Code:
ArrayList <String> list1 = new ArrayList<String>();
ArrayList <String> list2 = new ArrayList<String>();

list1.add("one");
list1.add("two");
list1.add("three");

What's the difference between:

Code:
list2.addAll(list1);

and

Code:
for (int i = 0; i < list1.size(); i++) {
     list2.add(list1.get(i));
}

That addAll() saves you 2/3 lines of code and a possible ArrayOutOfBoundsException so it's pretty handy (and if it wasn't there people would be bitchin' that they have to write that loop every time).
I can't recommend enough the API Javadoc of the Collection framework (http://docs.oracle.com/javase/7/docs/api/) it's pretty thorough and full of good advice and knowledge (and examples of applied design patterns).
 
Top Bottom