• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this ok to do? is it done? or should it be avoided?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i noticed in a tutorial the guy put this line at the front of his code

import static java.lang.System.*;

then he didnt have to use System.out.print ....just out.print

is this bad form...do you guys do this?

pitfalls caveats?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caveat: don't do this kind of thing until you know how to write small classes (fewer than 50 lines of code) that are clean and well-factored. Having a lot of static imports in code that is long, messy, and convoluted just makes it more difficult to read. Static imports should be used sparingly, with the main consideration being that the reduction of noise caused by the fully qualified name outweighs the danger of causing confusion and ambiguity.

I only usually use static imports when the code that surrounds the abbreviated names can provide enough context so that it's immediately apparent to the reader that static import was used. Also, they're worth using in contexts where most people are already familiar with those names being statically imported, such as with the assertXXX() methods of the JUnit framework. If you stick to using them context where it is common practice to import them statically, you should be fine. Otherwise, use them with caution and always think about others who would read the code later. Don't just use it to avoid having to type as much. That's just being lazy, and not in a good way.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call me lazy, but when I develop some code, and I need many S.o.println's, first thing I do is to create a method called 'a'

That saves me quite some typing. To test the removal once you're done, first comment out the body.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Call me lazy, but when I develop some code...first thing I do is to create a method called 'a'...


Yes, I'd say that's very lazy.  

At the very least call it 'print' or 'display'.

Winston
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah, that would totally defeat the goal...  
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if you guys are jesting or serious. Create a method to save typing?
In eclipse one just needs to type sysout hit ctrl+space+enter and you are done. I am sure other IDEs have similar shortcuts
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regarding

private static void a(String s, boolean asterisks) {
   if (asterisks) System.out.println ("*****");
   System.out.println(s);
}

is this a method? ...what is it?...what would it do? tried compiling it...nothing happened
i know its bad practice but please explain to a newb...
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What went wrong when you tried compiling that method? It looks all right to me. You must of course call it from inside the same class because it has private access.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey campbell richie

so should the class be called a too?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just asked "If a class contains a method named a, should the class also be named a?"
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought it means it shou‍ld be called “too”.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:regarding

private static void a(String s, boolean asterisks) {
   if (asterisks) System.out.println ("*****");
   System.out.println(s);
}

is this a method? ...what is it?...what would it do? tried compiling it...nothing happened
i know its bad practice but please explain to a newb...



Ok, so you're a newbie but...

1. We'd expect that you tried this out yourself.  I understand that the lack of context would make you a little confused about whether or not this code is a method but it really shouldn't be that big of a confusion. You don't seem to be that much of newbie that you can't even recognize a method when you see one.  If you are, then the answer is yes, that's supposed to be a method, and yes, the method name is a, which is a very poor choice of name as others have alluded to with their tongue-in-cheek and not-so-tongue-in-cheek remarks. As Winston noted, you should use an intent-revealing name instead like print or display if you really want to do something like this. I don't recommend it though, especially with the additional boolean parameter.

2. What the method does is pretty easy to figure out. First, if the second parameter is true it will print five asterisks. Then it will print the string passed in.  That means a call like a("Hello", true) will print *****Hello, and a call like a("Hello", false) will just print Hello.

The reason I don't recommend this kind of thing is that it's lazy in a bad way. First, it doesn't save much typing because of the extra parameter you have to provide. If you used a better name for the method, that saves you even less on typing. More importantly, the extra parameter creates extra noise in the code because the reader is constantly having to parse the meaning of the true/false value. However small of an effort that may be, it's still cognitive load placed on the reader and having your code peppered with calls like this adds up to a lot of cognitive weight. Always give consideration to the reader of your code and make it easier for them to understand your code, not harder.

3. There are no stupid questions around here but there's a limit to the number of obvious ones that people will tolerate you asking. You can only keep asking questions like this for so long. After a point, people will think that you're not putting any effort into your practice and learning and they will start being more and more inclined to ignore you. Practice and get better.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:I don't know if you guys are jesting or serious. Create a method to save typing?


Sure. I do it a lot ... even sometimes in my public APIs (although I try to keep it to a minimum).

Not quite sure I'd go to Piet's length at 'precis' though.

Winston
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:That means a call like a("Hello", true) will print *****Hello

I'm sure Junilu meant it would print:
*****
Hello

Check what is the difference between print() and println().
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that was stupid of me...was late...had a long day...sorry

the a method could belong to any class name i know...can a class dog...have a method dog...just out of curiosity...even if it could i know it would be stupid to name things in this way.

in this little method

private static void a(String s, boolean asterisks) {
   if (asterisks) System.out.println ("*****");
   System.out.println(s);
}

i understand boolean asterisks etc...but where is it getting true or false from?
not to harp on...i get your point about naming methods a
was just looking at this method to see if i could understand it

thanks
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:i understand boolean asterisks etc...but where is it getting true or false from?



The second parameter of the method is of type boolean. And the only possible values of a boolean variable are true and false. So if the caller of the method passes true as the value of the second parameter, then the method gets true as the value of its second parameter. Likewise if it passes false, then the method gets false.

Hope that answers your question.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:yes that was stupid of me...was late...had a long day...sorry

the a method could belong to any class name i know...can a class dog...have a method dog...just out of curiosity...even if it could i know it would be stupid to name things in this way.


No need to apologize, but again, when you ask questions like "Can I do this or that?" it kind of makes you look like you're just waiting for other people to spoon feed you answers. What's keeping you from writing a little bit of code to see for yourself?

Read our wiki page about how to ask good questions (see link in my signature below).
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jon,

I seem to have confused you, which was certainly not my intention. I hope I can clear this confusion.

Some background: a couple of years ago I wrote a Sudoku-program. Now, the part that solved a Sudoku contained some teething bugs, and testing for these was tedious. Among other ways (f.i. single stepping), I found myself typing "System.out.println("...") in many places and that soon became just as tedious. Therefore, I thought of my now infamous 'a' method. Doesn't it save a lot of typing? Actually, I have four versions; the other ones being without the boolean, one with 'Object' as parameter and a version that writes its ouput to a file.

These methods are in some library, and copying these into my code takes only a couple of seconds.

So far the background.

Now, I knew that admitting to such practises would grant me some critique.

1) and indeed, never ever call a method 'a'! And that is exactly why I chose that name.

2) When I'm done testing my code, I tell my IDE to get rid of everything 'a'. Takes some seconds as well. No other people will see this, and so the fortunate reviewer does not have to face the burden of understanding what this 'true' is doing

3) Of course, with an APL-like key combination you can insert a genuine  S.o.p into your code, if you set up your IDE this way. Problem is that the removal may be a little problematic; some genuine "S.o.p"'s are meant to stay in the code.

4) And finally, men of name and fame speak of lazyness and bad habits. And they may be right. But if I must choose between my positive experiences of the past years on the one hand, and a little dogmatic view on the other, what should I value more you think?

Well, let me finish with a piece of text from the CCR band: 'Oh mother, tell your children, not to do the things I've done'

Greetings,
Piet
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, Piet, you should have said so before. I am not ashamed to say I have done such things for convenience in the moment. There's laziness and there's the other kind of laziness that helps us get things done quickly but then fastidiously cleans up and puts away the shims and jigs so that what's left to see is only the nice, professional looking work. That's not lazy at all, it's what all good craftsmen do.  
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:I...was just looking at this method to see if i could understand it


TBH, I've done precisely the same as Piet many times, except that I usually call the method 'show()' or 'debug()'. Indeed, I have the latter in a 'Utils' class, viz:which allows me to print out pretty much anything I like for the purposes of debugging, eg:
  assert Utils.debug("value=%s", someValue);
which I can then turn on with the '-ea' parameter.

It doesn't save any typing, and is not what assert was intended for - indeed, there are probably much better ways of doing it these days -  but it's served me quite well.

Winston
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic