• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this good a programming practice?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am relatively new to programming concepts in relation to "good programming practice".

Is it a good programming practice to override a method that refers to the same method. 
What I am trying to do is to write a method with different primitive type parameters that does the same thing.
This is what I mean, consider the following code.

These are my practice programs. 



 
Bartender
Posts: 572
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like it would be much easier to do this in the opposite direction -it is much easier to determine whether a string is a palindrome than it is to determine whether an integer is palindromic (it is a simple one-line do-loop, with no need to call other methods). So the version that takes an int argument  passes the String representation of the integer to the version that takes a String argument.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an acceptable practice. One thing to watch for in your case is that Integer.parseInt() could throw an exception.

Also...
I think you need tempPalidrome.
 
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

First, get rid of the habit to do painting/styling/decorating and other similar kind of stuff in your code. That simply makes your code unreadable. Don't get me wrong - i'm not saying the comments are bad, they are bad when they are used in a wrong place and at a wrong time (which is the case in most cases what we see here on Ranch).

  • Comments should be written in the form of 'JavaDocs' when the components of the class are tempted to be re-used across the bigger project or other projects. Search for how to write JavaDocs.
  • Comments should be written in the form of 'very short and descriptive' when there is a need to explain some extra bits which you can't easily express in a code.

  • An example of second bullet point:
    Any other forms of comments used in the code quite often turs out to be useless. An example of such:

    After all, you should aim to write expressive code, that means: descriptive class names, variables names, method names. So all these would reveal code intention, so it wouldn't require you write obvious things once again just in the actual comments form.

    That was an intro, now about your actual question.

    First. You don't override that method. You can override only inherited method if it isn't static. What you have here, is overloaded method, and it is perfectly valid (in general) case to do so, same as many methods in the Java API does that - overloads each other, so they could accept different parameters or different amount of parameters or both.

    Second. Think a little about it. What if somebody would pass an argument "1234Frankie"? What would happen? Something not what you'd want it to happen, right?
    Now think a little if you'd have that method implemented otherway round, regardless if it is a integer passed, or string, you'd treat it as a string. That would work in both cases probably.

    Let's see what others think here.
     
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is probably only one sort of comment worse than those:-
    int count = 3; // initialise count to 3
    That tells you only what you already know, viz. that counting starts at 3, and doesn't tell you what you really want to know, viz. what happened to 1 and 2.
     
    Frankie Law
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Awesome!!! Thank you for all the inputs here.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Our pleasure to help
     
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just want to point out that checking if an integer is a palindrome can be done solely with integer arithmetic operations. No need to convert to string then use string/substring methods to check.

    If you want a better challenge, figure out how to implement isPalindrome(int number) by using only these integer operators: * / % +

    Here's some code to get you started:

    You have to provide the ??? parts to make this method work.

    Hint: The ??? on line 4 can be implemented in as few as three lines of code, including a closing brace, }, on a line by itself.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!