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

Ruby is an awesome language, and it is a joy to use. Magical really is a good word to describe it.

People smarter than I am have said that it comes at the cost of performance, though.


e: I also love Scala! I like the way it does parameters, although it does take getting used to.
Yes, this was mentioned in the book. Direct quote: "Application performance is secondary. Ruby is about the performance of the programmer."


Here's how you do it with 1 line:

Code:
CollectionUtils.filter(myListWithNumbers, new HigherThanPredicate(10));
Turns [5,11,17,1] into [11,17].
Plus the lines for HigherThanPredicate.
 
It's not just about the lines of code, it's about expressiveness. I always like Java, but after reading the aforementioned book, I almost hate it. So much code to support the task you wanna achieve instead of just doing the task.
Expressiveness is a red herring. Fewer lines of code means fewer bugs. Expressiveness is convenient, but it doesn't really save that much time when you become an expert in a language.

Java is an example of a language API which has classes that exist just to make other classes work. Take the io system with readers and writers. Rather than creating something that performs a behavior, you have to create things which allow something else to perform a behavior. The end result is that you end up with a lot of lines of code to do something really simple, to the point where you can literally end up with game stopping bugs just by trying to print something to System.out.
 
Thanks again to everybody that helped me out last night; I now have another basic beginner's problem with an overly simple solution I just can't figure out for the life of me :D.

So here's my code:

Code:
package
{
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;

	
	public class Main extends MovieClip
	{
        public var ball1:Ball;
		public var vx:int = 0;
		public var vy:int = 0;
		public function Main()
		{
			ball1 = new Ball();
			ball1.x = 350;
			ball1.y = 400;
            addChild(ball1);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
			addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		public function onMouseDownHandler(event:MouseEvent):void
		{
			ball1.startDrag();
		}
		public function onMouseUpHandler(event:MouseEvent):void
		{
			ball1.stopDrag();
			ball1.alpha = Math.random();
		}
		public function onKeyDownHandler(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.DOWN) 
			{
				ball1.y - 3;
			}
			if (event.keyCode == Keyboard.UP)
			{
				ball1.y + 3;
			}
			if (event.keyCode == Keyboard.LEFT)
			{
				ball1.x - 3;
			}
			if (event.keyCode == Keyboard.RIGHT)
			{
				ball1.x + 3;
			}
		}
		public function onKeyUpHandler(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
			{
				vx = 0;
			}
			else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
			{
				vy = 0;
			}
		}
		public function onEnterFrameHandler(event:Event):void
		{
			ball1.x += vx;
			ball1.y += vy;
		}

	}
}
The only things that work when I export the SWF are my start and stop drag methods and the Math.random() which randomly changes the alpha property of my ball. I can't get any of my Keyboard events to move the ball in anyway, even though the syntax is fine and I receive no compiler errors. Help? Thanks.
 
I'm not really familiar with ActionScript, but in your onKeyDownHandler shouldn't you be doing ball1.x/y += or -= instead of the statements you're currently doing?
 
I'm not really familiar with ActionScript, but in your onKeyDownHandler shouldn't you be doing ball1.x/y += or -= instead of the statements you're currently doing?
The onEnterFrameHandler is already doing that, so it's a matter of doing something with vx and vy in onKeyDownHandler. There is already code that resets vx and vy when the key is released, which kind of makes me think that this is a homework question.
 
The onEnterFrameHandler is already doing that, so it's a matter of doing something with vx and vy in onKeyDownHandler. There is already code that resets vx and vy when the key is released, which kind of makes me think that this is a homework question.
Edit: Nevermind, I guess what your first line answered the question I posed when I first wrote this post (which was where are vx and vy being set to anything besides 0).
 
I'm not really familiar with ActionScript, but in your onKeyDownHandler shouldn't you be doing ball1.x/y += or -= instead of the statements you're currently doing?
Well what's happening here is the onKeyDownHandler moves the ball 3 pixels in whatever direction I pick, and then the onEnterFrameHandler uses vx and vy, which were previously assigned values of 0, is stopping the ball from constantly moving.

Try adding the keyboard event listeners to the stage.
Tried this, and it doesn't seem to work.

The onEnterFrameHandler is already doing that, so it's a matter of doing something with vx and vy in onKeyDownHandler. There is already code that resets vx and vy when the key is released, which kind of makes me think that this is a homework question.
Homework question? What do you mean? (it's summer lol)

I simply can't get the ball moving! Thanks again for the suggestions.
 
I actually love PHP, its fun......so far.
Functions like String to time, and how easy it is to load a text file into an array, then into a combobox.

Really need to learn some html and css though. Far too used to .net and java.
 
Well what's happening here is the onKeyDownHandler moves the ball 3 pixels in whatever direction I pick, and then the onEnterFrameHandler uses vx and vy, which were previously assigned values of 0, is stopping the ball from constantly moving.
You're not actually modifying the x and y members of your ball, though. Your statements are basically:

Code:
			if (event.keyCode == Keyboard.DOWN) 
			{
                                 //ball1.y - 3;
                                397;
			}
			if (event.keyCode == Keyboard.UP)
			{
				//ball1.y + 3;
                                403;
			}
			if (event.keyCode == Keyboard.LEFT)
			{
				//ball1.x - 3;
                                347;
			}
			if (event.keyCode == Keyboard.RIGHT)
			{
				//ball1.x + 3;
                                353;
			}
 
Expressiveness is a red herring. Fewer lines of code means fewer bugs. Expressiveness is convenient, but it doesn't really save that much time when you become an expert in a language.
I don't really want to argue with you since I'm sure you are much more knowledgeable than I am, but are you sure that expressiveness isn't an important quality for a language to have?

I suppose it becomes less important to an "expert" in that given language, but how many people are truly experts of any language, much less all of them? Something like Brainf*ck comes to mind as an extreme example, but even in a more realistic sense- is it practical to expect programmers to be experts? A language like Ruby seems like it would provide a genuine advantage for certain types of projects. While it may make more sense for larger, more complicated applications to be written in a more efficient language- surely it might sometimes make more sense to approach certain projects with the goal of getting it done quickly and with clarity (which I imagine would improve quality, since everyone is more aware of what's going on).
 
You're not actually modifying the x and y members of your ball, though. Your statements are basically:

Code:
			if (event.keyCode == Keyboard.DOWN) 
			{
                                 //ball1.y - 3;
                                397;
			}
			if (event.keyCode == Keyboard.UP)
			{
				//ball1.y + 3;
                                403;
			}
			if (event.keyCode == Keyboard.LEFT)
			{
				//ball1.x - 3;
                                347;
			}
			if (event.keyCode == Keyboard.RIGHT)
			{
				//ball1.x + 3;
                                353;
			}
Thanks for the input! I ended up completely removing both my keyUpHandlers and my enterFrameHandlers, and now the ball moves and drags fine. No problems with this, right? I feel like I may be practicing some sort of bad practice...
 
When you start to develop an RPG in your first object-oriented language, and then give up when you get done setting up the basic classes/structures.
Funny that. I made a 3d RPG for my game development module at university.

We programmed it in C++ with DirectX 9 and my god was it a massive task.

this is the prototype we ended up with: http://dl.dropbox.com/u/40547891/Hero%27s%20Quest.zip

Don't be surprised if it doesn't work on your pc though. its fairly glitchy, and we forced 4x AA and 16x AF to keep the IQ up.

It's also the worst code I've produced. Due to time constraints everything was really rushed.
 
Thanks for the input! I ended up completely removing both my keyUpHandlers and my enterFrameHandlers, and now the ball moves and drags fine. No problems with this, right? I feel like I may be practicing some sort of bad practice...
Directly modifying your position will cause movement to depend on how frequently the system is calling your key handler. Maybe this frequency is locked to some arbitrary value or you can set it. I have no idea about flash, but I would guess that flash sets an upper limit to that sort of thing, so that aspect is probably not a big deal.

The other thing about movement is that if this is for a game, characters often accelerate (up to some maximum velocity) and decelerate instead of being stop and go.

If you have some sort of idle processing (enterFrameHandler I suppose), you would change the position by the velocity multiplied by the delta time between frames.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Updating using delta time is usually not necessary in Flash applications because framerate is fixed.
 
I don't really want to argue with you since I'm sure you are much more knowledgeable than I am, but are you sure that expressiveness isn't an important quality for a language to have?
I'd say its convenient. Under the hood, most imperative programming languages are largely the same. If you wanted to write a game in Python, at the top most level, it will be structured identically to a game you would write in C or Ruby or something else. The expressiveness will decide whether you can do that task more easily, with fewer bugs. But it isn't a deal breaker.

Much more important, in my opinion, are the language libraries and portability. For instance, I wouldn't write a game in Perl. I could. But I wouldn't. I really like Objective-C and its libraries over Java and its libraries, but when it comes down to it, I can't write Objective-C programs that work on Macs, Windows, and Linus. It's easier to find help writing your Python game in the Python community than it would be to find support writing an Ada application. And of course, there's whatever you need to write in - like if you want to get into the game industry, you'll want to practice your C and C++ even though it might be more fun to write Python. No matter what language you prefer, you'll be using JavaScript for client-side web apps and Objective-C for iOS apps.

At the end of the day, expressiveness isn't about what you can do but more about how easily it is to do something. It makes some languages better suited to some problems and not others, but at the end of the day, you can pretty much solve most problems with most languages. So, don't get upset if you get stuck needing sets in JavaScript.

A language like Ruby seems like it would provide a genuine advantage for certain types of projects.
It is an advantage, but it's not a deal breaker. You can certainly solve those same problems in other languages. But Ruby has Rails, which has been far more of an advantage to its growth than any language feature. Again, it's not what the language can do, but the libraries, community, and necessity.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Although linking your game to system time rather than frame time does have some advantages. For example you can make your Flash app run at 60 fps just by inputting 60 fps in the doc properties, and your game will immediately get smoother, rather than having to recalculate all the "speeds" of objects in your games.

Also it helps combat lag, which can be common with Flash.

Unrelated note, I bought Game Engine Architecture and it is exactly what I've been looking for.
 
Although linking your game to system time rather than frame time does have some advantages. For example you can make your Flash app run at 60 fps just by inputting 60 fps in the doc properties, and your game will immediately get smoother, rather than having to recalculate all the "speeds" of objects in your games.

Also it helps combat lag, which can be common with Flash.

Unrelated note, I bought Game Engine Architecture and it is exactly what I've been looking for.
Isn't this how you can get minecraft to run at super speeds by changing the date and time?
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I've never played Minecraft so I don't know if you mean running Minecraft at increased speeds (moving faster, jumping faster etc) or "time traveling" like resetting the internal clock in Animal Crossing to relive Christmas week.
 
By setting your system time in the future after you have launches the game. The game begins to run at what seems to be a faster clock speed. The game plays like it is on fastward or something. I presume it has something to do with it being rendered off the CPU clock or something.
 
I've been using Code Academy to learn some JS in my spare time and it's hilarious. The game-y aspects to it (achievements and the like) are pretty neat though.

If only they enforced proper programming practises and better explained things for people new to programming. I feel it's a great resource if you're already familiar with OOP otherwise. I just worry for people who're only getting half the story; just because they can get the code to work doesn't mean they're really understanding things.

Still, whatever gets people into programming, I'm all for it. I've gotten some of my non-programmer friends into it and one even picked up a proper JS book. c:
 
Nice to see a general thread for programming pop up. I never paid much attention to the C++ one, but I've enjoyed the stuff in the programming rant thread.

I've been thinking of trying to pick up a new language over the summer. I could pick from one of a few and be fine, but mostly I just want to try something different to really get me thinking, just doing something. Some great resources so far for a few of the options.
 
I've been using Code Academy to learn some JS in my spare time and it's hilarious. The game-y aspects to it (achievements and the like) are pretty neat though.

If only they enforced proper programming practises and better explained things for people new to programming. I feel it's a great resource if you're already familiar with OOP otherwise. I just worry for people who're only getting half the story; just because they can get the code to work doesn't mean they're really understanding things.

Still, whatever gets people into programming, I'm all for it. I've gotten some of my non-programmer friends into it and one even picked up a proper JS book. c:
I'd have to see what they teach but for anyone that uses javascript extensively you don't actually try to go down the OO path with it, it's just not its strength. People who do are often mistakenly caught up in this aspect because of previous OO experience and wind up shoehorning OO principals into a language that is a lot more functional than it is object oriented. If you really want that you'd probably use coffeescript or something.
 
If you wanted to learn C, I don't know why you wouldn't just learn C++ first. You learn a lot of C from C++ and it's basically learning 2 languages for the price of one.
Nope. Unfortunately this isn't the case. Avoid C++.

Functional.

Learning functional programming by learning a purely functional language teaches you good practices that you can carry on to high performance computing that is usually done in C and Fortran.

Intel MIC development is in C and Fortran, no C++ nor anything OOP

I hope I can't be traced to who I'm really are IRL I just violated an NDA
I think
 
Just for fun, in Python:

Code:
[i for i in [5,11,17,1] if i > 10]
It's not as compact, but I like how is just reads in plain English.

Currently writing a web-based CMS with a Python back-end (and no, the world does not need another CMS, but it's a custom-made thing).
 
Nope. Unfortunately this isn't the case. Avoid C++.
This is the case. People who think they "know" C after having done C++ have not actually sat down to write anything in C that isn't trivial. It's not a package deal.

edit: I disagree with the sentiment of "avoiding" C++ -- I just don't feel that it's ever a "Learn C++: Be effective with C!" scenario.
 
I'm working on trying to get an actual working custom kernel working for my phone, and I'm trying to un-fuck the touchscreen driver. Found this little gem:

Code:
void print_msg(void)
{
        printk("[TSP] msg = ");
        if ((quantum_msg[1] & 0x80) == 0x80 )
                printk("1");
        else
                printk("0");
        if ((quantum_msg[1] & 0x40) == 0x40 )
                printk("1");
        else
                printk("0");
        if ((quantum_msg[1] & 0x20) == 0x20 )
                printk("1");    
        else
                printk("0");    
        if ((quantum_msg[1] & 0x10) == 0x10 )
                printk("1");    
        else
                printk("0");    

// ... it goes on for the remaining bits ...
}
Refactored version:
Code:
void print_msg(void)
{
        int i;
        printk("[TSP] msg = ");

        for(i = 0x80; i > 0; i >>= 1)
        {
                if(quantum_msg[1] & i)
                        printk("1");
                else
                        printk("0");
        }
        printk("\n");
}
 
edit: I disagree with the sentiment of "avoiding" C++ -- I just don't feel that it's ever a "Learn C++: Be effective with C!" scenario.
C++ makes you a worse programmer. I'd recommend new programmers to stay the hell away from C++ until they're more experienced.
 
C++ makes you a worse programmer. I'd recommend new programmers to stay the hell away from C++ until they're more experienced.
I agree. I am new to programming, but going from Java to c++ had been bad. There's a lot of 'don't do it unless you really know what you're doing and even then you probably shouldn't do it' in c++. But like I said I am still a noob so maybe I will grown to like it.
 
This is the case. People who think they "know" C after having done C++ have not actually sat down to write anything in C that isn't trivial. It's not a package deal.

edit: I disagree with the sentiment of "avoiding" C++ -- I just don't feel that it's ever a "Learn C++: Be effective with C!" scenario.
This. Nothing wrong with learning C++ first.

Hell, I never learned C++ formally and even I don't think you should avoid C++.
 
C++ makes you a worse programmer. I'd recommend new programmers to stay the hell away from C++ until they're more experienced.
What do you mean? I can understand disliking C++. As someone who has used it extensively for the past few years, I have my own personal grudges against it. But, I don't see how it would make someone a worse programmer.

Oh, do you mean specifically in regards to C?
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I'm also curious about the people suggesting functional languages before C++. If a novice programmer's career goals are to work in the game industry, wouldn't it be in their best interest to start out with C++ or a C++ like language?
 
I'm also curious about the people suggesting functional languages before C++. If a novice programmer's career goals are to work in the game industry, wouldn't it be in their best interest to start out with C++ or a C++ like language?
It's sort of a messy language for a beginner and they might have a hard time getting their head around the fundamentals of programming with a syntax like that.

Btw

"1120: Access of undefined property stage."

How is that even a thing? Fuck AS3

I feel like the language combined with the Flash Pro interface is confusing and weird for no good reason; or maybe I'm just stupid. Probably the latter. :/
 
Check out YouTube. I was skeptical, but it's pretty good for seeing code in action, and quicker than reading.
Best to have an IDE open too so you can try it as your watching, then afterwards tinker with it, change the code.
Also find online/written materials for what you watched, as they'll be more detailed.
 
Check out YouTube. I was skeptical, but it's pretty good for seeing code in action, and quicker than reading.
Best to have an IDE open too so you can try it as your watching, then afterwards tinker with it, change the code.
Also find online/written materials for what you watched, as they'll be more detailed.
Did you find this way more helpful when it came to learning java? I watched some videos earlier this year, but I wasn't able to remember anything from them.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
It's sort of a messy language for a beginner and they might have a hard time getting their head around the fundamentals of programming with a syntax like that.

Btw

"1120: Access of undefined property stage."

How is that even a thing? Fuck AS3

I feel like the language combined with the Flash Pro interface is confusing and weird for no good reason; or maybe I'm just stupid. Probably the latter. :/
Only the document class gets direct access to the stage supervariable I believe.