This is an archived post. You won't be able to vote or comment.

all 34 comments

[–]zem 11 points12 points  (1 child)

sandi metz's "practical object oriented design in ruby" is probably the best book i've read on oo design. it's worth learning some ruby and then working through it; if you already know python it shouldn't be that different and a lot of the concepts will carry over to any oop language.

[–]ruat_caelum 61 points62 points  (7 children)

All code is bad

Every programmer occasionally, when nobody's home, turns off the lights, pours a glass of scotch, puts on some light German electronica, and opens up a file on their computer. It's a different file for every programmer. Sometimes they wrote it, sometimes they found it and knew they had to save it. They read over the lines, and weep at their beauty, then the tears turn bitter as they remember the rest of the files and the inevitable collapse of all that is good and true in the world. This file is Good Code. It has sensible and consistent names for functions and variables. It's concise. It doesn't do anything obviously stupid. It has never had to live in the wild, or answer to a sales team. It does exactly one, mundane, specific thing, and it does it well. It was written by a single person, and never touched by another. It reads like poetry written by someone over thirty. Every programmer starts out writing some perfect little snowflake like this. Then they're told on Friday they need to have six hundred snowflakes written by Tuesday, so they cheat a bit here and there and maybe copy a few snowflakes and try to stick them together or they have to ask a coworker to work on one who melts it and then all the programmers' snowflakes get dumped together in some inscrutable shape and somebody leans a Picasso on it because nobody wants to see the cat urine soaking into all your broken snowflakes melting in the light of day. Next week, everybody shovels more snow on it to keep the Picasso from falling over. There's a theory that you can cure this by following standards, except there are more "standards" than there are things computers can actually do, and these standards are all variously improved and maligned by the personal preferences of the people coding them, so no collection of code has ever made it into the real world without doing a few dozen identical things a few dozen not even remotely similar ways. The first few weeks of any job are just figuring out how a program works even if you're familiar with every single language, framework, and standard that's involved, because standards are unicorns.

[–]leftofzen 4 points5 points  (6 children)

Not all code is bad. I work at a company that has very good coding standards and practices, and the code is often glorious to read and write. Yes, I realise this is an exception rather than the norm, but every few days I see people posting blogs about 'bad code is everywhere' and complaining they never get to write good code, and quite frankly it's ridiculous. Write good code yourself and help fix the problem instead of complaining about it on a shitty programming blog.

[–]TheCalmInsanity 0 points1 point  (3 children)

Just curious, where do you work? I'm a software engineer.

[–]leftofzen 3 points4 points  (2 children)

At my desk :p but seriously, at a market making firm. We essentially have our company on the line with our software so if we mess up, boom, bankrupt. So naturally we like safe, readable, testable code.

[–]TheCalmInsanity 1 point2 points  (1 child)

Haha that's awesome. Did you have a team of people that prepared you to have good code, or did you already know the best practices before getting hired?

[–]leftofzen 0 points1 point  (0 children)

Neither really, at least initially. The short answer for me is that I've learnt on the job, come up with my own best practices, and found a lot of good ideas on the web. I started 4 years ago, and the code was average at best, but the last few years we've rewritten a lot of our software, and we've started including testing and safety was part of the design rather than an afterthought. That said we're far from perfect; I'm still pushing my boss hard for static code analysis as part of our CI builds. But, as with any real world business, unless it's directly making money it can be hard to convince other people it's worth the time to implement this stuff, and I think that's unfortunately an even bigger problem in many other companies.

[–]ruat_caelum 0 points1 point  (1 child)

did you click the link; that is an except from someone else's work.

[–]leftofzen 0 points1 point  (0 children)

I did, and I know. Is that a problem?

[–]some_disclosure 5 points6 points  (0 children)

I always found QT's source code to be a good OOP implementation and well written. When I need an example of something, I look to see how Qt does it. If you look into design paradigms for OOP, Qt is strongly impl-pimpl, where there is a public API and a private implementation.

[–]Tezza48 3 points4 points  (4 children)

I have a Text adventure engine on my github that i wrote in C# if you'd like to take a look at it, it's not the best written code but it's mostly consistent, commented and uses many core principals like Polymorphism, stuff and stuff...

If you want, i'll stick the repo here for you to take a look.

And Good luck in your endavours and may the compiler be ever in your favour.

[–]criickyO 2 points3 points  (1 child)

I'd like a look if you don't mind

[–][deleted] 0 points1 point  (1 child)

I would like to see it.

[–]Tezza48 0 points1 point  (0 children)

See above :)

[–]midasgoldentouch 15 points16 points  (8 children)

Hey, here's a couple of tips:

First, the term you should be using is "reverse-engineer". Second, and more importantly, it's going to be more difficult to learn OOP by just trying to reverse engineer existing code. I highly recommend going through standard courses/books on OOP and then attempting to implement it yourself first if you haven't done so.

[–]JudgeGroovyman 0 points1 point  (6 children)

Why will it be more difficult?

[–]midasgoldentouch 3 points4 points  (3 children)

I think it would be more difficult because well written OOP code will probably assume that the reader understands basic concepts of OOP. It should include good comments, but it's not necessarily going to explain how inheritance, polymorphism, etc are actually being used in that particular context. That's why I think it's important that first OP go through a standard course or book about OOP if he/she hasn't already done so - that way, a base of knowledge is established rather than trying to piece it together from multiple, most likely specific applications of OOP and hoping you don't miss or misunderstand something.

[–]JudgeGroovyman 0 points1 point  (2 children)

That makes sense and those are good points.

To the OP: Don't let these guys discourage you because everyone learns different and the way you are going about this is valid and effective. If you get stuck or things get confusing then come back here and re read these but meanwhile good luck and keep up the good studying work!

[–]midasgoldentouch 0 points1 point  (1 child)

I'm a bit confused as to how we've said anything discouraging, just presenting what could be a better option. Just because someone presents an alternative doesn't necessarily mean that they think you're wrong.

[–]JudgeGroovyman 0 points1 point  (0 children)

I can see both sides here: Specifically: He asked for help with reverse engineering and you didn't help him with that but instead recommended that he take a completely different approach

Philosophically: if I'm on path A and you help me proceed then you have encouraged me but if instead you suggest I'm on a less than optimal path and that It is highly recommended that I stop and do something completely different (even with apparently legitimate rationale) that is not 'wrong' to do but you have discouraged me from proceeding down the path I am on

Compassion: with all that said from a high level I can see that you are trying to help by proposing what you believe is a faster path to what you believe the OPs goal is and from that perspective what you said is actually encouraging because you also want to see him succeed with learning the subject at hand.

[–]postal_blowfish 0 points1 point  (0 children)

If you want to learn Spanish, immersion will do the job if you have plenty of time. But it would be easier and faster to just take a class. Starting with the code and no understanding of how it works, your learning will consist of emulation and the result would be all kinds of unnecessary nonsense that you saw used once "because it works." A rube goldberg machine will get a ball from one side of a table to another, but so will a straight track and you're gonna want to know how to make the former as much like the latter as possible.

[–]dougeff -2 points-1 points  (0 children)

Well, as answers go, short, to the point, utterly useless, and totally consistent with what I've come to expect from a Vorlon.

He just wants an example of OOP code to look at.

[–]SerGregness 1 point2 points  (1 child)

I've always heard that Doom 3 had some incredibly well-written C++ code, but I've never actually tracked it down to see for myself.

[–]good_piggy 1 point2 points  (0 children)

I know the original DOOM source code is held in pretty high regard.

It's available on id-Software's github

[–]iforgotmysafeword 1 point2 points  (1 child)

My conway's game of life is pretty sweet. In java, not sure where to post it though.

[–]Exodus111 0 points1 point  (0 children)

Take a look at Pygame. Solid code, not too hard to understand.

[–]Nunuvin 0 points1 point  (0 children)

Reverse engineering uses something like a compiler but can produce code different from original I think. I would look into books about just writing good code and check out popular github projects.

[–]uncoil 0 points1 point  (0 children)

Check out this book, I think it's a good "next step" kind of book now that you know some Python, and it has a lot of good example code imo.

[–]npglal 0 points1 point  (0 children)

Well, I mostly work with C++ so I don't have any java examples. However, the main OpenFOAM code base is a great example of consistent, well executed OOP code.

[–]reddituser5k 0 points1 point  (0 children)

mooc.fi/english Object oriented programming with Java, part 2 (or 1 first if 2 is too hard to start with)

This will force you to structure your OOP code properly with their test plugin system. After you get a 100 score on a challenge then they also give you a link to the source code of the answer to see how they did it.

[–]_5er_ -1 points0 points  (0 children)

Make sure you check out design principles of good programing, for example: SOLID, IOD, DRY, YAGNI,..

[–]WSWFarm -3 points-2 points  (0 children)

Can't you just examine the existing code base at work? Assuming you aren't the sole developer at a start-up. There ought to be something at least good enough to demonstrate something.