使用注释破折号来划分代码块

问题描述 投票:0回答:1

我正在尝试学习java,目的是为了找到一份工作。当我编写代码时,有时我发现使用教程中经常看到的格式来导航代码非常困难。我发现对我有帮助的是用长注释虚线划分代码块(下面的示例)。

这确实有帮助,但我担心这通常是否违反专业最佳实践。我发现它在视觉上很有吸引力,但公司或其他程序员会吗?

//package declaration
package word_builder; // declares the package to be used in testPackage

//----------------------------------------------------------------------------------------------------------------------
//imports
import java.util.Scanner;
//----------------------------------------------------------------------------------------------------------------------
public class rootSuffixCombine{ // creates the rootSuffix class to contain rootSuffix method
//----------------------------------------------------------------------------------------------------------------------
//methods
        public static void rootSuffix(){   // method for combining just a root and a suffix
//----------------------------------------------------------------------------------------------------------------------
            // initial prompt and scanner creation
            System.out.println("Do you want to make a word?");
            Scanner partTaker = new Scanner(System.in);
            String answer = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
//variable initialization to start loops
            int x = 1;
            int y = 1;
//----------------------------------------------------------------------------------------------------------------------
// outer while loop for taking user input to either continue with program or end it
            while (x == 1){
//----------------------------------------------------------------------------------------------------------------------
// affirmative case (continues program, prints results, then asks if you want to make another)
                if (answer.equalsIgnoreCase("yes")){ // sets answer and ignores case
//----------------------------------------------------------------------------------------------------------------------

                    System.out.print("please input \"root\": ");  // takes the root
                    String root = partTaker.nextLine();
                    System.out.print("please input \"suffix\": ");  // takes the suffix
                    String suffix = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
// main output
                    System.out.println("The word you created is: " + "\"" + root.concat(suffix) + "\"");
//----------------------------------------------------------------------------------------------------------------------
//resets loop in case user wants to make another word
                    y = 1;
//----------------------------------------------------------------------------------------------------------------------
                    // second while loop for creating more words
                    while (y == 1){
//----------------------------------------------------------------------------------------------------------------------
// prompts for second word
                        System.out.println("Do you want to make another word?");
                        answer = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
//affirmative case
                        if (answer.equalsIgnoreCase("yes")) {
//ends second loop
                            y = 0;}
//----------------------------------------------------------------------------------------------------------------------
// negative case
                        else if (answer.equalsIgnoreCase("no")) {  //ignores case
// closes second loop
                            break;}
//----------------------------------------------------------------------------------------------------------------------
//handling non-standard answers
                         else{
                            System.out.println("sorry, i dont understand");
//resets loop
                            x = 1;
                            y = 1;}}}
// end of second loop
//----------------------------------------------------------------------------------------------------------------------
// negative case (main loop)
                else if (answer.equalsIgnoreCase("no")) {
                    System.out.println("OK, shutting down.");
                    break;} //ends loop
//----------------------------------------------------------------------------------------------------------------------
// catches non-standard response and repeats initial question until "yes" or "no" is given
                else{
                    System.out.println("I dont understand, try again.");
                    answer = partTaker.nextLine();}}}
//----------------------------------------------------------------------------------------------------------------------
// main, which runs the method(S)
    public static void main(String[] args){
        rootSuffix();}}
//----------------------------------------------------------------------------------------------------------------------
//END

java comments
1个回答
0
投票

向 10 名开发人员询问如何格式化源代码,您至少会得到关于该主题的 11 条意见。

您会发现关于 Javadoc 注释的要求存在一些共识,并且 Javadoc 工具定义了有关如何格式化它们的一些基本要求。

还有 Sun 编码指南,定义了如何命名类和其他内容等内容,但对于 格式 来说,它不是很明确。

但除此之外,没有什么是真正固定的。是否使用 K&C 风格的格式化已经开始了

final void method (final int value) {
  return value * (4-value);
}

与每个大括号使用新行的样式

final void method( final int value ) 
{
    return value * (4 - value);
}

用于缩进的空格数量可能会有所不同(或者您使用制表符来达到此目的),以及是否使用 ASCII 图形向代码添加某些结构。

为了对此进行一些排序,公司和项目定义了涵盖该主题的(有时非常冗长且详细的)编码指南,当您为他们工作时,您应该遵循它。

但是,无论你个人的代码格式设置方式是令人震惊还是吸引人,显然都是审阅者的意见,我不敢就此给你任何建议——你可能会遇到那些对“良好格式设置”的看法与我完全相反的人,这可能会让你失去一份有趣工作的机会。

© www.soinside.com 2019 - 2024. All rights reserved.