Forums Register Login

Codiging Standards

+Pie Number of slices to send: Send
Hello, folks!!
I am using qualify field variables with "this" to distinguish them from local variables.
What do you think about that?
Could i have points deducted from mine assignment
because i am using field variable with "this" ?
Is there somebody that "clean" the SCJD exam
using that issue?
Thanks advance,
Claudio Luz
+Pie Number of slices to send: Send
Are your methods that long that they could be confused? If they are long I'd suggest spliting them up to smaller methods.
While "this" is fine, I haven't found an issue where I had to use "this". 1 because I wouldn't call Local Variables the same name as a class variable.
Have you read the Coding Standards site at Sun. Check the SCJD Links and FAQ page for the link to the Coding Standards web site.
Good luck
Mark
+Pie Number of slices to send: Send
 


I am using qualify field variables with "this" to distinguish them from local variables.


I can think of only one situation where this is appropriate: assigning values of member variables, such as in
setLastName(String lastName) {
this.lastName = lastName;
}
Eugene.
+Pie Number of slices to send: Send
Mark and Eugene, maybe, i have some longer methods or same a lot ones in a .java file, therefore if the program has > 400 lines, maybe
the use of qualify "this" can help the
"SUN assessor" identify quickly the type
of variable ( field or local, because the assessor, maybe, could be "homicidal maniac" and know where i live ), Is it Right?
I have, for example, a .java file to
15 instances variable and about 500 lines,
i think of that qualify the variable with
"this", could help other programmer,
at a glance, identify the kind of variable
quickly...
Claudio Luz
+Pie Number of slices to send: Send
 


I have, for example, a .java file to
15 instances variable and about 500 lines,
i think of that qualify the variable with
"this", could help other programmer,
at a glance, identify the kind of variable
quickly...


Oh, you are saying that you want to use this.myVariable instead of just myVariable even though there is no local myVariable, just the one that is a member of the class? That is, you want to always prefix a variable with "this.", if it is a class member variable? No, that's not a good practice at all.
To improve the readability of your code, follow these guidelines instead:
-- use meaningful variable names
-- declare the local variables just before they are used (as opposed the beginning of the method)
-- reduce the number of local variables by refactoring (such as extract method)
-- each class should cover a narrow, focused, and uniform set of responsibilities, i.e. avoid having methods such as getLastName() and generatePrimeNumber() in the same class
-- use helper methods and classes to perform secondary tasks
-- there are also multiple patterns (such as facade, MVC, factory) that help reduce the complexity of the code by breaking it into multiple loosely coupled pieces.
More generally, what you want to achieve is tight encapsulation, high cohesion, and low coupling among your classes. This translates into transparency of design, ease of reuse, and ultimately low cost of change/maintainance.
Regarding the 400 lines of code in the class, there are no hard rules on the size (although one page per class seems to be a common recommendation), but if you can refactor your code to make it easy to use and understand, do so by any means.
Also, as Mark suggested, read Java Coding Conventions, -- it is a Sun's recipe for naming, formatting, and documenting Java code.

Eugene.
[ February 13, 2003: Message edited by: Eugene Kononov ]
+Pie Number of slices to send: Send
Eugene, your suggestions for write my clear
and maintanable code using some design patterns,
code refactoring ( extract method ), use
appropriate method granularity and make the
local variables next to its access point
are worth for me, mainly, because the section of general considerations of assignment mark the total of 58 points
Now, about the use of the "errant this", i am beggining to beliave that compiler writers when add a qualify such as "this" in a programming language is just only for establish a difference between member and local variables owns the same name.. Well, I was thinking that the qualify "this" could have other use in software development.
Claudio Luz, thanks a lot.
+Pie Number of slices to send: Send
You can use "this." for clarification -- but only after all other options to clarify the code have been exhausted. I absolutely agree with Eugene, the fact that you feel it is necessary is a "code smell" suggesting that your code is badly in need of refactoring.
- Peter
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 572 times.
Similar Threads
Passing passwords
How to get ActionForm populated from disabled field.
xml parsers does not read such a large file contents
Population Drop Down Fields in JSP from Database table
retrieve
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:23:40.