9 Replies - 775 Views - Last Post: 13 May 2012 - 11:08 PM

#1 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 05:42 AM

Hey people,
Recently every time I'm working on classes' methods I have something bugging me really badly:

Let's say I'm working on a method that has 3 steps in it:
1) Read a string
2) Check the length of the string
3) Print the string and length on the screen

Should I write (probably a private) method for each step so the code is nice and clean?
(I don't do this, but really want to. it can change the code to be so organized and well-designed!).

Or should I just write everything in 1 method, because I know that I'm not going to use one of those steps again (someplace else)?

So to make it simple as possible:
Do you create a method for a behavior or a repeating behavior?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Do you create a method for a behavior or a repeating behavior?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 06:22 AM

Don't try and over-engineer simple code. It's really hard to mess up something so trivial. Most languages have built-in libraries with method for I/O, string processing, etc., so there's really no need to add fluff:

Java:

class Main
{
        public static void main (String[] args) throws java.lang.Exception
        {
                Scanner kbd = new Scanner(System.in);
                String s = kbd.next();
                System.out.println("string: "+s+", length = "+s.length());
        }
}



C++:

#include <iostream>
 
using namespace std;
 
int main() {
        string s;
        cin >> s;
        cout << "string: " << s << ", length: " << s.size();
        return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 06:43 AM

View Postblackcompe, on 12 May 2012 - 06:22 AM, said:

Don't try and over-engineer simple code. It's really hard to mess up something so trivial. Most languages have built-in libraries with method for I/O, string processing, etc., so there's really no need to add fluff:

Java:

class Main
{
        public static void main (String[] args) throws java.lang.Exception
        {
                Scanner kbd = new Scanner(System.in);
                String s = kbd.next();
                System.out.println("string: "+s+", length = "+s.length());
        }
}



C++:

#include <iostream>
 
using namespace std;
 
int main() {
        string s;
        cin >> s;
        cout << "string: " << s << ", length: " << s.size();
        return 0;
}



It's just a simple example, and I thought (and still think) it's obvious. :plain:
Why do you refer to the example rather then to the question??
Was This Post Helpful? -1
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 07:05 AM

Ha, I just felt like coding. Anyway, your right, I didn't read your question well enough. For your simple example, my answer still stands, no need to go out of the way to overcomplicate things. But generally, reusable code should be modularized, i.e.: encapsulated in a method. Try to use your best intuition to choose when you should abstract some code into a method. Like I said, there's no need to create a method for finding the length of a string if one already exists.

int stringLength(char* str){ return strlen(s); }



Are you serious?

If you were to write code for say.... a mathematical function that is likely to be used in a lot of places, then abstract it.

For your example, it seems like that's something you'd do in a loop in a method that's doing some console I/O. I wouldn't put them in a separate method. It may be what you consider reusable, but that code so trivial why even bother. It's sort of a guessing game.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 08:55 AM

I agree its trivial in the context of this example, but I'd still do it in a real fullsized app, for consistancy sake.

I can imagine an application or library whose purpose is dealing with the string input. In the real world, which is where these simple examples are really aiming towards, there would be lots of methods. Some are trivial some are not. But consistancy in code makes it more easy to deal with. So I would probably expect something like this

int CountLetters(char* str) { ... }
int CountNumbers (char* str) { ... }
int CountVowels (char* str) { ... }
int CountConsenents (char* str) { ... }
int CountSpecials (char* str) { ... }
int CountTotal (char * str) { ... }


If you start breaking from style and saying to yourself "This is trivial, this isn't: This I'll just write as 3 lines in the other method... this I'll make a method for.." Then you make it just a little more difficult to navigate for the next team member looking at it or even yourself when you re-visit this application after a year.

In the end it really is a style choice. So do what feels natural to you and is in keeping with the guidelines of your work environment.
Was This Post Helpful? 1
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 02:33 PM

> Do you create a method for a behavior or a repeating behavior?

There isn't a hard rule. And it's important to understand that there shouldn't be such a rule. It's all too often that I come across code that on paper follows great design and coding principles, but in principle is more challenging to read (even within technical constraints). Obviously there are technical requirements, like if you're trying to dance around a compiler's optimizer, you'll perhaps construct some awkward code. Fine.

But generally speaking, you need to develop a judgment for whether your code communicates effectively. You'll develop that from maintaining codebases, working with other people's code, and in increasing the complexity magnitude of your projects.

Rather than letting this issue eat away at you, I would just continue to program, and when you review your code, think about how you could have improved it. Keep that attitude and you'll do fine.
Was This Post Helpful? 2
  • +
  • -

#7 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Do you create a method for a behavior or a repeating behavior?

Posted 12 May 2012 - 07:57 PM

Despite the fact that you guys really exaggerated because of just an example, I got the answer, I guess. so thanks. I guess next time I should give you an algorithm to manipulate a robot to satisfy you, rather then something simple :mellow:
Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Do you create a method for a behavior or a repeating behavior?

Posted 13 May 2012 - 06:58 AM

pokiaka: Hey, you got our best answers. I'm sorry if they weren't what you were looking for, but designing code isn't a clean and cut science. It's something you get better at with experience, and as someone said, using other people's codebases. There are heuristics and patterns that you can study to get better though. If you want to investigate the topic further there are plenty of good books on code design and refactoring. I highly suggest Clean Code, Design Patterns by GOF, and Refactoring: Improving ... by Martin Fowler.

We didn't exaggerate on anything. We simply said, given the example, there is no need to abstract any of those steps because it's not complex code. If you want to embrace keeping clean code, then by all means, modularize your steps. It may help build the foundation you need to get better. Whatever works.
Was This Post Helpful? 0
  • +
  • -

#9 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Do you create a method for a behavior or a repeating behavior?

Posted 13 May 2012 - 09:53 AM

View Postblackcompe, on 13 May 2012 - 06:58 AM, said:

pokiaka: Hey, you got our best answers. I'm sorry if they weren't what you were looking for, but designing code isn't a clean and cut science. It's something you get better at with experience, and as someone said, using other people's codebases. There are heuristics and patterns that you can study to get better though. If you want to investigate the topic further there are plenty of good books on code design and refactoring. I highly suggest Clean Code, Design Patterns by GOF, and Refactoring: Improving ... by Martin Fowler.

We didn't exaggerate on anything. We simply said, given the example, there is no need to abstract any of those steps because it's not complex code. If you want to embrace keeping clean code, then by all means, modularize your steps. It may help build the foundation you need to get better. Whatever works.


Well, what I know is that I'll just go with the way suits me the best.
And you exaggerated because you fail to understand what a good example is.
When I gave it, I assumed you would know to answer my question as if you wouldn't have those basic language functions, and as if you could actually abstract the method.
Giving you a method that I'm currently working on will be irrelevant and hard to understand without the context, so I'll probably need to tell it to you too, and as you can see - bad example.

But it doesn't matter, I know what I'll be doing from now.
Thank you all.

Was This Post Helpful? 0
  • +
  • -

#10 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: Do you create a method for a behavior or a repeating behavior?

Posted 13 May 2012 - 11:08 PM

for those people that are actually interested in receiving help:

a personal method I like to use is to write your code in such a way that it could easily be cut and pasted into a new method body and be replaced be a method call. of course, this is fairly abstract and not applicable in every instance; although i honestly believe it will end up making your code easier to follow.

another situation to create a new method is when a section of code could be made more efficient by someone more knowledgeable/with more time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1