[Discuss] Declare class field's type in scala class field

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Discuss] Declare class field's type in scala class field

Jinkui Shi
hi, all

Scala can infer the actual type if we didn’t declare its type. There also nothing different in the byte code of java class format.
It’s convenient for write the code, but hard to read. Maybe it’s time to face such bad smell code.
Scala check style plugin also have such rule. We can add this to the code guidelines.

So I have a proposal: Declare all the class field with its type as far as possible in Scala code. The variate in the function can be ignore if necessary.

What do you think about this?

The test below to show that declare type and type inference are the same after compiling.

Thanks
Jinkui Shi

--------------------------------------------------------------------------------------------------

object DeclareTypeTest {

  def main(args: Array[String]): Unit = {
    //  1
    declareTypeFunction
    //  2
    undeclareTypeFunction
  }

  def declareTypeFunction: Int = {
    1
  }

  def undeclareTypeFunction: Int = {
    1
  }
}


java -c DeclareTypeTest$.class :

  public void main(java.lang.String[]);
    Code:
       0: aload_0
       1: invokevirtual #18                 // Method declareTypeFunction:()I
       4: pop
       5: aload_0
       6: invokevirtual #21                 // Method undeclareTypeFunction:()I
       9: pop
      10: return

  public int declareTypeFunction();
    Code:
       0: iconst_1
       1: ireturn

  public int undeclareTypeFunction();
    Code:
       0: iconst_1
       1: ireturn


Reply | Threaded
Open this post in threaded view
|

Re: [Discuss] Declare class field's type in scala class field

Till Rohrmann
Agreed. I think it's a good idea to annotate the code with as much
information as possible to help other people to understand it. I think it's
a good idea to include this information in the coding guidelines. But I'm
not sure whether we have a dedicated Scala coding guideline.

Cheers,
Till

On Thu, Jan 26, 2017 at 10:27 AM, Jinkui Shi <[hidden email]> wrote:

> hi, all
>
> Scala can infer the actual type if we didn’t declare its type. There also
> nothing different in the byte code of java class format.
> It’s convenient for write the code, but hard to read. Maybe it’s time to
> face such bad smell code.
> Scala check style plugin also have such rule. We can add this to the code
> guidelines.
>
> So I have a proposal: Declare all the class field with its type as far as
> possible in Scala code. The variate in the function can be ignore if
> necessary.
>
> What do you think about this?
>
> The test below to show that declare type and type inference are the same
> after compiling.
>
> Thanks
> Jinkui Shi
>
> ------------------------------------------------------------
> --------------------------------------
>
> object DeclareTypeTest {
>
>   def main(args: Array[String]): Unit = {
>     //  1
>     declareTypeFunction
>     //  2
>     undeclareTypeFunction
>   }
>
>   def declareTypeFunction: Int = {
>     1
>   }
>
>   def undeclareTypeFunction: Int = {
>     1
>   }
> }
>
>
> java -c DeclareTypeTest$.class :
>
>   public void main(java.lang.String[]);
>     Code:
>        0: aload_0
>        1: invokevirtual #18                 // Method
> declareTypeFunction:()I
>        4: pop
>        5: aload_0
>        6: invokevirtual #21                 // Method
> undeclareTypeFunction:()I
>        9: pop
>       10: return
>
>   public int declareTypeFunction();
>     Code:
>        0: iconst_1
>        1: ireturn
>
>   public int undeclareTypeFunction();
>     Code:
>        0: iconst_1
>        1: ireturn
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [Discuss] Declare class field's type in scala class field

Bruno Aranda
Hi,

Twitter's Effective Scala contains very well-known and accepted conventions
for Scala:

http://twitter.github.io/effectivescala/

Cheers,

Bruno

On Thu, 26 Jan 2017 at 14:16 Till Rohrmann <[hidden email]> wrote:

> Agreed. I think it's a good idea to annotate the code with as much
> information as possible to help other people to understand it. I think it's
> a good idea to include this information in the coding guidelines. But I'm
> not sure whether we have a dedicated Scala coding guideline.
>
> Cheers,
> Till
>
> On Thu, Jan 26, 2017 at 10:27 AM, Jinkui Shi <[hidden email]> wrote:
>
> > hi, all
> >
> > Scala can infer the actual type if we didn’t declare its type. There also
> > nothing different in the byte code of java class format.
> > It’s convenient for write the code, but hard to read. Maybe it’s time to
> > face such bad smell code.
> > Scala check style plugin also have such rule. We can add this to the code
> > guidelines.
> >
> > So I have a proposal: Declare all the class field with its type as far as
> > possible in Scala code. The variate in the function can be ignore if
> > necessary.
> >
> > What do you think about this?
> >
> > The test below to show that declare type and type inference are the same
> > after compiling.
> >
> > Thanks
> > Jinkui Shi
> >
> > ------------------------------------------------------------
> > --------------------------------------
> >
> > object DeclareTypeTest {
> >
> >   def main(args: Array[String]): Unit = {
> >     //  1
> >     declareTypeFunction
> >     //  2
> >     undeclareTypeFunction
> >   }
> >
> >   def declareTypeFunction: Int = {
> >     1
> >   }
> >
> >   def undeclareTypeFunction: Int = {
> >     1
> >   }
> > }
> >
> >
> > java -c DeclareTypeTest$.class :
> >
> >   public void main(java.lang.String[]);
> >     Code:
> >        0: aload_0
> >        1: invokevirtual #18                 // Method
> > declareTypeFunction:()I
> >        4: pop
> >        5: aload_0
> >        6: invokevirtual #21                 // Method
> > undeclareTypeFunction:()I
> >        9: pop
> >       10: return
> >
> >   public int declareTypeFunction();
> >     Code:
> >        0: iconst_1
> >        1: ireturn
> >
> >   public int undeclareTypeFunction();
> >     Code:
> >        0: iconst_1
> >        1: ireturn
> >
> >
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: [Discuss] Declare class field's type in scala class field

Jinkui Shi
Hi, Till, Bruno

Thank for your reply.

Twitter’s scala programming suggestion is useful. I prefer the alexandru/scala-best-practices.

Like this rule `2.16. Public functions SHOULD have an explicit return type`, the class field type explicitly declaring is better for developer to read.

[1] https://github.com/alexandru/scala-best-practices/blob/master/sections/2-language-rules.md#216-public-functions-should-have-an-explicit-return-type <https://github.com/alexandru/scala-best-practices/blob/master/sections/2-language-rules.md#216-public-functions-should-have-an-explicit-return-type>

Thanks
Jinkui Shi

> On Jan 27, 2017, at 02:33, Bruno Aranda <[hidden email]> wrote:
>
> Hi,
>
> Twitter's Effective Scala contains very well-known and accepted conventions
> for Scala:
>
> http://twitter.github.io/effectivescala/
>
> Cheers,
>
> Bruno
>
> On Thu, 26 Jan 2017 at 14:16 Till Rohrmann <[hidden email]> wrote:
>
>> Agreed. I think it's a good idea to annotate the code with as much
>> information as possible to help other people to understand it. I think it's
>> a good idea to include this information in the coding guidelines. But I'm
>> not sure whether we have a dedicated Scala coding guideline.
>>
>> Cheers,
>> Till
>>
>> On Thu, Jan 26, 2017 at 10:27 AM, Jinkui Shi <[hidden email]> wrote:
>>
>>> hi, all
>>>
>>> Scala can infer the actual type if we didn’t declare its type. There also
>>> nothing different in the byte code of java class format.
>>> It’s convenient for write the code, but hard to read. Maybe it’s time to
>>> face such bad smell code.
>>> Scala check style plugin also have such rule. We can add this to the code
>>> guidelines.
>>>
>>> So I have a proposal: Declare all the class field with its type as far as
>>> possible in Scala code. The variate in the function can be ignore if
>>> necessary.
>>>
>>> What do you think about this?
>>>
>>> The test below to show that declare type and type inference are the same
>>> after compiling.
>>>
>>> Thanks
>>> Jinkui Shi
>>>
>>> ------------------------------------------------------------
>>> --------------------------------------
>>>
>>> object DeclareTypeTest {
>>>
>>>  def main(args: Array[String]): Unit = {
>>>    //  1
>>>    declareTypeFunction
>>>    //  2
>>>    undeclareTypeFunction
>>>  }
>>>
>>>  def declareTypeFunction: Int = {
>>>    1
>>>  }
>>>
>>>  def undeclareTypeFunction: Int = {
>>>    1
>>>  }
>>> }
>>>
>>>
>>> java -c DeclareTypeTest$.class :
>>>
>>>  public void main(java.lang.String[]);
>>>    Code:
>>>       0: aload_0
>>>       1: invokevirtual #18                 // Method
>>> declareTypeFunction:()I
>>>       4: pop
>>>       5: aload_0
>>>       6: invokevirtual #21                 // Method
>>> undeclareTypeFunction:()I
>>>       9: pop
>>>      10: return
>>>
>>>  public int declareTypeFunction();
>>>    Code:
>>>       0: iconst_1
>>>       1: ireturn
>>>
>>>  public int undeclareTypeFunction();
>>>    Code:
>>>       0: iconst_1
>>>       1: ireturn
>>>
>>>
>>>
>>