top | item 20587704

(no title)

MRD85 | 6 years ago

I'm a second-year CS student that is still learning in this realm. Is there a general way that most developers would rewrite lines 5261-5284? My assumption is I would look to find some rules I can apply to simplify the code but I'm also aware a CS course is a bit of a bubble and what I've learnt so far might not be the way things are handled in industry.

For example, line 5284 has a 8 inequality operators checking every 4th element (140. 144, 148,...). A single "blocks[1][y][x] % 4 != 0" would remove them all. There also appears to be 3 main segments in that huge block of code (111-118, 119-126, 137-168) which would allow it to be simplified.

A second question: Why is there no comments? Is this common?

discuss

order

munmaek|6 years ago

> Why is there no comments? Is this common?

It's frighteningly common, and more likely if only 1 developer is working on something. CS students comment far too much, but I would expect a monstrous chain like this to have at least -some- documentation.

mindfulgeek|6 years ago

One of the best refactoring tools is naming. Arlo Banshee wrote and interesting article about the subject some time ago (http://arlobelshee.com/good-naming-is-a-process-not-a-single...). This is a really long post, but the overall theme is iterate over code until everything has a name. To apply this to your question, the first thing you would do is take those lines, put it in a function with a bad name like “doesSomething” then keep iterating until someone can actually read the code and it makes sense.

In the beginning of learning to program it’s easy to get stuck on the “clever” side of programming, but a lot of the time it can remove readability. The earlier you learn this, the more your future coworkers will appreciate you.

Good luck!

jrgilman|6 years ago

> A second question: Why is there no comments? Is this common?

Man I remember being this naive once :(

Cthulhu_|6 years ago

My first major project (at an internship) had all the comments on every file (even getters / setters, and it even had getters/setters because someone told me to). Interestingly enough it was a project that had to read static analysis tools' output and combine it into a single report / webapp, so I was very aware of all the output that was reported and all the niggly little things that these tools (think PMD, CPD, FindBugs, etc) pointed out. Including how a getter wasn't commented.

realSlavojTrump|6 years ago

As mindfulgeek mentions, doing some sort of extract method refactor to slop a name onto it can be a good first step. Then do some sleuthing thru other number name pairings, inserting names in place of the numbers, until you have a solid grasp of what the names should actually be. Feathers in Working Effectively With Legacy Code brings up a concept called seams... u can insert a seam to map to objects and then do object manipulations with polymorphism.

Iirc that particular bit of code is the power logic. You can file an issue on my fork if ys like me to write more on this when im not on a broken phone.