Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 328 329 [330] 331 332 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907883 times)

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4935 on: September 17, 2013, 10:37:34 am »

The problem with not using 2 letter variable names is that you keep misspelling them :v
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4936 on: September 17, 2013, 12:12:43 pm »

Reminds me of the Fortran program I'm currently tasked with altering to run on a GPU using some compiler accelerator magic.  I can understand that the language has its roots in a standard from apparently 1957 if Wikipedia is to be trusted, but it seems that nobody who still uses it has moved on past the bad habits its early versions instilled.  Variable names longer than 3 characters are exceedingly rare.  Gotos are pretty common, and the idea of breaking a program up into multiple source files seems to be a foreign concept (or even breaking them up into functions / subroutines).

It's not even that this particular program is that bad, as far as Fortran programs go.  Every last one I've seen is like this, despite the fact that the fixed column format standard went away 20 years ago and you're quite free to define variables with names longer than 6 characters.

I am so glad I rarely have to touch Fortran...
Logged
Through pain, I find wisdom.

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4937 on: September 17, 2013, 01:51:51 pm »

Fortran itself actually isn't that bad. It's just that it tends to be used by aging scientists who cling to their bad habits out of spite.
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4938 on: September 17, 2013, 04:14:10 pm »

Yeah, most of the Fortran code I've touched was written by senior university professors or in one case one of the senior researchers at a government lab.  I'm sure it's just old habits dying hard.

What's perplexing is that this particular program was written by a graduate student, and in Fortran 77 standards at that.  I'm hoping his adviser is one of those aging scientists and just handed him most of the code, because if new programmers outside of the computer science department are being taught to program this way it's a very sad thing.

Not that all that many programs written by even the graduate students inside the computer science department are formatted all that well... something that drives me up the wall.
Logged
Through pain, I find wisdom.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4939 on: September 17, 2013, 05:08:51 pm »

To be honest, most graduate programmers from University are shockingly bad. Most don't program as hobby or anything, it's "just a job". A way for them to get money. And this engineering FAQ sums it up prety well: "When told that work is work and spare time is not for engineering, I am concerned that the candidate's world view is not really that of a successful engineer."

From what I've heard it used to be there was a purging, Computer Science had massive drop out rates in comparison to other courses because so many people just couldn't hack it. But then they started trying to ease students into it. No more learning about pointers and memory first semester, that kind of thing. Some would say all they accomplished was making it so the less inclined towards Computer Science were no longer being culled from the herd, and result was a devaluing of what having a Computer Science degree meant.

Personally I think the right step would be to bring focus on writing readable, testable code, and refactoring code to be readable and testable, as vital parts of the programming modules. Knowing how and why to encapsulate and polymorphism is much more useful than memorising the definitions of those words. Definitions form the common language, they shouldn't be directly tested but simply a part of the vocabulary of the tests.
« Last Edit: September 17, 2013, 05:23:51 pm by MorleyDev »
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4940 on: September 18, 2013, 03:05:42 am »

I have a background in physics even though I work as a "real programmer", and I can confirm that habits are inherited along with old code from professors. I took a personal interest in learning more programming essentials on the side so I avoided the worst pitfalls, but most of my fellow students didn't and just worked off of the samples they were given. I still get calls from some of those that ended up in academia to help out when they've backed themselves into a corner.

Sometimes there are so many dirty tricks, that probably made sense on a PDP-11, that it's impossible to figure out what makes the code work. Like it depends on some fluke in the chip logic to generate the expected numbers.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4941 on: September 19, 2013, 07:40:06 am »

For this thing http://pastebin.com/5yb3KW3W

Would making a loop that uses an ifstream and only stops when the inputted string matches strStartRead, then proceeds to read what I want it to work? If so, is there a better way to do that?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4942 on: September 19, 2013, 08:19:29 am »

I'd recommend using a single map<string, int> to store all your datas instead of hardcoding them into lots of different variables; it's a tad slower, but much more flexible. If you absolutely need the speed, you can use an int[] with enums as keys instead, and keep a string-to-enum conversion table; it's just as fast as your method, but still more flexible.
Then your file format could look like this:

Code: [Select]
bFarms 7
bMines 4
sFood 30
sCoal 40
tMining 1
tSmelting 1

Then the order of data in your savefile is completely irrelevant, and you don't need to code two hundred lines to load a single savefile.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4943 on: September 19, 2013, 08:26:32 am »

But if I did that, how would I distinguish between, say, the food variables of different empires? Would I necessarily have to make every single string unique?
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4944 on: September 19, 2013, 08:37:52 am »

You stuff all the variables into a class called Empire and make two instances of Empire. That's possible.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4945 on: September 19, 2013, 08:38:27 am »

Oh, you're storing multiple empires in the same file? In that case, you could just separate empires by a single line, say, "#EMPIRE <name>", and begin editing a new empire every time you find such a line.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4946 on: September 19, 2013, 09:01:04 am »

So how would I make values in the Holdings class consist of a string and an int, or would I not be able to use a class with that method? From what I can tell I would be going through all of the data in the file, then creating new str and int variables for each value? How would I do that?
Logged

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4947 on: September 19, 2013, 09:07:41 am »

Code: [Select]
Unable to open SCSI controller 0:[\] , error code: 0x5
Unable to open SCSI controller 1:[\] , error code: 0x5

Uhh, what. This prints when I poll an event from SFML. (sf::Window::pollEvent) At least for the LostFocus event, which happens to be the first one. The string isn't even defined in SFML. It's only been happening recently (today).
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4948 on: September 19, 2013, 09:58:46 am »

So how would I make values in the Holdings class consist of a string and an int, or would I not be able to use a class with that method? From what I can tell I would be going through all of the data in the file, then creating new str and int variables for each value? How would I do that?
No, you don't create new variables for each value. It's probably time to introduce you to the std::map class.

A map is basically a collection of things, indexed by keys. You can put things inside, labeled with keys, or you can search through the collection to find a thing with a certain key attached to it. You can start using maps by using #include <map>. A map type looks like this: map<KeyType, ValueType>. Since you want to store integer values, labeled by strings, you need a map<string, int>. To put a value into a map labeled with a key, you write map[key] = value. To retrieve that value later, just write map[key]. Note that key is of type string, not an int. Since C++ automatically converts C-strings to std::string's, you could write map["bFarms"] = 7.

Code: (Old version) [Select]
class Holdings
{
public:
        Structures EmpireStructures;
        Stocks EmpireStocks;
        Technologies EmpireTechnologies;
        Population EmpirePopulation;
        //...

Code: (New version) [Select]
class Holdings
{
public:
        map<string, int> data();
        //...

Code: (New getHoldings method) [Select]
#include <sstream>
        int getHoldings(ifstream fin, string strEndRead)
        {
                istringstream is();
                while (true) {
                        string key;
                        int value;
                        is.str(fin.getline());
                        is >> key;
                        if (key == strEndRead) break;
                        is >> value;
                        data[key] = value;
                }
        }
« Last Edit: September 19, 2013, 11:18:36 am by MagmaMcFry »
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4949 on: September 19, 2013, 10:16:12 am »

Incidentally, no need to #include <utility>. <map> implicitly includes that file as a header. I think.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward
Pages: 1 ... 328 329 [330] 331 332 ... 796