- Existem diversas discussões sobre qual deve ser o tamanho de uma função.
- Mas algo mais importante é se perguntar: "Quando devemos envolver um código na sua própria função?"
- Algumas pessoas se guiam por:
- tamanho - uma função não deve ser tão grande que não caiba na tela
- reuso - qualquer código utilizado mais de uma vez deve ser colocado em uma função, caso contrário, deve ser deixado inline
- Uma abordagem interessante é separação entre intenção e implementação.
- Se você tiver que se esforçar ao olhar um fragmento de código para entender o que ele faz, o código deve ser extraído para um função e a função nomeada.
- Quando você ler o código novamente, o propósito da função ficará explícito sem a necessidade de entender o seu funcionamento internamente.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'dart:ui' as ui; | |
| import 'package:flutter/services.dart' show rootBundle; | |
| import 'dart:async'; | |
| import 'dart:typed_data'; | |
| void main() => runApp(new MyApp()); | |
| class MyApp extends StatelessWidget { | |
| // This widget is the root of your application. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) 2017 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) 2017 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) 2017 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var functions = require('firebase-functions'); | |
| const sendgrid = require('sendgrid') | |
| const client = sendgrid("YOUR_SG_API_KEY") | |
| function parseBody(body) { | |
| var helper = sendgrid.mail; | |
| var fromEmail = new helper.Email(body.from); | |
| var toEmail = new helper.Email(body.to); | |
| var subject = body.subject; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ImageSliderAdapter extends FragmentPagerAdapter { | |
| private final List<Fragment> fragmentList = new ArrayList<>(); | |
| public ImageSliderAdapter(@NonNull FragmentManager fm) { | |
| super(fm); | |
| } | |
| @Override | |
| public Fragment getItem(int position) { | |
| return fragmentList.get(position); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends Activity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| // Always register before calling into the super class. | |
| getApplication().registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks(this)); | |
| super.onCreate(savedInstanceState); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pascalwelsch.extensions | |
| import android.app.Activity | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.os.Build | |
| import android.os.Bundle | |
| /** | |
| * Extensions for simpler launching of Activities |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package <you_package>.data.api; | |
| import android.content.Context; | |
| import android.support.annotation.NonNull; | |
| import com.google.gson.ExclusionStrategy; | |
| import com.google.gson.FieldAttributes; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; |