Author Topic: With or without a class?  (Read 8761 times)

SarahW

  • Newcomer
  • Posts: 33
  • Karma: +0/-0
    • View Profile
    • Email
With or without a class?
« on: March 01, 2017, 10:25:59 AM »
I know some would probably say pick another language besides Ruby. But I find using C++ almost impossible, or maybe I'm not looking at programming in the right way.

Would there ever be a reason to do most operations outside of a class? The way I was taught with Ruby, they always said to try to avoid doing anything outside of a class. Which means, doing things within a functional basis.

But then what I know now is its best not to overcrowd functions, or you'll end up with seemingly endless nesting. And took a while to figure out you shouldn't use break or load (although in practice I get less errors through load.)

Can something like basic navigation be effectively done outside of a class?

I've gotten into this trap of assigning tiles to a tile variable and switching off variables the @ sign isn't standing on. And I have different functions doing different things: one displays ASCII graphics, one receives input, then functions beneath take some kind of game play function before it is eventually looped back to the function that displays the ASCII graphical conditionals.

I tried doing things outside of a class a long time ago, but couldn't really skip to different part of the code without crude nesting.

I tried C++, but it's ... weird and not intuitive for me. I love Ruby.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: With or without a class?
« Reply #1 on: March 01, 2017, 11:06:49 AM »
Which means, doing things within a functional basis.

You can do that in C++ very well, but often a class is a better solution anyway providing that it's used correctly. Functions are nice when you process simple or static data. Sometimes you need to get some data without creating an instance of a class which can be both safer (only static data and "pure" functions) and also less demanding for memory management. With functions I find that namespaces help a lot to avoid problems in large projects. Namespaces are like a "type" of static class for functions, that look like "member" functions of that namespace.
« Last Edit: March 01, 2017, 11:11:14 AM by Krice »

SarahW

  • Newcomer
  • Posts: 33
  • Karma: +0/-0
    • View Profile
    • Email
Re: With or without a class?
« Reply #2 on: March 01, 2017, 09:27:16 PM »
Which means, doing things within a functional basis.

You can do that in C++ very well, but often a class is a better solution anyway providing that it's used correctly. Functions are nice when you process simple or static data. Sometimes you need to get some data without creating an instance of a class which can be both safer (only static data and "pure" functions) and also less demanding for memory management. With functions I find that namespaces help a lot to avoid problems in large projects. Namespaces are like a "type" of static class for functions, that look like "member" functions of that namespace.

Ah OK cool so you don't necessarily need to use a class in all cases.

The problems I found in C++ is the programming language itself wont seem to ... let me navigate between functions in that language. I'm not sure if that's a design flaw or how the language was intended.

When I tried finding out information about the error on like Stackoverflow (a lot of those people just tell me that something works, without really telling me why or how something works) Diaspora is even worse about some things, as they seem to only have experience with programming better calculators rather than video games.

Not that calculators aren't important. But in all my programming experience, I don't really think I needed algebra, calculus, or trig once. People say that to try to freak you out of making video games it seems like without understanding there are simpler ways to do things.

But I haven't needed to solve for the color pink squared yet, and don't plan on starting. (A riff on Khan Academy.)

But going to take another dig at C++ and see if I can resolve it myself.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: With or without a class?
« Reply #3 on: March 02, 2017, 07:22:16 AM »
The problems I found in C++ is the programming language itself wont seem to ... let me navigate between functions in that language. I'm not sure if that's a design flaw or how the language was intended.

What does "between functions" mean? The way function (also called procedure) works is that you call it and the execution moves to the function and runs through it, with optional if-else structures etc. and returns from the function in some point. However if you really want to "break" the function to smaller pieces you can do that with infamous 'goto' command in C++ as well. I get that you are new to programming. It helps to read some kind of generic book about programming to understand it better (which by the way I never did).

SarahW

  • Newcomer
  • Posts: 33
  • Karma: +0/-0
    • View Profile
    • Email
Re: With or without a class?
« Reply #4 on: March 02, 2017, 11:38:10 PM »
For the purpose of what I mean:

Code: [Select]
class Greet

  def hello
    puts "Hello World"
    g = Greet.new
    g.wave_hand
  end
 
  def wave_hand
    puts "* Waves hand."
    abort
  end

end

g = Greet.new
g.hello


In other words, each defined section has a different purpose, so as not to overcrowd singular defined sections within a class.

As suppose to operating outside of a class, where a game loop is impossible. Believe me, I've tried.