Over complicating code

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2851 Views - Last Post: 11 May 2014 - 10:53 AM

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Over complicating code

Posted 08 May 2014 - 01:16 AM

Lately my professor has been telling me that I have been over complicating the projects, even though the end results meet all of the requirements. I was just wondering if I could get fired for over complicated code out in the field (not THAT complex, but at the same time not that clean and clear...) even if it does what its supposed to do. Also, are there any tips to not over complicate things? I know that might some like a dumb question since it can just be the trait of the person, but I want make life easier for me and the people reading my code, since maintainability is very important in software engineering.

Is This A Good Question/Topic? 0
  • +

Replies To: Over complicating code

#2 TgaJava  Icon User is online

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 58
  • Joined: 28-April 14

Re: Over complicating code

Posted 08 May 2014 - 01:38 AM

During my programming experience I often encounter such a case. Like you do, I also complicate code. But I complicate it in order to have lesser lines of code for a lot of work. For sure there is no clear boundary between complicated and non-complicated code. Thus just you should have to manage the tradeoff among different aspects and build a good program.
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 732
  • View blog
  • Posts: 3,178
  • Joined: 03-December 12

Re: Over complicating code

Posted 08 May 2014 - 02:46 AM

*
POPULAR

The only advice I can give,

Aim for the most readable code. You will thank yourself if you have to come back to it and the maintenance guy definitely will.
Was This Post Helpful? 6
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,457
  • Joined: 12-December 12

Re: Over complicating code

Posted 08 May 2014 - 03:19 AM

It would have been helpful of your professor to provide you with one or two examples of what he considers over-complicated code. However,

Quote

not THAT complex, but at the same time not that clean and clear...

you already seem to recognise where you have some issues, which is half the battle.

The only general advice I have is to:

  • Use appropriate data-types and data-structures
  • Recognise repeated code that could be broken out into methods or placed in a loop
  • If a method is, and will only ever be, used once (or twice), consider putting the code back inline
  • Try to avoid methods that only consist of one or two lines
  • Simplify intricate conditionals (lots of and's and or's) by using local variables to store intermediate results
  • Remove comments that don't add anything to the code.

Concerning the last point, the clearer the code (and the more descriptive the variable names) the fewer the comments that need to be added. This is the payback!

In fact, the fewer the comments we need to add is a good measure of how un-complicated our code is.
Was This Post Helpful? 4
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,457
  • Joined: 12-December 12

Re: Over complicating code

Posted 08 May 2014 - 03:30 AM

View PostTgaJava, on 08 May 2014 - 08:38 AM, said:

I also complicate code. But I complicate it in order to have lesser lines of code for a lot of work. For sure there is no clear boundary between complicated and non-complicated code.

I have to comment on this, in that reducing the number of lines of code should never be a goal, especially when it adds complication.

Clean, and improve, the code. This will often have the side-effect of reducing the number of lines.
Was This Post Helpful? 3
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 732
  • View blog
  • Posts: 3,178
  • Joined: 03-December 12

Re: Over complicating code

Posted 08 May 2014 - 04:45 AM

I believe andrewsw also has either a tutorial or a blog post about refactoring that talks about these issues.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,457
  • Joined: 12-December 12

Re: Over complicating code

Posted 08 May 2014 - 04:48 AM

That's actually AdamSpeight2008, Refactoring
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9390
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: Over complicating code

Posted 08 May 2014 - 07:48 AM

View Poststreek405, on 08 May 2014 - 03:16 AM, said:

...
I was just wondering if I could get fired for over complicated code out in the field (not THAT complex, but at the same time not that clean and clear...) even if it does what its supposed to do.
...
but I want make life easier for me and the people reading my code, since maintainability is very important in software engineering.

Typically the first doesn't allow for the former. Have I seen folks get fired for that specific thing, no... but typically that is just one part of a larger picture of issues levied against the coworker that would ultimately end up getting canned.

If you happen to write two hundred lines of code into a series of convoluted recursive functions that only take up sixty lines I would pitch a fit having to come back in to shore up documentation, bug fixes, or enhancements.

Certainly there is a flow to programming, and sometimes elegant solutions are great, but I realize I have a big ol' team that has to trudge through my work and I would rather minimize their time to interact with my code in favor of getting their work done quickly. It's about having a degree of team spirit and a fewer shades of self serving needs for silly metrics.

Of course all of this is relative to the work place, what you actually mean by 'over complicated code', and so on.
Was This Post Helpful? 2
  • +
  • -

#9 TgaJava  Icon User is online

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 58
  • Joined: 28-April 14

Re: Over complicating code

Posted 08 May 2014 - 08:28 AM

View Postandrewsw, on 08 May 2014 - 03:30 AM, said:

View PostTgaJava, on 08 May 2014 - 08:38 AM, said:

I also complicate code. But I complicate it in order to have lesser lines of code for a lot of work. For sure there is no clear boundary between complicated and non-complicated code.

I have to comment on this, in that reducing the number of lines of code should never be a goal, especially when it adds complication.

Clean, and improve, the code. This will often have the side-effect of reducing the number of lines.




If I have a lot of variables to store some related data and if I assign each variable a value using a single line, then the no of lines I use would be equivalent to the no of variables. To solve this I use arrays instead of many variables and I may be able to assign/set values to the array elements using a loop.(if there is some pattern ). Using the variables is easy to understand but when using arrays at least some little amount of complexity is added.

If I have 1000 lines of code initially, and later I may find out or understand some repetitive nature in my code and I may convert it into a looping or recursive statement which may reduce the code to 200 lines but adds some complexity.

So I argue that for the sake of little/manageable complexity, I may choose to have lesser lines of code. Later for maintenance instead of looking a long list of code you will only need to read some less amount of code.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Over complicating code

Posted 08 May 2014 - 09:18 AM

Fighting complexity is actually one of the primary jobs of the programmer. Just getting it to work, you can throw anything at it until it sticks. Getting it to work cleanly, concisely, and maintainability, that's the real challenge. Simple IS hard.

Da Vinci once said "art is never finished, only abandoned." Code is art and usually works like that. Once it works, you can tweak it forever, changing a bit here, a function there, making more or less classes/functions/variables. Sometimes refactoring can go the wrong way. The art of programming is finding that magical place were not one line could be added or taken away: good luck with that.

Much of this comes from experience. The more you code, the more you attempt to clean your code, the better you'll get at it.

View PostTgaJava, on 08 May 2014 - 11:28 AM, said:

...So I argue that for the sake of little/manageable complexity, I may choose to have lesser lines of code.


Code, or it didn't happen. ;)
Was This Post Helpful? 4
  • +
  • -

#11 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 576
  • Joined: 10-March 13

Re: Over complicating code

Posted 08 May 2014 - 08:25 PM

Thank you all for your input, it was very helpful and I will take it to heart!
Was This Post Helpful? 2
  • +
  • -

#12 depricated  Icon User is online

  • I was alone. I was thinking of you.

Reputation: 836
  • View blog
  • Posts: 2,696
  • Joined: 13-September 08

Re: Over complicating code

Posted 09 May 2014 - 04:40 AM

Coding is like music. Too simple and it's not what you want to hear, too complex and it's just a cacophany. It may get the job done, but as has been said - simply getting it done isn't the goal, the goal is to get it done simply. That you care enough to post about it suggests that you're on the right track. AdamSpeight's tutorial that andrewsw linked is great - if you're interested in some reading to help you along, there was a book I took a lot from and I'd highly recommend. It's called "Clean Code" by Robert C. Martin. It emphasizes simplicity over complexity and explains ways to approach problems that help keep the solution simple, readable, and maintainable. I've read a heap of books, but none have stood out to me quite like this one, and if the critique is that your code is too needlessly complex, this may be just what you want.

Another thing I've done is occasionally post my code on here and ask people to critique it or show me what I could do to make it better. The people here are awesome like that, and there's no judgment in it. Start up a thread with a sample of your code and we'll help you see new ways to build it.
Was This Post Helpful? 1
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Over complicating code

Posted 09 May 2014 - 06:46 AM

View Postdepricated, on 09 May 2014 - 07:40 AM, said:

post my code on here and ask people to critique it or show me what I could do to make it better.


This is totally my favorite game. I refactor compulsively. It's better than solitaire.

I'd second Clean Code. Also, if building a library, none would be complete without Code Complete. Or, The Pragmatic Programmer, which is much shorter and more fun.
Was This Post Helpful? 1
  • +
  • -

#14 depricated  Icon User is online

  • I was alone. I was thinking of you.

Reputation: 836
  • View blog
  • Posts: 2,696
  • Joined: 13-September 08

Re: Over complicating code

Posted 09 May 2014 - 08:40 AM

Just gotta second baavgai's mention of Code Complete and the Pragmatic Programmer - those are great books too.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7877
  • View blog
  • Posts: 13,376
  • Joined: 19-March 11

Re: Over complicating code

Posted 09 May 2014 - 09:14 AM

While I agree completely with the need for simplicity and clarity in writing, it's also important to remember that you can't be a perfectionist about it. Nobody wants a program that's going to be perfect, if it ever gets done. At some point, it's necessary to bang the stuff out.
This is sort of why I tell newbies to write lots and lots of code, and to work really hard on simplicity when they're writing on their own time: when you're writing on the clock, you need to be able to generate code without dithering about it, and it's a lot easier to write acceptable code the first time if you've trained your mind from the start to seek simplicity.

Evil refactoring trick (don't tell anyone I told you this!): If there's an area that you're going to want to go back and refactor, hide a subtle bug in there. It's usually a lot easier to commit changes if there's a reported bug that you're fixing.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2