• Post Reply Bookmark Topic Watch Topic
  • New Topic

Level of abstraction of methods in java  RSS feed

 
Nidheesh Krishna
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is mean by single level of abstraction of methods in java? please explain this with an example or give me url which gives complete explanation of this
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give some more context - where exactly did you read this, and what was the source exactly talking about?

It's hard to give you an answer because "single level of abstraction" isn't a common or well-defined concept that Java developers know about, so without context it's hard to understand what exactly that means.
 
Nidheesh Krishna
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
n order to make sure functions are doing “one thing”, it is important that all statements within the function are all at the same level of abstraction. What is mean by this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed with Jesper. There isn't really enough context. Sorry.

Anyway, the best that I can speculate is that it is related to a design recommendation. Or perhaps some sort of best practice (on using methods).... but regardless, without enough context, it is pretty vague.

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi neelesh!
Nidheesh Krishna wrote:In order to make sure functions are doing “one thing”, it is important that all statements within the function are all at the same level of abstraction. What is mean by this?
see this live example-


now see above the method name gives an intention that it would post in the mentioned forum and it is,but we cannot say that it is performing only posting indeed it is doing other stuff too,like opening a codeRanch,click the forum Button and then gettting the required forum and then perform steps required for the posting .
their are plenty of things going wrong in this method-

.the statements inside a method should have equal level of abstraction.see in oops the whole idea is to hide the implementation details and crystal clear the functionality-again our method is breaking this contract
->in our method the first 2 lines let's us know about the implementation of these 2 lines is for opening a website and then go into the forums page.this implementation can be hided if we provide a single openForumPage(String websiteName) in place of these 2 lines.again in the if block opening a editor,creating a post,closing the post and editor object..all these can be made hidden in another method createPost(Forum forum).now if you will check the abstraction level of the first 2 lines with the lines in the if block,then the first 2 lines have higher abstraction level than the latter one because the latter one is revealing more than the former about the implementation details.

askInForums():
openForumPage():
CreatePost():


now compare the new code with the previous one.you will see now the method statements has equal level of abstraction here in the new code and the each method is prforming what it is revealing by its name.


PS--the methods and classes which are not declared here are assumed to perform the same task as intended by their name.

Hope it helps!

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you call that level of abstraction? Why isn't it common‑or‑garden refactoring? Please tell us where you found the term “level of abstraction.”
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Would you call that level of abstraction? Why isn't it common‑or‑garden refactoring? Please tell us where you found the term “level of abstraction.”


yes what i have done is a refactoring,actually i have mistaken the term-"level of abstraction".i have google about it and found it's a concept(or a term) used in the very nice book-"Clean Code".what i have understand is-take an example
suppose we have a three text files say file1,file2,file3, and say they have n1,n2,n3. paragraphs in it.suppose we want to use these files in our method then we have some option stated below:

Case1:

Case2:

Case3:

-----------------
In our Case1 each statement is reading a singal file.i am using a number to imply the level of abstraction here,as at each line it is reading a single file the level of abstraction of all the Three statement is 1 here.
-----------------
In our Case2 the first statement is reading 1 file and second statement is reading 2 files,so level of abstraction is 1 and 2 respectively for the two statements.
-----------------
In our Case3 i have only written a single statement but in actuall it will contain n1+n2+n3 lines 1 for each paragraph.now the first n1 lines is a single task as it can be narrated like a story,after it the n2 lines is another story-so it is another task,similarly the remaining n3 lines is another task.in the first story it would read n1 lines so each line has level of abstraction=1/n1(for understanding say each line will give its 1/n1 part to a story),the another n2 and n3 lines have their level of abstraction-1/n2 and 1/n3 respectively.

so what actually we conclude is in the first case it has all the statements equal level of abstraction.level of abstraction is a concept linked with the amount of work being performed by a method and if i would go deeper inside the method file1(),file2(),file3()..the level of abstraction changes because inside their we have to use another criteria for defining this term like in our third case.
author stated this concept for defining how to refactor the problem in a method if we need to make it perform several task.

this is what i have understand...
if i am wrong at any point please correct me and i would appreciate that.
and Thanks a lot @campbell it really worth reading about this concept on web,atleast for me.added some knowledge in my dataBase...again Thanks..
Kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit-for my previous post]



please dont misinterpret 2nd line with some method.it is actually-

similarly it is for the third case.

Kind Regards,
Praveen.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:"single level of abstraction" isn't a common or well-defined concept that Java developers know about, so without context it's hard to understand what exactly that means.

Single Level of Abstraction Principle (SLAP) is something that I encourage all programmers I work with to understand and always keep in mind. It's a fundamental principle of well-written code.

The canonical example is Compose Method

This code has a single level of abstraction

Go see the original code that the above was refactored from at the Industrial Logic link that you find in these search results: Compose Method
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another example of extracting code to attain a single level of abstraction.

This code has two levels of abstraction:

Line 7 is at a different level of abstraction from the rest of the code in the method. Extracting it out to its own method, you get this:

Line 7 is now at the same level of abstraction as the rest of the code in the method.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's what I had a couple of extract method refactorings before that:

Line 8 and Lines 11-15 were at a different level of abstraction.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nidheesh Krishna wrote:In order to make sure functions are doing “one thing”, it is important that all statements within the function are all at the same level of abstraction. What is mean by this?

I don't like saying that SLAP is about making sure methods/functions are doing "one thing".  Saying that can cause confusion because when you read my last example, the play() method actually appears to do multiple things.

As you can see, getting a SLA often involves Extract Method refactoring. It's really Extract Method that is more about making methods that do just one thing.  SLA is more about getting a uniform level of detail in the same method. You shouldn't have code that's doing detailed calculations with intricate for-loops and what not and then also have calls to higher-level methods mixed in. Mixing abstraction levels causes the reader to bounce back and forth between them and that can impede comprehension of what the code is doing.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often advise people to read their code out loud so that they can *hear* the story that their code is telling. When I do that, it's easier for me and others I'm programming with to identify parts of the code that are at a different level of abstraction. 

For example, with this code:
I would read that out loud as:
Ok, when we start playing, the currentPlayer becomes whoever goes first. Then we draw the board. While the currentPlayer makes his/her move and the game isn't over, the other player becomes the current player. Blah blah blah current player. If the game was won, then blah blah blah currentPlayer, otherwise blah blah blah

Note that I will literally say "Blah blah blah" at the parts where the code says System.out.println(). It's just something I've trained myself to do when I sense I'm suddenly trying to interpret code that's at a different level of abstraction. Then the Extract Method refactoring begins.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:Note that I will literally say "Blah blah blah"

You can even have some fun when you do this. Like when after I saw the animated film "Hotel Transylvania," I thought that Dracula character was so funny that for a while I would use a deep, sinister monster voice and say "Bleh bleh bleh" instead. Just some monster geek humor, you know. To make things a little less boring.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!